@@ -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
0 commit comments