All-Pairs Shortest Paths

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

ALL-PAIRS SHORTEST

PATHS
PROBLEM
 Let G=(V,E) be a directed graph with n vertices.
 Let cost be a cost adjacency matrix for G such
that cost(i,i) = 0, 1≤i ≤n.
 cost(i,j) is the length (or cost) of edge ‹i,j›  E(G)
and cost(i,j)=∞ if i≠j and ‹i,j›  E.
 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.
Cont…
 cost(i,j)  0, for every edge ‹i,j› , we only require
that G have no cycles with negative length.
 If we allow G to contain a cycle of negative
length , the shortest path between any two
vertices on this cycle has length -∞.
 Let us examine a shortest path i to j path in G, i≠j.
 This path originate at vertex i and goes through
some intermediate vertices and terminates at j.
Cont…
 If k is an intermediate vertex on this shortest
path, then the subpaths from I to k must be
shortest path from I to k and k to j.
 If k is the intermediate vertex with highest
index, then i to k path is a shortest i to k path in
G going through no vertex with index greater
than k-1.
 We need to find two shortest paths, one from i
to k and other from k to j.
Cont…
 Using Ak(i,j) to represent the length of a
shortest path from i to j going through no
vertex index greater than k, we obtain
 Ak(i,j) = min{Ak-1(i,j), Ak-1(i,k)+Ak-1(k,j)}, k1
 Clearly, A0(i,j) = cost(i,j), 1 ≤ i ≤ n, 1 ≤ j ≤n
Algorithm
Algorithm AllPaths(cost, A, 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]}
}
 K=1, i=1, j=1
A[1,1]= min(A[1,1], A[1,1]+A[1,1]) = min(0, 0+0) = 0
 K=1, i=1, j=2
A[1,2]= min(A[1,2], A[1,1]+A[1,2]) = min(4, 0+4) = 4
 K=1, i=1, j=3
A[1,3]= min(A[1,3], A[1,1]+A[1,3]) = min(11, 0+11) = 11
 K=1, i=2, j=1
A[2,1]= min(A[2,1], A[2,1]+A[1,1]) = min(6, 6+0) = 6
 K=1, i=2, j=2
A[2,2]= min(A[2,2], A[2,1]+A[1,2]) = min(0, 6+4) = 0
 K=1, i=2, j=3
A[2,3]= min(A[2,3], A[2,1]+A[1,3]) = min(2, 6+11) = 2
 K=1, i=3, j=1
A[3,1]= min(A[3,1], A[3,1]+A[1,1]) = min(3, 3+0) = 3
 K=1, i=3, j=2
A[3,2]= min(A[3,2], A[3,1]+A[1,2]) = min(∞, 3+4) = 7
 K=1, i=3, j=3
A[3,3]= min(A[3,3], A[3,1]+A[1,3]) = min(0, 3+11) = 0
 K=2, i=1, j=1
A[1,1]= min(A[1,1], A[1,2]+A[2,1]) = min(0, 4+6) = 0
 K=2, i=1, j=2

A[1,2]= min(A[1,2], A[1,2]+A[2,2]) = min(4, 4+0) = 4


 K=2, i=1, j=3

A[1,3]= min(A[1,3], A[1,2]+A[2,3]) = min(11, 4+2) = 6


 K=2, i=2, j=1

A[2,1]= min(A[2,1], A[2,2]+A[2,1]) = min(6, 0+6) = 6


 K=2, i=2, j=2

A[2,2]= min(A[2,2], A[2,2]+A[2,2]) = min(0, 0+0) = 0


 K=2, i=2, j=3

A[2,3]= min(A[2,3], A[2,2]+A[2,3]) = min(2, 0+2) = 2


 K=2, i=3, j=1

A[3,1]= min(A[3,1], A[3,2]+A[2,1]) = min(3, 7+6) = 3


 K=2, i=3, j=2

A[3,2]= min(A[3,2], A[3,2]+A[2,2]) = min(7, 7+0) = 7


 K=2, i=3, j=3

A[3,3]= min(A[3,3], A[3,2]+A[2,3]) = min(0, 7+2) = 0


 K=3, i=1, j=1
A[1,1]= min(A[1,1], A[1,3]+A[3,1]) = min(0, 6+3) = 0
 K=3, i=1, j=2

A[1,2]= min(A[1,2], A[1,3]+A[3,2]) = min(4, 6+7) = 4


 K=3, i=1, j=3

A[1,3]= min(A[1,3], A[1,3]+A[3,3]) = min(6, 6+0) = 6


 K=3, i=2, j=1

A[2,1]= min(A[2,1], A[2,3]+A[3,1]) = min(6, 2+3) = 5


 K=3, i=2, j=2

A[2,2]= min(A[2,2], A[2,3]+A[3,2]) = min(0, 2+7) = 0


 K=3, i=2, j=3

A[2,3]= min(A[2,3], A[2,3]+A[3,3]) = min(2, 2+0) = 2


 K=3, i=3, j=1

A[3,1]= min(A[3,1], A[3,3]+A[3,1]) = min(3, 0+3) = 3


 K=3, i=3, j=2

A[3,2]= min(A[3,2], A[3,3]+A[3,2]) = min(7, 0+7) = 7


 K=3, i=3, j=3

A[3,3]= min(A[3,3], A[3,3]+A[3,3]) = min(0, 0+0) = 0

You might also like