MCS-208 · Unit 8

Graphs

Section 5 of 8

Block 3 · Unit 8Graphs9 min · Section 5 of 8

8.5All-Pairs Shortest Paths: Floyd–Warshall

Covers IGNOU MCS-208 §8.3.4

A road atlas often has a table of distances between every pair of cities. Computing such a table is the all-pairs shortest-path problem. The Floyd–Warshall algorithm solves it with three nested loops.

The idea

Number the vertices 1,…,n1, \dots, n. Let dij(k)d_{ij}^{(k)} be the length of the shortest path from ii to jj whose intermediate vertices all lie in {1,…,k}\{1, \dots, k\}. A shortest such path either does not use kk (so it is the best path with intermediates in {1,…,k−1}\{1, \dots, k-1\}), or uses kk exactly once (so it is a best path from ii to kk followed by a best path from kk to jj):

dij(k)=min⁡(dij(k−1),  dik(k−1)+dkj(k−1)),dij(0)=Cij.d_{ij}^{(k)} = \min\left(d_{ij}^{(k-1)},\; d_{ik}^{(k-1)} + d_{kj}^{(k-1)}\right), \qquad d_{ij}^{(0)} = C_{ij}.

CC is the cost matrix: Cii=0C_{ii} = 0, CijC_{ij} = weight of edge (i,j)(i, j), and ∞ if there is no edge. This is dynamic programming: each table is built from the previous one. The same matrix can be updated in place, giving:

Floyd-Warshall
D = C
for k = 1 to n
    for i = 1 to n
        for j = 1 to n
            if D[i][k] + D[k][j] < D[i][j] then
                D[i][j] = D[i][k] + D[k][j];  P[i][j] = k

P[i][j] remembers the intermediate vertex, so paths can be rebuilt. Running time: O(n3)O(n^3); space O(n2)O(n^2). It works with negative edges as long as there is no negative cycle.

Floyd–Warshall versus repeated Dijkstra

Floyd–WarshallDijkstra from every vertex
TimeO(n3)O(n^3)O(n3)O(n^3) with arrays; O(n(n+e)log⁡n)O(n(n + e)\log n) with heaps
Negative edgesAllowed (no negative cycles)Not allowed
CodeThree loopsMore complex
Best forDense or small graphsLarge sparse graphs

Key takeaways

  • Recurrence: dij(k)=min⁡(dij(k−1),dik(k−1)+dkj(k−1))d_{ij}^{(k)} = \min(d_{ij}^{(k-1)}, d_{ik}^{(k-1)} + d_{kj}^{(k-1)}).
  • Three nested loops with kk outermost: O(n3)O(n^3) time, O(n2)O(n^2) space.
  • Diagonal 0, missing edges ∞; guard against ∞ + ∞ overflow.
  • Works with negative edges if there is no negative cycle; PP rebuilds the paths.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the time complexity of the Floyd–Warshall algorithm?

Show answer

Answer: C. O(n3)O(n^3)

Three nested loops of nn iterations each. O(ne)O(ne) is Bellman–Ford.

Question 2 · multiple choice

Which loop must be the outermost in Floyd–Warshall?

Show answer

Answer: C. The loop over kk (allowed intermediate vertex)

The recurrence builds D(k)D^{(k)} from D(k−1)D^{(k-1)}; all pairs must be updated for one kk before the next kk is allowed.

Question 3 · multiple choice

With D[i][k]=4D[i][k] = 4, D[k][j]=3D[k][j] = 3 and D[i][j]=9D[i][j] = 9, what is D[i][j]D[i][j] after processing kk?

Show answer

Answer: D. 7

4+3=7<94 + 3 = 7 < 9, so the path through kk is better.

Question 4 · multiple choice

What should D[i][i]D[i][i] be at the start?

Show answer

Answer: A. 0

The cost from a vertex to itself is 0 (if there are no negative cycles).

Question 5 · multiple choice

What does this program print?

C · q8-floyd.c
#include <stdio.h>

#define INF 1000

int main(void)
{
    int D[3][3] = {{0, 8, 2},
                   {INF, 0, INF},
                   {INF, 3, 0}};
    for (int k = 0; k < 3; k++)
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (D[i][k] + D[k][j] < D[i][j])
                    D[i][j] = D[i][k] + D[k][j];
    printf("%d %d\n", D[0][1], D[2][0]);
    return 0;
}
Show answer

Answer: B. 5 1000

0→2→1 costs 2+3=5<82 + 3 = 5 < 8. There is no path from 2 to 0, so D[2][0]D[2][0] stays INF (1000). (INF is small here, so INF + INF does not overflow.)

Question 6 · short answer

Write the Floyd–Warshall algorithm and state its time complexity and the condition under which it works.

Show worked answer

Input: cost matrix CC with C[i][i]=0C[i][i] = 0, C[i][j]C[i][j] = weight of edge (i,j)(i, j), ∞ if no edge. Algorithm: set D=CD = C and P[i][j]=−1P[i][j] = -1; for k=1k = 1 to nn: for i=1i = 1 to nn: for j=1j = 1 to nn: if D[i][k]+D[k][j]<D[i][j]D[i][k] + D[k][j] < D[i][j] then D[i][j]=D[i][k]+D[k][j]D[i][j] = D[i][k] + D[k][j] and P[i][j]=kP[i][j] = k. At the end D[i][j]D[i][j] is the shortest distance from ii to jj, and PP gives the intermediate vertices. Time O(n3)O(n^3), space O(n2)O(n^2). It works with negative edge weights provided the graph has no negative-weight cycle.

↑ ↓ move↵ openTab switch type