Skip to content

Commit ef39841

Browse files
committed
Allow disabling self loop and repeated edges in a graph
1 parent 78a2937 commit ef39841

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

cyaron/graph.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def graph(point_count, edge_count, **kwargs):
229229
int point_count -> the count of vertexes
230230
int edge_count -> the count of edges
231231
**kwargs(Keyword args):
232+
bool self_loop = True -> whether to allow self loops or not
233+
bool repeated_edges = True -> whether to allow repeated edges or not
232234
bool directed = False -> whether the chain is directed(true:directed,false:not directed)
233235
(int,int) weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
234236
int weight_limit -> If you use a int for this arg, it means the max limit of the weight(included)
@@ -237,17 +239,31 @@ def graph(point_count, edge_count, **kwargs):
237239
-> the generator of the weights. It should return the weight. The default way is to use the random.randint()
238240
"""
239241
directed = kwargs.get("directed", False)
242+
self_loop = kwargs.get("self_loop", True)
243+
repeated_edges = kwargs.get("repeated_edges", True)
240244
weight_limit = kwargs.get("weight_limit", (1, 1))
241245
if not list_like(weight_limit):
242246
weight_limit = (1, weight_limit)
243247
weight_gen = kwargs.get(
244248
"weight_gen", lambda: random.randint(
245249
weight_limit[0], weight_limit[1]))
246250
graph = Graph(point_count, directed)
251+
used_edges = set()
247252
for i in range(edge_count):
248253
u = random.randint(1, point_count)
249254
v = random.randint(1, point_count)
255+
256+
if (not self_loop and u == v) or (not repeated_edges and (u, v) in used_edges):
257+
# Then we generate a new pair of nodes
258+
i -= 1
259+
continue
260+
250261
graph.add_edge(u, v, weight=weight_gen())
262+
263+
if not repeated_edges:
264+
used_edges.add((u, v))
265+
if not directed:
266+
used_edges.add((v, u))
251267
return graph
252268

253269
@staticmethod

examples/test_simple_graph.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
t = randint(1, n) #
1717
test_data.input_writeln(n, m, s, t) # Write n,m,s,t to the input file
1818

19-
graph = Graph.graph(n, m, weight_limit=5) # Generate a graph with n vertexs, m edges and weights less than 5
20-
test_data.input_writeln(graph) # Write the graph (the graph object will process the string)
19+
graph = Graph.graph(n, m, weight_limit=5, self_loop=False, repeated_edges=False)
20+
# Generate a graph with n vertexs, m edges and weights less than 5, no self loops and repeated edges
21+
test_data.input_writeln(graph.to_str(shuffle=True))
22+
# Write the graph and shuffle it
2123

22-
test_data.output_gen("~/Downloads/std_binary") # Use the solve programme to generate the output file
24+
#test_data.output_gen("~/Downloads/std_binary") # Use the solve programme to generate the output file
2325

2426
# You don't need to close the files, the IO object will do it

0 commit comments

Comments
 (0)