# 1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.

## Presentation on theme: "1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm."— Presentation transcript:

1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm

2 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.

3 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?

4 Preliminary knowledge: In this chapter, we use an adjacency-matrix representation: w ij = 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=(d ij ),where entry d ij 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 d ij = at termination.

5 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}

6 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 (V 4 )-time algorithm for the all-pairs shortest-paths problem and the improve its running time to (V 3 lgV).

7 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.

8 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 +w kj.

9 A recursive solution to the all- pairs shortest-paths problem: Now,let d ij (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 d ij (m) as the minimum of d ij (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: d ij (m) =min ( d ij (m-1), {d ik (m-1) +w kj })= {d ik (m-1) +w kj }.

10 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 =d ij (n-1) =d ij (n) =d ij (n+1) =...

11 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=W 2, … D (n-1) =D (n-2) *W=W n-1. As we argued above, the matrix D (n-1) =W n-1 contains the shortest-path weights.So we get the algorithm.

12 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)

13 Continue: EXTEND-SHORTEST-PATH(D,W) n rows[D] let D’=(d ij ’) be a n*n matrix for i 1 to n do for j 1 to n do d ij ’ for k 1 to n do d ij ’ min(d ij ’,d ik +w kj ) return D’

14 Example: 1 54 3 2 3 4 7 -4 8 1 -5 2 Figure 1 6

15 D (1) =D (2) = D (3) = D (4) =

16 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) =W 2 =W*W D (4) = W 2 * W 2 … D (2 ) =W 2 =W 2 -1 * W 2 -1

17 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)

18 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 (V 3 ) time. As before, negative-weight edges may be present, but we shall assume that there are no negative- weight cycles.

19 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= is any vertex of p other than v 1 or v l, that is, any vertex in the set {v 2,v 3,…,v l-1 }

20 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}.

21 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.

22 i j k p1 p2 All intermediate vertices in {1,2,…,k-1} 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.

23 A recursive solution to the all- pairs shortest paths problem: Let d ij (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 d ij (k) = w ij if k=0, min(d ij (k-1),d ik (k-1) +d kj (k-1) ) if k 1. The matrix D (n) =(d ij (n) ) gives the final answer-d ij (n) = for all i,j V-because all intermediate vertices are in the set {1,2,…,n}.

24 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 d ij (k) min(d ij (k-1),d ik (k-1) +d kj (k-1) ) return D (n)

25 Example: 1 54 3 2 3 4 7 -4 8 1 -5 2 Figure 3 6

26 D (0) = (0) = D (1) = (1) =

27 D (2) = (2) = D (3) = (3) =

28 D (4) = (4) = D (5) = (5) =

29 The End:

Download ppt "1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm."

Similar presentations