290 likes | 522 Views
Chapter 26 All-Pairs Shortest Paths. Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm. 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?
E N D
Chapter 26 All-Pairs Shortest Paths • Problem definition • Shortest paths and matrix multiplication • The Floyd-Warshall algorithm
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? • Modelthe 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.
Solve the problem by Single-Source 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?
Preliminary knowledge: • In this chapter, we use an adjacency-matrix representation: • wij= 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 denotes the shortest-path weight from vertex i to vertex j, then dij= at termination.
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} • 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).
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.
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, 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 +wkj.
A recursive solution to the all-pairs 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: • dij(m)=min ( dij(m-1), {dik(m-1)+wkj})= {dik(m-1)+wkj}.
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 • =dij(n-1)=dij(n)=dij(n+1)=...
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-SHORTEST-PATH(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.
Continue: • SLOW-ALL-PAIRS-SHORTEST-PATH(W) • n rows[W] • D(1) W • for m2 to n-1 • do D(m) EXTEND-SHORTEST-PATHS(D(m-1),W) • return D(n-1)
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’
Example: • Figure 1 2 4 3 1 3 8 1 -5 -4 2 7 5 4 6
D(1)= D(2)= D(4)= D(3)=
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 matrix products by computing the sequence: • D(1)=W, • D(2)=W2=W*W • D(4)= W2* W2 • … • D(2 )=W2 =W2 -1 * W2 -1
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)
Floyd-Warshall algorithm: • In this section, we shall use a different dynamic-programming formulation to solve the all-pairs shortest-paths problem on a directed graph G=(V,E).The resulting algorithm, known as the Floyd-Warshall algorithm, runs in (V3) time. As before, negative-weight edges may be present, but we shall assume that there are no negative-weight cycles.
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}
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 Floyd-Warshall 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}.
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 down into i k j as shown Figure 2.p1 is a shortest path from i to k with all intermediate vertices in the set {1,2,…,k-1}, so as p2.
All intermediate vertices in {1,2,…,k-1} p2 p1 k j i 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.
A recursive solution to the all-pairs 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)= for all i,j V-because all intermediate vertices are in the set {1,2,…,n}.
Computing the shortest-path weights bottom up: • FLOYD-WARSHALL(W) • n rows[W] • D(0) W • for k1 to n • dofor 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)
Example: • Figure 3 2 4 3 1 3 8 1 -5 -4 2 7 5 4 6
D(0)= (0)= (1)= D(1)=
D(2)= (2)= (3)= D(3)=
D(4)= (4)= (5)= D(5)=