Chapter 26 All-Pairs Shortest Paths • Problem definition • Shortest paths and matrix multiplication • The Floyd-Warshall algorithm 1 Problem definition: • Real problem:When to make a table of distances between all pairs of cities for a road atlas,how can we compute the distances? • Model the problem: • Given a weighted,directed graph G=(V,E) with a weight function w:E R that maps edges to real-valued weights. • For every pair of vertices u,v V, find a shortest(least weight) path from u to v, where the weight of a path is the sum of the weights of its constituent edges. 2 Solve the problem by SingleSource shortest paths algorithm: • If all edge weights are nonnegative, we can use Dijkstra’s algorithm. • If negative-weight edges are allowed, then we use Bellman-Ford algorithm instead. • Can you analysis the complexity of the two solutions ? Is it possible for us to find a more efficient algorithm? 3 Preliminary knowledge: • In this chapter, we use an adjacency-matrix representation: • wij= 0 if i=j • the weight of directed edge (i,j) if i j and (i,j) E • if i j and (i,j) E • The output of the algorithm presented in this chapter is an n*n matrix D=(dij),where entry dij contains the weight of a shortest path from vertex i to vertex j. That is , if we let (i, j ) denotes the shortest-path weight from vertex i to vertex j, then dij= (i, j ) at termination. 4 Continue: • To solve the all-pairs shortest-paths problem on an input adjacency matrix, we need to compute not only the shortest path weights but also a predecessor matrix = ( ij), where ij is NIL if either i=j or there is no path from i to j, and otherwise ij is some predecessor of j on a shortest path from i. • The subgraph induced by the ith row of the matrix should be a shortest-paths tree with root i. For each vertex i V,we define the predecessor subgraph of G for i as G ,i =(V ,i,E ,i) • V ,i={j V: i,j NIL}{i} 5 • E ,i={( ij , j): j V,i and ij NIL} Shortest paths and matrix multiplication: • This section presents a dynamic-programming algorithm for the all-pairs shortest-paths problem on a directed graph G=(V,E). Each major loop of the dynamic program will invoke an operation that is very similar to matrix multiplication, so that the algorithm will look like repeated matrix multiplication. We shall start by developing a (V4)-time algorithm for the all-pairs shortest-paths problem and the improve its running time to (V3lgV). 6 Review: the steps to develop a dynamic-programming algorithm • Characterize the structure of an optimal solution. • Recursively define the value of an optimal solution. • Computing the value of an optimal solution in a bottom-up fashion. • Constructing an optimal solution from computed information. 7 The structure of a shortest path: • Consider a shortest path p from vertex i to vertex j, and suppose that p contains at most m edges.Assuming that there are no negative-weight cycles,m is finite.If i=j,then p has weight 0 and no edges.If vertices i and j are distinct, p' then we decompose path p into i k j,where path p’ now contains at most m-1 edges.Moreover, p’ is a shortest path from i to k. Thus, we have (i, j ) (i, k ) +wkj. 8 A recursive solution to the allpairs shortest-paths problem: • Now,let dij(m) be the minimum weight of any path from vertex i to vertex j that contains at most m edges.When m=0,there is a shortest path from i to j with no edges if and only if i=j.For m>=1,we compute dij(m) as the minimum of dij(m-1) (the weight of the shortest path from i to j consisting of at most m-1 edges) and the minimum weight of any path from i to j consisting of at most m edges,obtained by looking at all possible predecessors k of j.Thus,we recursively define: (m-1)+w })= min {d (m-1)+w }. • dij(m)=min ( dij(m-1), 1min kj ik kj k n {dik 1 k n 9 Continue: • If the graph contains no negative-weight cycles,then all shortest paths are simple and thus contain at most n-1 edges.A path from vertex i to vertex j with more than n-1 edges cannot have less than a shortest path from i to j.The actual shortest-path weights are therefore given by • (i, j ) =dij(n-1)=dij(n)=dij(n+1)=... 10 Computing the shortest-path weights bottom up: • We compute the shortest-path weights by extending shortest paths edge by edge.Letting A*B denote the matrix “product” returned by EXTEND-SHORTESTPATH(A,B) .We compute the sequence of n-1 matrices: • D(1)=D(0)*W=W, • D(2)=D(1)*W=W2, • … • D(n-1)=D(n-2)*W=Wn-1. • As we argued above, the matrix D(n-1)=Wn-1 contains the shortest-path weights.So we get the algorithm. 11 Continue: • • • • • • SLOW-ALL-PAIRS-SHORTEST-PATH(W) n rows[W] D(1) W for m 2 to n-1 do D(m) EXTEND-SHORTEST-PATHS(D(m-1),W) return D(n-1) 12 Continue: • • • • • • • • • EXTEND-SHORTEST-PATH(D,W) n rows[D] let D’=(dij’) be a n*n matrix for i 1 to n do for j 1 to n do dij’ for k 1 to n do dij’ min(dij’,dik+wkj) return D’ 13 Example: • Figure 1 2 4 3 1 3 8 1 -4 2 7 5 6 -5 4 14 0 3 8 4 0 1 7 4 0 (1) D = 2 5 0 6 0 8 2 4 0 3 3 0 4 1 7 D(2)= 4 0 5 11 2 1 5 0 2 8 1 6 0 0 3 3 2 4 3 0 4 1 1 7 4 0 5 11 D(3)= 2 1 5 0 2 8 5 1 6 0 0 1 3 2 4 3 0 4 1 1 7 4 0 5 3 (4) D = 2 1 5 0 2 8 5 1 6 0 15 Improving the running time: • Our goal,however, is not to compute all the D(m) matrices: we are interested in matrix D(n-1).Recall that in the absence of negative-weight cycles, D(m)=D(n-1),for all integers m>=n-1.So, we can compute D(n-1) with only lg(n 1) matrix products by computing the sequence: • D(1)=W, • D(2)=W2=W*W • D(4)= W2* W2 • … • D(2 lg(n 1) )=W2lg(n 1) =W2 lg(n 1)-1 * W2 lg(n 1) -1 16 Continue: • • • • • • • FASTER-ALL-PAIRS-SHORTEST-PATHS(W) n rows[W] D(1) W while n-1>m do D(2m) EXTEND-SHORTEST-PATHS(D(m),D(m)) m 2m return D(m) 17 Floyd-Warshall algorithm: • In this section, we shall use a different dynamicprogramming formulation to solve the all-pairs shortestpaths problem on a directed graph G=(V,E).The resulting algorithm, known as the Floyd-Warshall algorithm, runs in 3) time. As before, negative-weight edges may be (V present, but we shall assume that there are no negativeweight cycles. 18 The structure of a shortest path: • In the Floyd-Warshall algorithm, we use a different characterization of the structure of a shortest path than we used in the matrix-multiplication-based all-pairs algorithms.The algorithm considers the “intermediate” vertices of a shortest path, where an intermediate vertex of a simple path p=<v1,v2,…,vl> is any vertex of p other than v1 or vl, that is, any vertex in the set {v2,v3,…,vl-1} 19 Continue: • Let the vertices of G be V={1,2,…,n}, and consider a subset {1,2,…,k} of vertices for some k.For any pair of vertices i,j V,consider all paths from i to j whose intermediate vertices are all drawn from {1,2,…,k},and let p be a minimum-weight path from among them.The FloydWarshall algorithm exploits a relationship between path p and shortest paths from i to j with all intermediate vertices in the set {1,2,…,k-1}. 20 Continue: • The relationship depends on whether or not k is an intermediate vertex of path p. • If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1,2,…,k-1}. Thus, a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k-1} also a shortest path from i to j with all intermediate vertices in the set {1,2,…,k}. • If k is an intermediate vertex of path p,then we break p p1 p2 down into i k j as shown Figure 2.p1 is a shortest path from i to k with all intermediate vertices in 21 the set {1,2,…,k-1}, so as p2. All intermediate vertices in {1,2,…,k-1} p1 i k p2 j P:all intermediate vertices in {1,2,…,k} Figure2 Path p is a shortest path from vertex i to vertex j,and k is the highest-numbered intermediate vertex of p. Path p1, the portion of path p from vertex i to vertex k,has all intermediate vertices in the set {1,2,…,k-1}.The same holds for path p2 from vertex k to vertex j. 22 A recursive solution to the allpairs shortest paths problem: • Let dij(k) be the weight of a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k}. A recursive definition is given by • dij(k)= wij if k=0, • min(dij(k-1),dik(k-1)+dkj(k-1)) if k 1. • The matrix D(n)=(dij(n)) gives the final answer-dij(n)= (i, j ) for all i,j V-because all intermediate vertices are in the set {1,2,…,n}. 23 Computing the shortest-path weights bottom up: • • • • • • • FLOYD-WARSHALL(W) n rows[W] D(0) W for k 1 to n do for i 1 to n do for j 1 to n dij(k) min(dij(k-1),dik(k-1)+dkj(k-1)) • return D(n) 24 Example: • Figure 3 2 4 3 1 3 8 1 -4 2 7 5 6 -5 4 25 0 3 8 4 0 1 7 4 0 (0) D = 2 5 0 6 0 1 NIL 1 NIL 1 2 NIL NIL NIL 2 NIL 3 NIL NIL NIL (0) = NIL 4 NIL NIL 4 NIL NIL NIL 5 NIL 0 3 8 4 0 1 7 4 0 D(1)= 2 5 5 0 2 6 0 1 NIL 1 NIL 1 2 NIL NIL NIL 2 (1)= NIL 3 NIL NIL NIL 1 4 NIL 1 4 NIL NIL NIL 5 NIL 26 0 3 8 0 4 0 (2) D = 2 5 5 4 4 1 7 5 11 0 2 6 0 1 2 1 NIL 1 2 NIL NIL NIL 2 NIL 3 NIL 2 2 (2) = 1 4 NIL 1 4 NIL NIL NIL 5 NIL 8 0 3 0 4 0 (3) D = 2 1 5 4 4 1 7 5 11 0 2 6 0 1 2 1 NIL 1 2 NIL NIL NIL 2 (3)= NIL 3 NIL 2 2 3 4 NIL 1 4 NIL NIL NIL 5 NIL 27 0 3 1 4 4 3 0 4 1 1 7 4 0 5 3 (4) D = 2 1 5 0 2 8 5 1 6 0 4 2 1 NIL 1 NIL 4 2 1 4 4 3 NIL 2 1 (4) = 3 4 NIL 1 4 4 3 4 5 NIL 0 1 3 2 4 3 0 4 1 1 7 4 0 5 3 D(5)= 2 1 5 0 2 8 5 1 6 0 4 5 1 NIL 3 NIL 4 2 1 4 (5)= 4 3 NIL 2 1 3 4 NIL 1 4 4 3 4 5 NIL 28 The End: 29