Download presentation
Presentation is loading. Please wait.
Published byJeremy Baldwin Modified over 9 years ago
1
2004 SDU Lecture11- All-pairs shortest paths
2
Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer partition problems into independent sub-problems, dynamic programming is applicable when the sub-problems are dependent. 2002 SDU 2
3
Dynamic programming Comparing to greedy method: Both are applicable when a problem exhibits optimal substructure In greedy algorithms, we use optimal substructure in a top-down fashion, that is, without finding optimal solutions to sub-problems, we first make a choice that looks best at the time-and then solve a resulting sub-problem. While in dynamic programming, we use optimal substructure in a bottom-up fashion, that is, we first find optimal solutions to sub-problems and, having solved the sub-problems, we find an optimal solution to the problem. 2002 SDU 3
4
Dynamic programming The development of a dynamic programming algorithm can be broken into four steps: 1.Characterize the structure of an optimal solution. 2.Recursively define the value of an optimal solution. 3.Compute the value of an optimal solution in a bottom-up fashion. 4.Construct an optimal solution from computed information. Steps 1-3 form the basis of a dynamic-programming solution to a problem. Step 4 can be omitted if only the value of an optimal solution is required. When we do perform step 4, we sometimes maintain additional information during the computation in step 3 to ease the construction of an optimal solution. 2002 SDU 4
5
5 0-1 version v/w: 1, 3, 3.6, 3.66, 4.
6
2015-5-15 6
7
7
8
8
9
9
10
10 35
11
2002 SDU 11 All –pairs shortest-paths problem Problem: Given a directed graph G=(V, E), and a weight function w: E R, for each pair of vertices u, v, compute the shortest path weight (u, v), and a shortest path if exists. Output: A V V matrix D = (d ij ), where, d ij contains the shortest path weight from vertex i to vertex j. //Important! A V V matrix =( ij ), where, ij is NIL if either i=j or there is no path from i to j, otherwise ij is the predecessor of j on some shortest path from i. // Not covered in class, but in Exercises!
12
2002 SDU 12 Methods 1)Application of single-source shortest-path algorithms 2)Direct methods to solve the problem: 1)Matrix multiplication 2)Floyd-Warshall algorithm 3)Johnson’s algorithm for sparse graphs 3)Transitive closure (Floyd-Warshall algorithm)
13
2002 SDU 13 Matrix multiplication -- suppose there are no negative cycles. A dynamic programming method: Study structure of an optimal solution Solve the problem recursively Compute the value of an optimal solution in a bottom- up manner The operation of each loop is like matrix multiplication.
14
2002 SDU 14 Matrix multiplication—structure of a shortest path Suppose W = (w ij ) is the adjacency matrix such that Consider a shortest path P from i to j, and suppose that P has at most m edges. Then, if i = j, P has weight 0 and no edges. If i j, we can decompose P into i k j, P’ is a shortest path from i to k. P’
15
2002 SDU 15 Matrix multiplication—recursive solution Let l ij (m) be the minimum weight of any path from i to j that contains at most m edges. l ij (0) = 0, if i = j, and otherwise. For m 1, l ij (m) = min{l ik (m-1) +w kj }, 1 k n The solution is l ij (n-1)
16
2002 SDU 16 Matrix Multiplication Solve the problem stage by stage (dynamic programming) L (1) = W L (2) … L (n-1) where L (m), contains the shortest path weight with path length m.
17
2002 SDU 17 Matrix multiplication (pseudo- code)
18
2002 SDU 18 Matrix multiplication (pseudo- code)
19
2002 SDU 19 Matrix multiplication (running time) O(n 4 ) Improving the running time: No need to compute all the L (m) matrices for 1 m n-1. We are interested only in L (n-1), which is equal to L (m) for all integers m ≥ n-1, with assuming that there are no negative cycles.
20
2002 SDU 20 Improving the running time Compute the sequence L (1) = W, L (2) = W 2 = W W, L (4) = W 4 = W 2 W 2, L (8) = W 8 = W 4 W 4... We need only lg(n-1) matrix products Time complexity: O(n 3 lg n)
21
2002 SDU 21 Improving running time
22
2002 SDU 22 Floyd-Warshall algorithm -- suppose there are no negative cycles. -structure of shortest path
23
2002 SDU 23 Floyd-Warshall algorithm (idea) d ij (k) : shortest path weight from i to j with intermediate vertices (excluding i, j) from the set {1,2,…,k} Intermediate vertex of a simple path p = is any vertex of p other than v 1 or v l. d ij (0) = w ij (no intermediate vertices at all) How to compute d ij (k) from D (r), for r < k?
24
2002 SDU 24 Floyd-Warshall algorithm - recursive solution d ij (0) = w ij (no intermediate vertices at all) d ij (k) = min(d ij (k-1), d ik (k-1) + d kj (k-1) ) if k ≥ 1 Result: D (n) = (d ij (n) ) (because all intermediate vertices are in the set {1, 2, …, n})
25
2002 SDU 25 Floyd-Warshall algorithm - compute shortest-path weights Solve the problem stage by stage: D(0) D(1) D(2) … D(n) where D(k) contains the shortest path weight with all the intermediate vertices from set {1,2…,k}.
26
2002 SDU 26 Floyd-Warshall algorithm 5 1 2 3 4 3 4 -5 6 -4 2 1 8 7 k=1: d 43 (1) = -5; d 42 (1) =5; d 45 (1) =-2
27
2002 SDU 27 Floyd-Warshall algorithm 5 1 2 3 4 3 4 -5 6 -4 2 1 8 7 k=2: d 43 (2) =-5; d 42 (2) =5; d 45 (2) = 2
28
2002 SDU 28 Floyd-Warshall algorithm 5 1 2 3 4 3 4 -5 6 -4 2 1 8 7 k=3: d 42 (3) =-1; d 43 (3) =-5; d 45 (3) =-2
29
2002 SDU 29 Floyd-Warhsall algorithm (pseudo-code) Time complexity: O(n 3 ) Space: O(n 3 )
30
2002 SDU 30 Floyd-Warshall algorithm (less space) Notice that: we can write d ij (k) directly on D (k-1) k-th row k-th column d ij (k) d ik (k-1) d kj (k-1) d ij (k) d kj (k) d ik (k)
31
2002 SDU 31 Floyd-Warhsall algorithm (less space)
32
2002 SDU 32 Constructing a shortest path For k = 0 For k 1
33
2002 SDU 33 Print all-pairs shortest paths
34
2002 SDU 34 Example: 1 54 3 2 3 4 7 -4 8 1 -5 2 6
35
2002 SDU 35 D (0) = (0) = D (1) = (1) =
36
2002 SDU 36 D (2) = (2) = D (3) = (3) =
37
2002 SDU 37 D (4) = (4) = D (5) = (5) =
38
2002 SDU 38 1 54 3 2 3 4 7 -4 8 1 -5 2 6 Shortest path from 1 to 2 in
39
2002 SDU 39 Transitive closure (the problem) Given a graph G=(V, E), compute the transitive closure G * = (V, E * ), where E * = {(i, j): There is a path from i to j in G} ij a b c
40
2002 SDU 40 Transitive closure One way: set w ij = 1 and run the Floyd-Warshall algorithm, if d ij (n) < , then (i, j) E * Running time O(n 3 )
41
2002 SDU 41 transitive closure Another way: Substitute “+” and “min” by AND and OR in Floyd’s algorithm Running time O(n 3 )
42
2002 SDU 42 Transitive closure (pseudo- code)
43
2002 SDU 43 Transitive closure
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.