@@ -262,7 +262,7 @@ def path_finder(curr_node, next_node, dest_node, parent, path):
262262
263263def test_shortest_paths ():
264264
265- def _test_shortest_paths (ds , algorithm ):
265+ def _test_shortest_paths_positive_edges (ds , algorithm ):
266266 import pydatastructs .utils .misc_util as utils
267267 GraphNode = getattr (utils , "Adjacency" + ds + "GraphNode" )
268268 vertices = [GraphNode ('S' ), GraphNode ('C' ),
@@ -288,10 +288,57 @@ def _test_shortest_paths(ds, algorithm):
288288 graph .add_edge ('D' , 'SLC' , - 10 )
289289 assert raises (ValueError , lambda : shortest_paths (graph , 'bellman_ford' , 'SLC' ))
290290
291- _test_shortest_paths ("List" , 'bellman_ford' )
292- _test_shortest_paths ("Matrix" , 'bellman_ford' )
293- _test_shortest_paths ("List" , 'dijkstra' )
294- _test_shortest_paths ("Matrix" , 'dijkstra' )
291+ def _test_shortest_paths_negative_edges (ds , algorithm ):
292+ import pydatastructs .utils .misc_util as utils
293+ GraphNode = getattr (utils , "Adjacency" + ds + "GraphNode" )
294+ vertices = [GraphNode ('s' ), GraphNode ('a' ),
295+ GraphNode ('b' ), GraphNode ('c' ),
296+ GraphNode ('d' )]
297+
298+ graph = Graph (* vertices )
299+ graph .add_edge ('s' , 'a' , 3 )
300+ graph .add_edge ('s' , 'b' , 2 )
301+ graph .add_edge ('a' , 'c' , 1 )
302+ graph .add_edge ('b' , 'd' , 1 )
303+ graph .add_edge ('b' , 'a' , - 2 )
304+ graph .add_edge ('c' , 'd' , 1 )
305+ dist , pred = shortest_paths (graph , algorithm , 's' )
306+ assert dist == {'s' : 0 , 'a' : 0 , 'b' : 2 , 'c' : 1 , 'd' : 2 }
307+ assert pred == {'s' : None , 'a' : 'b' , 'b' : 's' , 'c' : 'a' , 'd' : 'c' }
308+ dist , pred = shortest_paths (graph , algorithm , 's' , 'd' )
309+ assert dist == 2
310+ assert pred == {'s' : None , 'a' : 'b' , 'b' : 's' , 'c' : 'a' , 'd' : 'c' }
311+
312+ _test_shortest_paths_positive_edges ("List" , 'bellman_ford' )
313+ _test_shortest_paths_positive_edges ("Matrix" , 'bellman_ford' )
314+ _test_shortest_paths_negative_edges ("List" , 'bellman_ford' )
315+ _test_shortest_paths_negative_edges ("Matrix" , 'bellman_ford' )
316+ _test_shortest_paths_positive_edges ("List" , 'dijkstra' )
317+ _test_shortest_paths_positive_edges ("Matrix" , 'dijkstra' )
318+
319+ def test_all_pair_shortest_paths ():
320+
321+ def _test_shortest_paths_negative_edges (ds , algorithm ):
322+ import pydatastructs .utils .misc_util as utils
323+ GraphNode = getattr (utils , "Adjacency" + ds + "GraphNode" )
324+ vertices = [GraphNode ('1' ), GraphNode ('2' ),
325+ GraphNode ('3' ), GraphNode ('4' )]
326+
327+ graph = Graph (* vertices )
328+ graph .add_edge ('1' , '3' , - 2 )
329+ graph .add_edge ('2' , '1' , 4 )
330+ graph .add_edge ('2' , '3' , 3 )
331+ graph .add_edge ('3' , '4' , 2 )
332+ graph .add_edge ('4' , '2' , - 1 )
333+ dist , next_v = shortest_paths (graph , algorithm , 's' )
334+ assert dist == {'1' : {'3' : - 2 , '1' : 0 , '4' : 0 , '2' : - 1 },
335+ '2' : {'1' : 4 , '3' : 2 , '2' : 0 , '4' : 4 },
336+ '3' : {'4' : 2 , '3' : 0 , '1' : 5 , '2' : 1 },
337+ '4' : {'2' : - 1 , '4' : 0 , '1' : 3 , '3' : 1 }}
338+ assert next_v == {'1' : {'3' : '1' , '1' : '1' , '4' : None , '2' : None },
339+ '2' : {'1' : '2' , '3' : None , '2' : '2' , '4' : None },
340+ '3' : {'4' : '3' , '3' : '3' , '1' : None , '2' : None },
341+ '4' : {'2' : '4' , '4' : '4' , '1' : None , '3' : None }}
295342
296343def test_topological_sort ():
297344
0 commit comments