Dijkstra's algorithm gives shortest paths from one source. Floyd and Warshall's dynamic programming algorithm gives the shortest paths between all pairs at once, and allows negative edges.

The Problem

For a weighted directed graph with vertices numbered 1 to n, find the shortest distance d(i, j) from every vertex i to every vertex j. Repeating Dijkstra from each vertex costs Θ(V³) with arrays; Floyd-Warshall solves it in Θ(V³) with a simpler program, handling negative edge weights as long as there is no negative cycle.

The Idea

Let D⁽ᵏ⁾[i][j] be the length of the shortest path from i to j that uses only vertices 1..k as intermediates. D⁽⁰⁾ is the weight matrix W, with 0 on the diagonal and ∞ where there is no edge. When vertex k is allowed, either the best path does not use k, or it passes through k, which splits it into i → k and k → j using only the first k − 1 intermediates. Hence the recurrence D⁽ᵏ⁾[i][j] = min( D⁽ᵏ⁻¹⁾[i][j], D⁽ᵏ⁻¹⁾[i][k] + D⁽ᵏ⁻¹⁾[k][j] ). The final matrix D⁽ⁿ⁾ holds all shortest distances.

Algorithm and Analysis

The program has three nested loops: for k from 1 to n; for i from 1 to n; for j from 1 to n; update D[i][j] = min(D[i][j], D[i][k] + D[k][j]). The order of the loops matters: k must be the outermost. It can be done in place on one matrix. The time is Θ(n³) and the space Θ(n²). To recover the actual paths, keep a second matrix of predecessors or of the best intermediate vertex. The algorithm also finds the transitive closure (Warshall's version uses logical OR and AND instead of min and +). A negative value on the diagonal at the end reveals a negative cycle.

Definition: Floyd-Warshall algorithm. A dynamic programming algorithm that computes the shortest distances between all pairs of vertices of a weighted graph in Θ(n³) time by allowing intermediate vertices one at a time.

Table 3.3: Dijkstra versus Floyd-Warshall

Basis Dijkstra Floyd-Warshall
Answers One source to all All pairs
Negative edges Not allowed Allowed, no negative cycles
Time Θ(V²) or Θ((V + E) log V) Θ(V³)
Technique Greedy Dynamic programming

Example 3.3: Floyd-Warshall on four vertices

Problem: Directed edges: 1→2 (3), 1→4 (7), 2→1 (8), 2→3 (2), 3→1 (5), 3→4 (1), 4→1 (2). Find all-pairs shortest distances.

Solution:

  1. D⁰ rows: 1: [0, 3, ∞, 7]; 2: [8, 0, 2, ∞]; 3: [5, ∞, 0, 1]; 4: [2, ∞, ∞, 0].
  2. Allow vertex 1: 2→4 becomes 8 + 7 = 15; 3→2 becomes 5 + 3 = 8; 4→2 becomes 2 + 3 = 5.
  3. Allow vertex 2: 1→3 becomes 3 + 2 = 5; 4→3 becomes 5 + 2 = 7.
  4. Allow vertex 3: 1→4 becomes 5 + 1 = 6; 2→4 becomes 2 + 1 = 3. Allow vertex 4: 2→1 becomes 3 + 2 = 5; 3→1 becomes 1 + 2 = 3.

Result: D = [[0, 3, 5, 6], [5, 0, 2, 3], [3, 6, 0, 1], [2, 5, 7, 0]].

INF = 10**9
d = [[0, 3, INF, 7], [8, 0, 2, INF], [5, INF, 0, 1], [2, INF, INF, 0]]
n = len(d)
for k in range(n):
    for i in range(n):
        for j in range(n):
            if d[i][k] + d[k][j] < d[i][j]:
                d[i][j] = d[i][k] + d[k][j]
for row in d:
    print(row)

Output

[0, 3, 5, 6]
[5, 0, 2, 3]
[3, 6, 0, 1]
[2, 5, 7, 0]

Common mistakes

  • Placing the k loop inside the i and j loops, which gives wrong answers.
  • Using a large number such as the maximum integer for ∞ and overflowing when adding.
  • Forgetting the 0 diagonal in the initial matrix.
  • Running it on a graph with a negative cycle and trusting the result.

Key takeaways

  • D⁽ᵏ⁾[i][j] = min(D⁽ᵏ⁻¹⁾[i][j], D⁽ᵏ⁻¹⁾[i][k] + D⁽ᵏ⁻¹⁾[k][j]).
  • Three nested loops with k outermost; time Θ(n³), space Θ(n²).
  • It handles negative edges but not negative cycles.
  • Warshall's variant computes the transitive closure.

Practice questions

  1. What does D⁽ᵏ⁾[i][j] represent?
  2. Time complexity of Floyd-Warshall?
  3. Why must the k loop be outermost?

Answers to the practice questions

  1. Shortest i-to-j distance using only vertices 1..k as intermediates.
  2. Θ(n³).
  3. The recurrence adds vertices one at a time, so all pairs must be updated for k before moving to k + 1.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit III