|
Bellman-Ford relaxation to compute potentials h[v] for all vertices. |
|
|
|
Dijkstra over reweighted graph, using potentials h to make weights non-negative. |
|
Compute all-pairs shortest paths using Johnson's algorithm. |
Bellman-Ford relaxation to compute potentials h[v] for all vertices. Raises ValueError if a negative weight cycle exists.
Dijkstra over reweighted graph, using potentials h to make weights non-negative. Returns distances from start in the reweighted space.
Compute all-pairs shortest paths using Johnson’s algorithm.
graph: adjacency list {u: [(v, weight), …], …}
dict of dicts: dist[u][v] = shortest distance from u to v
ValueError: if a negative weight cycle is detected
Example: >>> g = { … 0: [(1, 3), (2, 8), (4, -4)], … 1: [(3, 1), (4, 7)], … 2: [(1, 4)], … 3: [(0, 2), (2, -5)], … 4: [(3, 6)], … } >>> round(johnson(g)[0][3], 2) 2.0