📚 DAA Tutorial

Design and Analysis of Algorithms

Learn Design and Analysis of Algorithms concepts with simple explanations, algorithms, examples and practical notes.

← Back to DAA

All Pairs Shortest Path Problem

Let G = (V, E) be a directed graph with n vertices. Let cost be the cost adjacency matrix for G, such that cost(i,i)=0, 1 ≤ i ≤ n.

  • cost(i,j) is the length (cost) of the edge ⟨i, j⟩ if (i, j) ∈ E(G).
  • cost(i,j)=∞ if i ≠ j and ⟨i, j⟩ ∉ E(G).

The all pairs shortest path problem is to determine a matrix A such that A(i,j) is the length of a shortest path from i to j. For every edge ⟨i, j⟩ we have C(i,j)≥0, and we require G to have no cycles with negative length.

A shortest i to j path in G, i ≠ j, originates at i, goes through some intermediate vertices and terminates at j. If k is some intermediate vertex, then the paths from i to k and from k to j must also be shortest paths.

Dynamic programming approach

If k is an intermediate vertex on a shortest i to j path with the highest index, then the i to k path is a shortest i to k path in G going through no vertex with index greater than k − 1. Similarly, the k to j path is a shortest k to j path in G going through no vertex of index greater than k − 1.

Let Ak(i,j) represent the length of a shortest path from i to j going through no vertex of index greater than k. Then:

Ak(i,j)=min{min1≤k≤n{Ak−1(i,k)+Ak−1(k,j)},cost(i,j)}

The recurrence used by the algorithm is:

A0(i,j)=cost(i,j)1≤i≤n,1≤j≤n Ak(i,j)=min{Ak−1(i,j),Ak−1(i,k)+Ak−1(k,j)}k≥1

Why negative cycles are not allowed

123 1−21
Example graph with a negative cycle (1, 2, 1)
Adjacency matrix A0=[ 01∞ −201 ∞∞0 ]

The recurrence would give

A2(1,3)≠min{A1(1,3),A1(1,2)+A1(2,3)}=2

Instead we see that

A2(1,3)=−∞

because the path 1, 2, 1, 2, 1, 2, …, 1, 2, 3 can repeat the cycle (1, 2, 1), which has a length of −1.

Computing An

An can be computed by first computing A1, A2, then A3 and so on. Since there is no vertex in G with index greater than n, A(i,j)=An(i,j). The function AllPaths computes An(i,j).

Algorithm AllPaths(cost, A, n)

Algorithm AllPaths(cost, A, n)
/* cost[1:n, 1:n] is the cost adjacency matrix of a graph with n vertices.
   A[i,j] is the cost of a shortest path from vertex i to vertex j.
   cost[i,i] = 0.0, for 1 ≤ i ≤ n. */
{
    for i := 1 to n do
        for j := 1 to n do
            A[i,j] := cost[i,j];
    for k := 1 to n do
        for i := 1 to n do
            for j := 1 to n do
                A[i,j] := min(A[i,j], A[i,k] + A[k,j]);
}

Time complexity: Θ(n3), since there are three nested loops of n iterations each.

Example

123 461132
Edges: 1→2 (4), 2→1 (6), 1→3 (11), 3→1 (3), 2→3 (2)
A0=[04116023∞0]
A1=[0411602370]
A2=[046602370]
A3=[046502370]

Computing A1 (using vertex 1 as the intermediate vertex)

A1(1,2)=min{A0(1,2),A0(1,1)+A0(1,2)}=min{4,0+4}=4A1(1,3)=min{A0(1,3),A0(1,1)+A0(1,3)}=min{11,0+11}=11A1(2,1)=min{A0(2,1),A0(2,1)+A0(1,1)}=min{6,6+0}=6A1(2,3)=min{A0(2,3),A0(2,1)+A0(1,3)}=min{2,6+11}=2A1(3,1)=min{A0(3,1),A0(3,1)+A0(1,1)}=min{3,3+0}=3A1(3,2)=min{A0(3,2),A0(3,1)+A0(1,2)}=min{∞,3+4}=7

Computing A2 (using vertex 2 as the intermediate vertex)

A2(1,2)=min{A1(1,2),A1(1,2)+A1(2,2)}=min{4,4+0}=4A2(1,3)=min{A1(1,3),A1(1,2)+A1(2,3)}=min{11,4+2}=6A2(2,1)=min{A1(2,1),A1(2,2)+A1(2,1)}=min{6,0+6}=6A2(2,3)=min{A1(2,3),A1(2,2)+A1(2,3)}=min{2,0+2}=2A2(3,1)=min{A1(3,1),A1(3,2)+A1(2,1)}=min{3,7+6}=3A2(3,2)=min{A1(3,2),A1(3,2)+A1(2,2)}=min{7,7+0}=7

Computing A3 (using vertex 3 as the intermediate vertex)

A3(1,2)=min{A2(1,2),A2(1,3)+A2(3,2)}=min{4,6+7}=4A3(1,3)=min{A2(1,3),A2(1,3)+A2(3,3)}=min{6,6+0}=6A3(2,1)=min{A2(2,1),A2(2,3)+A2(3,1)}=min{6,2+3}=5A3(2,3)=min{A2(2,3),A2(2,3)+A2(3,3)}=min{2,2+0}=2A3(3,1)=min{A2(3,1),A2(3,3)+A2(3,1)}=min{3,0+3}=3A3(3,2)=min{A2(3,2),A2(3,3)+A2(3,2)}=min{7,0+7}=7

A3 is the final matrix: it gives the shortest distance between every pair of vertices. For example, the shortest path from vertex 2 to vertex 1 has length 5, found through vertex 3 (2 + 3).