Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions cyaron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
weight_limit = kwargs.get("weight_limit", (1, 1))
if not isinstance(weight_limit, tuple):
weight_limit = (1, weight_limit)
weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1]))

if not 0 <= chain <= 1 or not 0 <= flower <= 1:
raise Exception("chain and flower must be between 0 and 1")
Expand All @@ -83,15 +84,15 @@ def tree(point_count, chain=0, flower=0, **kwargs):
random_count = point_count - 1 - chain_count - flower_count

for i in range(2, chain_count+2):
weight = random.randint(weight_limit[0], weight_limit[1])
weight = weight_gen()
graph.add_edge(i-1, i, weight=weight)

for i in range(chain_count+2, chain_count+flower_count+2):
weight = random.randint(weight_limit[0], weight_limit[1])
weight = weight_gen()
graph.add_edge(1, i, weight=weight)

for i in range(point_count-random_count+1, point_count+1):
weight = random.randint(weight_limit[0], weight_limit[1])
weight = weight_gen()
u = random.randrange(1, i)
graph.add_edge(u, i, weight=weight)

Expand All @@ -103,6 +104,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
weight_limit = kwargs.get("weight_limit", (1, 1))
if not isinstance(weight_limit, tuple):
weight_limit = (1, weight_limit)
weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1]))

if not 0 <= left <= 1 or not 0 <= right <= 1:
raise Exception("left and right must be between 0 and 1")
Expand All @@ -114,7 +116,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
graph = Graph(point_count,directed)
for i in range(2, point_count+1):
edge_pos = random.uniform(0, 1)
weight = random.randint(weight_limit[0], weight_limit[1])
weight = weight_gen()

node = 0
if edge_pos < left or left+right < edge_pos <= (1.0-left-right)/2: # Left
Expand All @@ -132,16 +134,43 @@ def binary_tree(point_count, left=0, right=0, **kwargs):

@staticmethod
def graph(point_count, edge_count, **kwargs):
directed = kwargs.get("directed", True)
directed = kwargs.get("directed", False)
weight_limit = kwargs.get("weight_limit", (1,1))
if not isinstance(weight_limit, tuple):
weight_limit = (1, weight_limit)

graph = Graph(point_count)
weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1]))
graph = Graph(point_count,directed)
for i in range(edge_count):
u = random.randint(1, point_count)
v = random.randint(1, point_count)
weight = random.randint(weight_limit[0], weight_limit[1])
graph.add_edge(u, v, weight=weight, directed=directed)
weight = weight_gen()
graph.add_edge(u, v, weight=weight)
return graph

#hack spfa (maybe?)
@staticmethod
def hack_spfa(point_count, **kwargs):
directed = kwargs.get("directed", False)
extraedg = kwargs.get("extra_edge", 2)
weight_limit = kwargs.get("weight_limit", (1,1))
if not isinstance(weight_limit, tuple):
weight_limit = (1, weight_limit)
weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1]))
skp=point_count+3
graph=Graph(point_count,directed)
if point_count%2==1:
skp=point_count/2+1
half=point_count/2
for i in range(1,half):
(x,y) = (i,i+1)
graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen())
(x,y) = (i+half,i+half+1)
graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen())
for i in range(1,half+1):
(x,y) = (i,i+half)
graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen())
for i in range(extraedg):
u = random.randint(1, point_count)
v = random.randint(1, point_count)
graph.add_edge(u, v, weight=weight_gen())
return graph
8 changes: 2 additions & 6 deletions test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
from cyaron import *

_n = ati([0, 7, 50])
_m = ati([0, 11, 100])

def oper(x):
return "%d %d" % (x.start, x.end)
return "%d %d" % (x.start, x.end)

for i in range(1, 3):
test_data = IO(file_prefix="test", data_id=i)
n = _n[i]
m = _m[i]
s = randint(1, n)
t = randint(1, n)
test_data.writeln(n, m, s, t)
test_data.writeln(n)
graph = Graph.tree(n,0.3,0.3)
test_data.writeln(graph.to_str(output=oper))
test_data.writeln('============Shuffle============')
Expand Down
23 changes: 23 additions & 0 deletions test3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

from cyaron import *
import random

_n = ati([0, 7, 8])

def oper(x):
return "%d %d weight=%d,%d" % (x.start, x.end, x.weight[0], x.weight[1])

def wg():
return [random.randint(3,5),random.randint(1,10)]

for i in range(1, 3):
test_data = IO(file_prefix="test", data_id=i)
n = _n[i]
test_data.writeln(n)
graph = Graph.hack_spfa(n,weight_gen=wg)
test_data.writeln(graph.to_str(output=oper))
test_data.writeln('============Shuffle============')
test_data.writeln(graph.to_str(output=oper,shuffle=True))
test_data.output_gen("~/Downloads/std_binary")
# A binary file or shell command that accepts input from stdin and outputs to stdout