22from collections .abc import Hashable
33
44Node = Hashable
5- Edge = tuple [Node , Node , float ]
6- Adjacency = dict [Node , list [tuple [Node , float ]]]
5+ edge = tuple [Node , Node , float ]
6+ adjacency = dict [Node , list [tuple [Node , float ]]]
77
88
9- def _collect_nodes_and_edges (graph : Adjacency ) -> tuple [list [Node ], list [Edge ]]:
9+ def _collect_nodes_and_edges (graph : adjacency ) -> tuple [list [Node ], list [edge ]]:
1010 nodes = set ()
11- edges : list [Edge ] = []
11+ edges : list [edge ] = []
1212 for u , neighbors in graph .items ():
1313 nodes .add (u )
1414 for v , w in neighbors :
@@ -17,7 +17,7 @@ def _collect_nodes_and_edges(graph: Adjacency) -> tuple[list[Node], list[Edge]]:
1717 return list (nodes ), edges
1818
1919
20- def _bellman_ford (nodes : list [Node ], edges : list [Edge ]) -> dict [Node , float ]:
20+ def _bellman_ford (nodes : list [Node ], edges : list [edge ]) -> dict [Node , float ]:
2121 """
2222 Bellman-Ford relaxation to compute potentials h[v] for all vertices.
2323 Raises ValueError if a negative weight cycle exists.
@@ -34,7 +34,6 @@ def _bellman_ford(nodes: list[Node], edges: list[Edge]) -> dict[Node, float]:
3434 if not updated :
3535 break
3636 else :
37- # One more iteration to check for negative cycles
3837 for u , v , w in edges :
3938 if dist [u ] + w < dist [v ]:
4039 raise ValueError ("Negative weight cycle detected" )
@@ -44,8 +43,8 @@ def _bellman_ford(nodes: list[Node], edges: list[Edge]) -> dict[Node, float]:
4443def _dijkstra (
4544 start : Node ,
4645 nodes : list [Node ],
47- graph : Adjacency ,
48- h : dict [Node , float ],
46+ graph : adjacency ,
47+ potentials : dict [Node , float ],
4948) -> dict [Node , float ]:
5049 """
5150 Dijkstra over reweighted graph, using potentials h to make weights non-negative.
@@ -61,7 +60,7 @@ def _dijkstra(
6160 if d_u > dist [u ]:
6261 continue
6362 for v , w in graph .get (u , []):
64- w_prime = w + h [u ] - h [v ]
63+ w_prime = w + potentials [u ] - potentials [v ]
6564 if w_prime < 0 :
6665 raise ValueError (
6766 "Negative edge weight after reweighting: numeric error"
@@ -73,7 +72,7 @@ def _dijkstra(
7372 return dist
7473
7574
76- def johnson (graph : Adjacency ) -> dict [Node , dict [Node , float ]]:
75+ def johnson (graph : adjacency ) -> dict [Node , dict [Node , float ]]:
7776 """
7877 Compute all-pairs shortest paths using Johnson's algorithm.
7978
@@ -98,17 +97,17 @@ def johnson(graph: Adjacency) -> dict[Node, dict[Node, float]]:
9897 2.0
9998 """
10099 nodes , edges = _collect_nodes_and_edges (graph )
101- h = _bellman_ford (nodes , edges )
100+ potentials = _bellman_ford (nodes , edges )
102101
103102 all_pairs : dict [Node , dict [Node , float ]] = {}
104103 inf = float ("inf" )
105104 for s in nodes :
106- dist_reweighted = _dijkstra (s , nodes , graph , h )
105+ dist_reweighted = _dijkstra (s , nodes , graph , potentials )
107106 dists_orig : dict [Node , float ] = {}
108107 for v in nodes :
109108 d_prime = dist_reweighted [v ]
110109 if d_prime < inf :
111- dists_orig [v ] = d_prime - h [s ] + h [v ]
110+ dists_orig [v ] = d_prime - potentials [s ] + potentials [v ]
112111 else :
113112 dists_orig [v ] = inf
114113 all_pairs [s ] = dists_orig
0 commit comments