All-pairs shortest paths: Floyd-Warshall
◈ 4 cardsA dynamic program over which vertices are allowed as intermediates — Θ(V³), handles negative edges, detects negative cycles on the diagonal.
All-pairs shortest paths
Sometimes you need for every pair of vertices, not just from one source — an distance matrix . You could run a single-source algorithm times, but Floyd-Warshall solves it directly in for arbitrary (including negative) edge weights, with a clean DP.
The DP over intermediate vertices
The insight is to parameterize by which vertices are allowed in the middle of a path. Define as the weight of a shortest path from to whose intermediate vertices all come from . Now consider a shortest such path: either vertex is not an intermediate (so the path is already optimal using ), or it is — and then it splits into and , each using only as intermediates. That gives the two-case recurrence: The base is the direct edge weight ( on the diagonal, if no edge). The final matrix holds the true shortest distances .
Running time, space, and negative cycles
Three nested loops — outermost, then , then — give , with only space if you overwrite the matrix in place. The algorithm handles negative edges, and a negative-weight cycle reveals itself as a negative diagonal entry (a path from a vertex back to itself that costs less than ). For sparse graphs, Johnson's algorithm reweights via Bellman-Ford and runs Dijkstras for — faster than when is small.
Worked example
On the CLRS Fig 23.1 graph (5 vertices, several negative edges, no negative cycle) Floyd-Warshall produces the full distance matrix shown in the exercise below. Notice the matrix is not symmetric — the graph is directed.