Skip to main content

Module all_pairs

Module all_pairs 

Source
Expand description

All-pairs shortest paths by the Floyd-Warshall algorithm and by Johnson’s algorithm.

See the book entry Shortest paths.

Structs§

AllPairsShortestPaths
The all-pairs distance matrix and the successor matrix for path reconstruction.

Functions§

floyd_warshall
Floyd-Warshall, O(V³) time and O(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 potentials h with w(u, v) + h(u) − h(v) ≥ 0; Dijkstra from every vertex on these reweighted edges, which keeps shortest paths shortest, then gives the distances d'(u, v) − h(u) + h(v). Errors on a negative cycle (any negative edge of an undirected graph is one).