Skip to content

Commit 2cffe89

Browse files
Prerak SinghPrerak Singh
authored andcommitted
johnson's algorithm and fixed tests for all pair shortes
t paths
1 parent a98308d commit 2cffe89

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import deque
66
from concurrent.futures import ThreadPoolExecutor
77
from pydatastructs.utils.misc_util import (
8-
_comp, raise_if_backend_is_not_python, Backend)
8+
_comp, raise_if_backend_is_not_python, Backend, AdjacencyListGraphNode)
99
from pydatastructs.miscellaneous_data_structures import (
1010
DisjointSetForest, PriorityQueue)
1111
from pydatastructs.graphs.graph import Graph
@@ -799,7 +799,7 @@ def _dijkstra_adjacency_list(graph: Graph, start: str, target: str):
799799
visited[u] = True
800800
for v in graph.vertices:
801801
edge_str = u + '_' + v
802-
if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value > 0 and
802+
if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value >= 0 and
803803
visited[v] is False and dist[v] > dist[u] + graph.edge_weights[edge_str].value):
804804
dist[v] = dist[u] + graph.edge_weights[edge_str].value
805805
pred[v] = u
@@ -900,6 +900,53 @@ def _floyd_warshall_adjacency_list(graph: Graph):
900900

901901
_floyd_warshall_adjacency_matrix = _floyd_warshall_adjacency_list
902902

903+
def _johnson_adjacency_list(graph: Graph):
904+
new_vertex=AdjacencyListGraphNode('q')
905+
graph.add_vertex(new_vertex)
906+
907+
for vertex in graph.vertices:
908+
if vertex != 'q':
909+
graph.add_edge('q',vertex,0)
910+
911+
distances, predecessors=shortest_paths(graph,'bellman_ford','q')
912+
913+
edges_to_remove=[]
914+
for edge in graph.edge_weights:
915+
edge_node=graph.edge_weights[edge]
916+
if edge_node.source.name=='q':
917+
edges_to_remove.append((edge_node.source.name,edge_node.target.name))
918+
919+
for u,v in edges_to_remove:
920+
graph.remove_edge(u,v)
921+
graph.remove_vertex('q')
922+
923+
for edge in graph.edge_weights:
924+
edge_node=graph.edge_weights[edge]
925+
u,v=edge_node.source.name,edge_node.target.name
926+
graph.edge_weights[edge].value+=distances[u]-distances[v]
927+
928+
print(graph.edge_weights)
929+
all_distances={}
930+
all_next_vertex={}
931+
932+
for vertex in graph.vertices:
933+
u = vertex
934+
dijkstra_dist,dijkstra_pred=shortest_paths(graph, 'dijkstra', u)
935+
print(dijkstra_pred)
936+
all_distances[u]={}
937+
all_next_vertex[u] = {}
938+
for v in graph.vertices:
939+
if dijkstra_pred[v]==None or dijkstra_pred[v]==u :
940+
all_next_vertex[u][v]=u
941+
else:
942+
all_next_vertex[u][v]=None
943+
if v in dijkstra_dist:
944+
all_distances[u][v]=dijkstra_dist[v]-distances[u]+distances[v]
945+
else:
946+
all_distances[u][v]=float('inf')
947+
948+
return (all_distances,all_next_vertex)
949+
903950
def topological_sort(graph: Graph, algorithm: str,
904951
**kwargs) -> list:
905952
"""

pydatastructs/graphs/tests/test_algorithms.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydatastructs import (breadth_first_search, Graph,
22
breadth_first_search_parallel, minimum_spanning_tree,
33
minimum_spanning_tree_parallel, strongly_connected_components,
4-
depth_first_search, shortest_paths, topological_sort,
4+
depth_first_search, shortest_paths,all_pair_shortest_paths, topological_sort,
55
topological_sort_parallel, max_flow)
66
from pydatastructs.utils.raises_util import raises
77

@@ -336,7 +336,7 @@ def _test_shortest_paths_negative_edges(ds, algorithm):
336336
graph.add_edge('2', '3', 3)
337337
graph.add_edge('3', '4', 2)
338338
graph.add_edge('4', '2', -1)
339-
dist, next_v = shortest_paths(graph, algorithm, 's')
339+
dist, next_v = all_pair_shortest_paths(graph, algorithm)
340340
assert dist == {'1': {'3': -2, '1': 0, '4': 0, '2': -1},
341341
'2': {'1': 4, '3': 2, '2': 0, '4': 4},
342342
'3': {'4': 2, '3': 0, '1': 5, '2': 1},
@@ -346,6 +346,10 @@ def _test_shortest_paths_negative_edges(ds, algorithm):
346346
'3': {'4': '3', '3': '3', '1': None, '2': None},
347347
'4': {'2': '4', '4': '4', '1': None, '3': None}}
348348

349+
_test_shortest_paths_negative_edges("List",'floyd_warshall')
350+
_test_shortest_paths_negative_edges("Matrix",'floyd_warshall')
351+
_test_shortest_paths_negative_edges("List",'johnson')
352+
349353
def test_topological_sort():
350354

351355
def _test_topological_sort(func, ds, algorithm, threads=None):

0 commit comments

Comments
 (0)