@@ -51,7 +51,7 @@ def to_str(self, **kwargs):
5151 edge_buf = []
5252 for edge in self .iterate_edges ():
5353 edge_buf .append (
54- Edge (tmp [edge .start ], tmp [edge .end ], edge .weight ))
54+ Edge (new_node_id [edge .start ], new_node_id [edge .end ], edge .weight ))
5555 random .shuffle (edge_buf )
5656 for edge in edge_buf :
5757 if not self .directed and random .randint (0 , 1 ) == 0 :
@@ -74,7 +74,8 @@ def iterate_edges(self):
7474 """
7575 for node in self .edges :
7676 for edge in node :
77- yield edge
77+ if edge .end >= edge .start or self .directed :
78+ yield edge
7879
7980 def __add_edge (self , x , y , w ):
8081 """__add_edge(self, x, y, w) -> None
@@ -92,7 +93,6 @@ def add_edge(self, x, y, **kwargs):
9293 not directed means if you added the edge x->y, you would also add the edge y->x
9394 """
9495 weight = kwargs .get ("weight" , 1 )
95- directed = kwargs .get ("directed" , True )
9696 self .__add_edge (x , y , weight )
9797 if not self .directed and x != y :
9898 self .__add_edge (y , x , weight )
@@ -145,7 +145,7 @@ def tree(point_count, chain=0, flower=0, **kwargs):
145145 """
146146 directed = kwargs .get ("directed" , False )
147147 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
148- if not isinstance (weight_limit , tuple ):
148+ if not list_like (weight_limit ):
149149 weight_limit = (1 , weight_limit )
150150 weight_gen = kwargs .get (
151151 "weight_gen" , lambda : random .randint (
@@ -193,7 +193,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
193193 """
194194 directed = kwargs .get ("directed" , False )
195195 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
196- if not isinstance (weight_limit , tuple ):
196+ if not list_like (weight_limit ):
197197 weight_limit = (1 , weight_limit )
198198 weight_gen = kwargs .get (
199199 "weight_gen" , lambda : random .randint (
@@ -204,11 +204,11 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
204204 if left + right > 1 :
205205 raise Exception ("left plus right must be smaller than 1" )
206206
207- can_left = { 1 }
208- can_right = { 1 }
207+ can_left = set ([ 1 ])
208+ can_right = set ([ 1 ])
209209 graph = Graph (point_count , directed )
210210 for i in range (2 , point_count + 1 ):
211- edge_pos = random .uniform ( 0 , 1 )
211+ edge_pos = random .random ( )
212212 node = 0
213213 # Left
214214 if edge_pos < left or left + right < edge_pos <= (1.0 - left - right ) / 2 :
@@ -240,7 +240,7 @@ def graph(point_count, edge_count, **kwargs):
240240 """
241241 directed = kwargs .get ("directed" , False )
242242 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
243- if not isinstance (weight_limit , tuple ):
243+ if not list_like (weight_limit ):
244244 weight_limit = (1 , weight_limit )
245245 weight_gen = kwargs .get (
246246 "weight_gen" , lambda : random .randint (
@@ -250,9 +250,8 @@ def graph(point_count, edge_count, **kwargs):
250250 u = random .randint (1 , point_count )
251251 v = random .randint (1 , point_count )
252252 graph .add_edge (u , v , weight = weight_gen ())
253- return graph
254-
255- # hack spfa (maybe?)
253+ return graph
254+
256255 @staticmethod
257256 def hack_spfa (point_count , ** kwargs ):
258257 """hack_spfa(point_count, **kwargs) -> None
@@ -270,7 +269,7 @@ def hack_spfa(point_count, **kwargs):
270269 directed = kwargs .get ("directed" , False )
271270 extraedg = kwargs .get ("extra_edge" , 2 )
272271 weight_limit = kwargs .get ("weight_limit" , (1 , 1 ))
273- if not isinstance (weight_limit , tuple ):
272+ if not list_like (weight_limit ):
274273 weight_limit = (1 , weight_limit )
275274 weight_gen = kwargs .get (
276275 "weight_gen" , lambda : random .randint (
@@ -280,7 +279,7 @@ def hack_spfa(point_count, **kwargs):
280279 graph = Graph (point_count , directed )
281280 if point_count % 2 == 1 :
282281 point_to_skip = point_count / 2 + 1
283- half = point_count / 2
282+ half = int ( point_count / 2 )
284283
285284 for i in range (1 , half ):
286285 (x , y ) = (i , i + 1 )
@@ -298,4 +297,5 @@ def hack_spfa(point_count, **kwargs):
298297 u = random .randint (1 , point_count )
299298 v = random .randint (1 , point_count )
300299 graph .add_edge (u , v , weight = weight_gen ())
300+
301301 return graph
0 commit comments