ENع
←Floyd-Warshall
Lesson 37 · Shortest paths and spanning trees

All-pairs shortest paths with Floyd-Warshall

14 min10 steps5 questionsAdvanced
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 37 · Interactive
01 / 10

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  2. 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 False
  3. One 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 cost
  4. Round 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)
  5. 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)
  6. 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)
  7. 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)
  8. 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]
  9. 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 last
  10. Cost: 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

Quiz Unlocks after the last step.