@@ -260,6 +260,11 @@ def graph(point_count, edge_count, **kwargs):
260260 directed = kwargs .get ("directed" , False )
261261 self_loop = kwargs .get ("self_loop" , True )
262262 repeated_edges = kwargs .get ("repeated_edges" , True )
263+ if not repeated_edges :
264+ max_edge = Graph ._calc_max_edge (point_count , directed , self_loop )
265+ if edge_count > max_edge :
266+ raise Exception ("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count , max_edge ))
267+
263268 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
264269 if not list_like (weight_limit ):
265270 weight_limit = (1 , weight_limit )
@@ -309,6 +314,11 @@ def DAG(point_count, edge_count, **kwargs):
309314 self_loop = kwargs .get ("self_loop" , False ) # DAG default has no loop
310315 repeated_edges = kwargs .get ("repeated_edges" , True )
311316 loop = kwargs .get ("loop" , False )
317+ if not repeated_edges :
318+ max_edge = Graph ._calc_max_edge (point_count , not loop , self_loop )
319+ if edge_count > max_edge :
320+ raise Exception ("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count , max_edge ))
321+
312322 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
313323 if not list_like (weight_limit ):
314324 weight_limit = (1 , weight_limit )
@@ -369,6 +379,11 @@ def UDAG(point_count, edge_count, **kwargs):
369379
370380 self_loop = kwargs .get ("self_loop" , True )
371381 repeated_edges = kwargs .get ("repeated_edges" , True )
382+ if not repeated_edges :
383+ max_edge = Graph ._calc_max_edge (point_count , False , self_loop )
384+ if edge_count > max_edge :
385+ raise Exception ("the number of edges of this kind of graph which has %d vertexes must be less than or equal to %d." % (point_count , max_edge ))
386+
372387 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
373388 if not list_like (weight_limit ):
374389 weight_limit = (1 , weight_limit )
@@ -450,3 +465,12 @@ def hack_spfa(point_count, **kwargs):
450465 graph .add_edge (u , v , weight = weight_gen ())
451466
452467 return graph
468+
469+ @staticmethod
470+ def _calc_max_edge (point_count , directed , self_loop ):
471+ max_edge = point_count * (point_count - 1 )
472+ if not directed :
473+ max_edge //= 2
474+ if self_loop :
475+ max_edge += point_count
476+ return max_edge
0 commit comments