All-pairs shortest paths with Floyd-Warshall
About this lesson
Dijkstra and Bellman-Ford find the cheapest routes from one start vertex. Floyd-Warshall finds them from every vertex to every other, all at once, with one table and three loops. Watch it run on a 4-vertex graph: in each round one middle vertex k may join a route, and a cell improves only when d[i][k] + d[k][j] beats it. The lessons "Dijkstra's algorithm" and "Bellman-Ford and negative edges" cover the single-source versions this builds on.
Every pair, cheapest route
A directed graph with weights: 4 vertices and 6 one-way edges, each with a cost. We want the cheapest route from every vertex to every other: 4 × 4 = 16 answers. Floyd-Warshall finds them all with one table.
vertices 0..3, edges (from → to: cost): 0 → 1: 3 0 → 2: 8 1 → 2: 2 1 → 3: 7 2 → 3: 1 3 → 0: 2 a route's cost is the sum of its edge costs goal: the cheapest cost for all 16 ordered pairs (i, j)
Read the lesson step by step
Every pair, cheapest route
A directed graph with weights: 4 vertices and 6 one-way edges, each with a cost. We want the cheapest route from every vertex to every other: 4 × 4 = 16 answers. Floyd-Warshall finds them all with one table.
vertices 0..3, edges (from → to: cost): 0 → 1: 3 0 → 2: 8 1 → 2: 2 1 → 3: 7 2 → 3: 1 3 → 0: 2 a route's cost is the sum of its edge costs goal: the cheapest cost for all 16 ordered pairs (i, j)Start from the direct edges
The table d starts with what we know already. d[i][j] is the cost of the direct edge from i to j, 0 on the diagonal, and ∞ (Python's float("inf")) where there is no edge yet.
d before any round: j=0 j=1 j=2 j=3 i=0 0 3 8 ∞ i=1 ∞ 0 2 7 i=2 ∞ ∞ 0 1 i=3 2 ∞ ∞ 0 D = [[0, 3, 8, INF], [INF, 0, 2, 7], [INF, INF, 0, 1], [2, INF, INF, 0]] INF + 5 is still INF, and INF < INF is FalseOne rule: go via k
One rule does all the work. For a middle vertex k, if going i → k → j costs less than d[i][j], write the cheaper cost in. Try it for every pair i, j, first with k = 0, then 1, 2 and 3.
1 INF = float("inf") 2 def floyd_warshall(d): 3 n = len(d) 4 for k in range(n): 5 for i in range(n): 6 for j in range(n): 7 if d[i][k] + d[k][j] < d[i][j]: 8 d[i][j] = d[i][k] + d[k][j] 9 return d line 4: one round per middle vertex k line 7: is i → k → j cheaper than the best so far? line 8: if so, keep the cheaper costRound k = 0
Round k = 0: may a route pass through 0? Only 3 has an edge into 0. 3 → 0 → 1 costs 2 + 3 = 5, beating ∞, and 3 → 0 → 2 costs 2 + 8 = 10. Two cells improve.
round k = 0: for every i, j try i → 0 → j 16 checks, 2 improve d[3][1]: 3 → 0 → 1 2 + 3 = 5 < ∞ d[3][2]: 3 → 0 → 2 2 + 8 = 10 < ∞ j=0 j=1 j=2 j=3 i=0 0 3 8 ∞ i=1 ∞ 0 2 7 i=2 ∞ ∞ 0 1 i=3 2 *5 *10 0 checks so far: 16 (* = improved this round)Round k = 1
Round k = 1. 0 → 1 → 2 costs 3 + 2 = 5, beating 8. 0 → 1 → 3 costs 3 + 7 = 10. 3 → 1 → 2 costs 5 + 2 = 7, reusing round 0's 5.
round k = 1: for every i, j try i → 1 → j 16 checks, 3 improve d[0][2]: 0 → 1 → 2 3 + 2 = 5 < 8 d[0][3]: 0 → 1 → 3 3 + 7 = 10 < ∞ d[3][2]: 3 → 0 → 1 → 2 5 + 2 = 7 < 10 j=0 j=1 j=2 j=3 i=0 0 3 *5 *10 i=1 ∞ 0 2 7 i=2 ∞ ∞ 0 1 i=3 2 5 *7 0 checks so far: 32 (* = improved this round)Round k = 2
Round k = 2. 0 → 2 → 3 costs 5 + 1 = 6, beating 10. 1 → 2 → 3 costs 2 + 1 = 3, beating 7. Each sum reads row 2 and column 2: the lit cross.
round k = 2: for every i, j try i → 2 → j 16 checks, 2 improve d[0][3]: 0 → 1 → 2 → 3 5 + 1 = 6 < 10 d[1][3]: 1 → 2 → 3 2 + 1 = 3 < 7 j=0 j=1 j=2 j=3 i=0 0 3 5 *6 i=1 ∞ 0 2 *3 i=2 ∞ ∞ 0 1 i=3 2 5 7 0 checks so far: 48 (* = improved this round)Round k = 3
Round k = 3, the last. 1 → 3 → 0 costs 3 + 2 = 5, 2 → 3 → 0 costs 1 + 2 = 3, and 2 → 3 → 1 costs 1 + 5 = 6. No ∞ is left.
round k = 3: for every i, j try i → 3 → j 16 checks, 3 improve d[1][0]: 1 → 2 → 3 → 0 3 + 2 = 5 < ∞ d[2][0]: 2 → 3 → 0 1 + 2 = 3 < ∞ d[2][1]: 2 → 3 → 0 → 1 1 + 5 = 6 < ∞ j=0 j=1 j=2 j=3 i=0 0 3 5 6 i=1 *5 0 2 3 i=2 *3 *6 0 1 i=3 2 5 7 0 checks so far: 64 (* = improved this round)Read any answer
Done: each cell is now a shortest distance. d[2][1] = 6 is the route 2 → 3 → 0 → 1, costing 1 + 2 + 3. The table keeps costs only; to rebuild routes, keep a second table of next stops.
final d (every shortest distance): j=0 j=1 j=2 j=3 i=0 0 3 5 6 i=1 5 0 2 3 i=2 3 6 0 1 i=3 2 5 7 0 d[2][1] = 6: 2 → 3 → 0 → 1 = 1 + 2 + 3 routes: store next[i][j] and update it with d[i][j]Why it is right
Why does it work? After round k, d[i][j] is the cheapest route whose middle stops all come from 0 to k. A best route through k is a best i → k part plus a best k → j part, both already in the table.
invariant after round k: d[i][j] = cheapest i → j route whose middle stops are all in {0, .., k} round k: such a route either skips k (old d[i][j]) or visits k once: (i → k) + (k → j), each with stops < k after round n - 1 every stop is allowed: d holds the true distances this is dynamic programming: each round builds on the lastCost: n³ checks
Three nested loops of n, so n³ checks: 4³ = 64 here. That is Θ(n³) time and one n × n table, Θ(n²) memory. Negative edges are fine; a negative cycle shows up as a negative number on the diagonal.
checks = n · n · n = 4³ = 64 time Θ(n³), memory Θ(n²) (the table, updated in place) n = 100 → 1,000,000 checks n = 1,000 → 1,000,000,000 checks negative cycle: some d[i][i] < 0 at the end
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the lesson's graph, what is d[0][2] after round k = 1?
It starts at 8, the direct edge 0 → 2. In round k = 1 the route 0 → 1 → 2 costs 3 + 2 = 5, which beats 8, so d[0][2] becomes 5.
02In round k, which cells does the update d[i][k] + d[k][j] read from?
d[i][k] is in column k and d[k][j] is in row k. That is the lit cross in the figure. Row k and column k do not change during round k, so updating the table in place is safe.
03What does d[i][j] hold after round k finishes?
That is the invariant. It counts which vertices may be middle stops, not how many edges are used. Only after the last round, when every vertex is allowed, is it the true distance.
04How many times does the comparison on line 7 run for a graph with 100 vertices?
Three nested loops of n = 100 each: 100 × 100 × 100 = 1,000,000 checks. That is Θ(n³) time.
05After Floyd-Warshall finishes, d[2][2] is -3. What does that tell you?
A route from 2 back to itself that costs less than 0 is a negative cycle. Going round it again makes any route through it cheaper still, so there is no shortest path. Sedgewick and Wayne's FloydWarshall.java detects it this way.
Glossary
- All-pairs shortest paths
- The problem of finding the cheapest route cost between every ordered pair of vertices (i, j), not just from one start vertex.
- Distance table d
- An n × n table where d[i][j] holds the cheapest known cost from vertex i to vertex j.
- Middle stop
- A vertex a route passes through on its way, not counting where it starts or ends. Round k allows vertex k as a middle stop.
- Relaxation
- Checking whether a route through a vertex is cheaper than the best known cost, and keeping the cheaper one. Line 7 and 8 of the code.
- Invariant
- A fact that stays true after every round. Here: after round k, d[i][j] is the cheapest route whose middle stops come from 0 to k.
- Dynamic programming
- Solving a problem by building answers to bigger subproblems out of answers to smaller ones stored in a table.
- Negative cycle
- A loop of edges whose costs add up to less than 0. Going round it again always lowers the cost, so shortest paths stop existing.
Notes and sources
- Floyd-Warshall is dynamic programming (MIT 6.006, Lecture 17): the subproblem is the cheapest route from i to j using only middle stops from {0..k}, and round k needs only round k - 1. Updating one table in place is safe because round k never changes row k or column k (d[k][k] = 0).
- For non-negative weights you can instead run Dijkstra from every vertex: |V| · O(|V| log |V| + |E|) time (MIT 6.006, Lecture 14; see the lesson "Dijkstra's algorithm"). On dense graphs that is no better than Floyd-Warshall's Θ(V³), and Floyd-Warshall is three loops.
- Negative edge costs are allowed. If the graph has a negative cycle, shortest paths are not defined; Floyd-Warshall then leaves some d[i][i] below 0, which is how Sedgewick and Wayne's FloydWarshall.java detects it.
- The code is real Python 3. floyd_warshall changes the list it is given and returns it. float("inf") is the same as math.inf, and INF + 5 is still INF, so a missing edge never makes a route look cheap.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 17: Dynamic Programming, Part 3 (Floyd-Warshall)
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 14: Johnson's Algorithm (APSP by repeated Dijkstra)
- Sedgewick and Wayne, Algorithms 4th ed., §4.4 Shortest Paths
- Sedgewick and Wayne, FloydWarshall.java
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), ch. 23 All-Pairs Shortest Paths
- Python documentation: math.inf
- Python documentation: float()
Quiz Unlocks after the last step.