Module 23 of 39

All-pairs shortest paths: Floyd–Warshall and Johnson

Compare repeated single-source searches, Floyd–Warshall, and Johnson's method for all-pairs shortest paths.

Finding routes from one depot is a one-source problem. A map service may instead need the best route between every pair of places. The output alone contains up to V² answers, so we should expect to store or produce much more information than for one starting point.

Background for this page. You need weighted paths and a table indexed by two vertices. Entry (i,j) stores a distance from i to j. A negative cycle can make a finite shortest-walk answer impossible. Related explanations: Single-source shortest paths: Dijkstra and Bellman–Ford · Matrices: dimensions, products, and transposes.

One option is simple: run a suitable one-source method once from each vertex. If all weights are nonnegative and the graph is sparse, repeated Dijkstra searches can be useful. A different method, Floyd–Warshall, builds a table of distances and considers possible intermediate vertices one at a time.

All-pairs search can consider intermediate vertices one at a time.

Suppose dist[i][j] is the best known route from i to j. When we allow vertex k as another possible intermediate stop, a route from i to j either stays as it was or goes from i to k and then from k to j:

dist[i][j] ← min(dist[i][j], dist[i][k] + dist[k][j]).

The order of intermediate vertices gives a dynamic-programming argument for correctness. Floyd–Warshall takes Θ(V³) time and Θ(V²) space. It can handle negative edges, but shortest paths are not well-defined for pairs affected by a negative cycle. A negative value on a diagonal after the algorithm is a way to detect such a cycle.

The textbook also develops a matrix-multiplication view of shortest paths. It replaces ordinary addition and multiplication with minimum and addition. This “min-plus” product expresses how paths with more edges are built from shorter paths. It is a useful connection between graph problems and algebra, even when Floyd–Warshall is the simpler method to implement.

Johnson’s algorithm is especially useful for sparse graphs that may contain negative edges but no negative cycle. Bellman–Ford first computes values used to reweight every edge so its new weight is nonnegative while preserving which paths are shortest. Then Dijkstra runs from each vertex. The original distances can be recovered afterward. The reweighting is the key idea; simply adding the same constant to every edge would change the relative cost of paths with different numbers of edges and would be wrong.

The loop order expresses the proof

Floyd–Warshall considers allowed intermediate vertices in stages. Before stage k, each table entry represents the best route whose intermediate vertices come from earlier stages. A route newly allowed to use k either avoids k or splits into a route from i to k and one from k to j. Taking the smaller of these costs establishes the next stage.

That is why the intermediate-vertex loop belongs outside the two endpoint loops in the standard in-place algorithm. Initialization uses zero on the diagonal, the minimum direct-edge weight where an edge exists, and infinity elsewhere. Multiple edges need the minimum; missing edges must not look like zero-cost links.

Afterward, a negative diagonal entry signals a negative cycle. Not every pair is necessarily affected by that cycle.

Practice. If dist[A][C] is 9, dist[A][B] is 3, and dist[B][C] is 4, what happens when Floyd–Warshall considers B as an intermediate vertex? Why can we not use Dijkstra directly on a graph with negative edges?

Answer. The route through B costs 7, so the estimate for A to C becomes 7. Dijkstra’s settled-distance step depends on nonnegative edges; a later negative edge could otherwise improve a distance it treated as final.

Try another case. Why can a route through an unreachable intermediate segment not improve a distance?

Reasoning. If either segment has infinite cost, it does not form a usable route. Implementations using finite integer sentinels must guard addition to avoid overflow.

View the full course and choose another topic.

Image preview

Fit