@@ -269,6 +269,11 @@ def graph(point_count, edge_count, **kwargs):
269269 directed = kwargs .get ("directed" , False )
270270 self_loop = kwargs .get ("self_loop" , True )
271271 repeated_edges = kwargs .get ("repeated_edges" , True )
272+ if not repeated_edges :
273+ max_edge = Graph ._calc_max_edge (point_count , directed , self_loop )
274+ if edge_count > max_edge :
275+ 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 ))
276+
272277 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
273278 if not list_like (weight_limit ):
274279 weight_limit = (1 , weight_limit )
@@ -318,6 +323,11 @@ def DAG(point_count, edge_count, **kwargs):
318323 self_loop = kwargs .get ("self_loop" , False ) # DAG default has no loop
319324 repeated_edges = kwargs .get ("repeated_edges" , True )
320325 loop = kwargs .get ("loop" , False )
326+ if not repeated_edges :
327+ max_edge = Graph ._calc_max_edge (point_count , not loop , self_loop )
328+ if edge_count > max_edge :
329+ 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 ))
330+
321331 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
322332 if not list_like (weight_limit ):
323333 weight_limit = (1 , weight_limit )
@@ -378,6 +388,11 @@ def UDAG(point_count, edge_count, **kwargs):
378388
379389 self_loop = kwargs .get ("self_loop" , True )
380390 repeated_edges = kwargs .get ("repeated_edges" , True )
391+ if not repeated_edges :
392+ max_edge = Graph ._calc_max_edge (point_count , False , self_loop )
393+ if edge_count > max_edge :
394+ 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 ))
395+
381396 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
382397 if not list_like (weight_limit ):
383398 weight_limit = (1 , weight_limit )
@@ -459,3 +474,12 @@ def hack_spfa(point_count, **kwargs):
459474 graph .add_edge (u , v , weight = weight_gen ())
460475
461476 return graph
477+
478+ @staticmethod
479+ def _calc_max_edge (point_count , directed , self_loop ):
480+ max_edge = point_count * (point_count - 1 )
481+ if not directed :
482+ max_edge //= 2
483+ if self_loop :
484+ max_edge += point_count
485+ return max_edge
0 commit comments