On this page
Suppose four towns are joined by one-way roads of different lengths, and you want the shortest distance from every town to every other. Floyd–Warshall fills a 4 × 4 table by repeating one line of logic. Below we build that table by hand, read the routes back out, and answer the simpler question "can I get there at all?"
What does the Floyd–Warshall algorithm do?
Floyd–Warshall computes the shortest path length between all pairs of vertices of a weighted directed graph, and its time complexity is O(n³) for n vertices (cp-algorithms). The graph may have negative edge weights but no negative-weight cycle, according to the same source. It stores one n × n distance table, so its space is Θ(n²) (Wikipedia).
Robert W. Floyd published it as "Algorithm 97: Shortest path" in Communications of the ACM in June 1962 (DOI record). Stephen Warshall's "A theorem on Boolean matrices" appeared in the Journal of the ACM in January 1962 (DOI record). Bernard Roy had published essentially the same algorithm in 1959, but it went unnoticed (cp-algorithms).
How does the recurrence d[i][j] = min(d[i][j], d[i][k] + d[k][j]) work?
Number the vertices 1 to n. Start with d[i][j] equal to the edge weight from i to j, 0 on the diagonal and ∞ where there is no edge. Then take k = 1, 2, …, n in turn. In phase k, for every pair (i, j), ask: is it shorter to go from i to k, then from k to j?
The invariant: after phase k, d[i][j] is the shortest i-to-j distance using only vertices 1 to k as stops in between. Each phase allows one more vertex as a stop, so after phase n every vertex is allowed and the table is final.
The k loop must be the outermost of the three nested loops; other orders can give wrong answers (Wikipedia). Three loops of n steps each is where the n³ comes from: 64 checks for our 4 vertices.
A worked 4-vertex example, k = 1 to 4
The edges are 1→2 (3), 1→4 (7), 2→1 (8), 2→3 (2), 3→1 (5), 3→4 (1) and 4→1 (2). Rows are "from", columns are "to".
Starting table D0:
Row 1: 0, 3, ∞, 7
Row 2: 8, 0, 2, ∞
Row 3: 5, ∞, 0, 1
Row 4: 2, ∞, ∞, 0
Phase by phase, only the entries that improve:
k = 1 (via vertex 1): d[2][4] = 8 + 7 = 15; d[3][2] = 5 + 3 = 8; d[4][2] = 2 + 3 = 5.
k = 2 (via vertex 2): d[1][3] = 3 + 2 = 5; d[4][3] = 5 + 2 = 7.
k = 3 (via vertex 3): d[1][4] = 5 + 1 = 6; d[2][1] = 2 + 5 = 7; d[2][4] = 2 + 1 = 3.
k = 4 (via vertex 4): d[2][1] = 3 + 2 = 5; d[3][1] = 1 + 2 = 3; d[3][2] = 1 + 5 = 6.
Final table D4:
Row 1: 0, 3, 5, 6
Row 2: 5, 0, 2, 3
Row 3: 3, 6, 0, 1
Row 4: 2, 5, 7, 0
Notice d[2][1] changed twice, from 8 to 7 to 5: a later phase can still beat an early improvement.
How do you reconstruct the shortest path?
The distance table gives lengths, not routes, so keep a second table: next[i][j], the first vertex after i on the best route. Set next[i][j] = j for every edge, and whenever phase k improves d[i][j], set next[i][j] = next[i][k].
In our example, next[2][1] becomes 3 in phase 3, next[3][1] becomes 4 in phase 4, and next[4][1] stays 1. Following them from 2 gives 2 → 3 → 4 → 1, of length 2 + 1 + 2 = 5.
If any diagonal entry d[v][v] ends up negative, the graph has a negative cycle (cp-algorithms).
Why is transitive closure a special case?
To ask only whether j is reachable from i, use a true/false table, replace min with OR and + with AND, as in Warshall's Boolean version (Wikipedia). The rule becomes t[i][j] = t[i][j] OR (t[i][k] AND t[k][j]).
In our graph every ∞ in D0 became a finite number by the end, so the closure is all true: every town reaches every other. Compare this with counting walks using adjacency matrix powers, and with Menger's theorem on graph connectivity.
A checklist before you trust your table
Diagonal set to 0 and missing edges to ∞ before phase 1.
k in the outer loop, i and j inside.
In code, guard ∞ + w so a large "infinity" does not overflow.
After the last phase, scan the diagonal for negative values.
For practice, run the table on the state graph from solving river crossing puzzles with a graph. If the invariant still feels slippery, the Fermi Concept Library covers concepts across mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.



