Expand description
All-pairs shortest paths by the Floyd-Warshall algorithm and by Johnson’s algorithm.
See the book entry Shortest paths.
Structs§
- AllPairs
Shortest Paths - The all-pairs distance matrix and the successor matrix for path reconstruction.
Functions§
- floyd_
warshall - Floyd-Warshall,
O(V³)time andO(V²)memory; negative edges are allowed. Returns an error if the graph has a negative cycle (a negative diagonal entry). - johnson
- Johnson’s algorithm (SciPy’s
johnson),O(V E log V), better than Floyd-Warshall on sparse graphs and allowing negative edges. Bellman-Ford from an added vertex joined to every vertex by a zero edge gives potentialshwithw(u, v) + h(u) − h(v) ≥ 0; Dijkstra from every vertex on these reweighted edges, which keeps shortest paths shortest, then gives the distancesd'(u, v) − h(u) + h(v). Errors on a negative cycle (any negative edge of an undirected graph is one).