@@ -229,6 +229,8 @@ def graph(point_count, edge_count, **kwargs):
229
229
int point_count -> the count of vertexes
230
230
int edge_count -> the count of edges
231
231
**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
232
234
bool directed = False -> whether the chain is directed(true:directed,false:not directed)
233
235
(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)
234
236
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):
237
239
-> the generator of the weights. It should return the weight. The default way is to use the random.randint()
238
240
"""
239
241
directed = kwargs .get ("directed" , False )
242
+ self_loop = kwargs .get ("self_loop" , True )
243
+ repeated_edges = kwargs .get ("repeated_edges" , True )
240
244
weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
241
245
if not list_like (weight_limit ):
242
246
weight_limit = (1 , weight_limit )
243
247
weight_gen = kwargs .get (
244
248
"weight_gen" , lambda : random .randint (
245
249
weight_limit [0 ], weight_limit [1 ]))
246
250
graph = Graph (point_count , directed )
251
+ used_edges = set ()
247
252
for i in range (edge_count ):
248
253
u = random .randint (1 , point_count )
249
254
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
+
250
261
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 ))
251
267
return graph
252
268
253
269
@staticmethod
0 commit comments