Skip to content

Commit 2629bbd

Browse files
Prerak SinghPrerak Singh
authored andcommitted
Documentation Update
1 parent 2cffe89 commit 2629bbd

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ def all_pair_shortest_paths(graph: Graph, algorithm: str,
826826
are implemented,
827827
828828
'floyd_warshall' -> Floyd Warshall algorithm as given in [1].
829+
'johnson' -> Johnson's Algorithm as given in [2]
829830
backend: pydatastructs.Backend
830831
The backend to be used.
831832
Optional, by default, the best available
@@ -858,6 +859,7 @@ def all_pair_shortest_paths(graph: Graph, algorithm: str,
858859
==========
859860
860861
.. [1] https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
862+
.. [2] https://en.wikipedia.org/wiki/Johnson's_algorithm
861863
"""
862864
raise_if_backend_is_not_python(
863865
all_pair_shortest_paths, kwargs.get('backend', Backend.PYTHON))
@@ -901,49 +903,47 @@ def _floyd_warshall_adjacency_list(graph: Graph):
901903
_floyd_warshall_adjacency_matrix = _floyd_warshall_adjacency_list
902904

903905
def _johnson_adjacency_list(graph: Graph):
904-
new_vertex=AdjacencyListGraphNode('q')
906+
new_vertex=AdjacencyListGraphNode('__q__')
905907
graph.add_vertex(new_vertex)
906908

907909
for vertex in graph.vertices:
908-
if vertex != 'q':
909-
graph.add_edge('q',vertex,0)
910+
if vertex != '__q__':
911+
graph.add_edge('__q__',vertex,0)
910912

911-
distances, predecessors=shortest_paths(graph,'bellman_ford','q')
913+
distances, predecessors = shortest_paths(graph,'bellman_ford','__q__')
912914

913-
edges_to_remove=[]
915+
edges_to_remove = []
914916
for edge in graph.edge_weights:
915-
edge_node=graph.edge_weights[edge]
916-
if edge_node.source.name=='q':
917+
edge_node = graph.edge_weights[edge]
918+
if edge_node.source.name == '__q__':
917919
edges_to_remove.append((edge_node.source.name,edge_node.target.name))
918920

919921
for u,v in edges_to_remove:
920922
graph.remove_edge(u,v)
921-
graph.remove_vertex('q')
923+
graph.remove_vertex('__q__')
922924

923925
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]
926+
edge_node = graph.edge_weights[edge]
927+
u,v = edge_node.source.name,edge_node.target.name
928+
graph.edge_weights[edge].value += distances[u]-distances[v]
927929

928-
print(graph.edge_weights)
929-
all_distances={}
930-
all_next_vertex={}
930+
all_distances = {}
931+
all_next_vertex = {}
931932

932933
for vertex in graph.vertices:
933934
u = vertex
934-
dijkstra_dist,dijkstra_pred=shortest_paths(graph, 'dijkstra', u)
935-
print(dijkstra_pred)
936-
all_distances[u]={}
935+
dijkstra_dist,dijkstra_pred = shortest_paths(graph, 'dijkstra', u)
936+
all_distances[u] = {}
937937
all_next_vertex[u] = {}
938938
for v in graph.vertices:
939-
if dijkstra_pred[v]==None or dijkstra_pred[v]==u :
940-
all_next_vertex[u][v]=u
939+
if dijkstra_pred[v] is None or dijkstra_pred[v] == u :
940+
all_next_vertex[u][v] = u
941941
else:
942-
all_next_vertex[u][v]=None
942+
all_next_vertex[u][v] = None
943943
if v in dijkstra_dist:
944-
all_distances[u][v]=dijkstra_dist[v]-distances[u]+distances[v]
944+
all_distances[u][v] = dijkstra_dist[v]-distances[u]+distances[v]
945945
else:
946-
all_distances[u][v]=float('inf')
946+
all_distances[u][v] = float('inf')
947947

948948
return (all_distances,all_next_vertex)
949949

0 commit comments

Comments
 (0)