Download presentation

Presentation is loading. Please wait.

Published byArthur Fowler Modified about 1 year ago

1
1 Chapter 25: All-Pairs Shortest-Paths

2
2 Some Algorithms When no negative edges –Using Dijkstra’s algorithm: O(V 3 ) –Using Binary heap implementation: O(VE lg V) –Using Fibonacci heap: O(VE + V 2 log V) When no negative cycles –Floyd-Warshall [1962] : O(V 3 ) time When negative cycles –Using Bellman-Ford algorithm: O(V 2 E) = O(V 4 ) – Johnson [1977] : O(VE + V 2 log V) time based on a clever combination of Bellman-Ford and Dijkstra

3
3 Notations G = (V, E) with w: E -> R Input form: matrix W= (w ij ) w ij =0 if i = j, w ij = the weight of the directed edge if i≠j and (i, j) є E, Otherwise w ij = ∞ Output: D = (d ij ), d ij = δ(i,j) the shortest weight path from i to j

4
4 A dynamic programming approach 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.

5
5 The structure of an optimal solution Consider a shortest path p from vertex i to vertex j, and suppose that p contains at most m edges. Assume that there are no negative-weight cycles. Hence m ≤ n-1 is finite.

6
6 The structure of an optimal solution If i = j, then p has weight 0 and no edge. If i≠j, we decompose p into i ~ k -> j where p’ contains at most m-1 edges. Moreover, p’ is a shortest path from i to k and δ(i,j) = δ(i,k) + w kj

7
7 Recursive solution to the all- pairs shortest-path problem Define: l ij (m) = minimum weight of any path from i to j that contains at most m edges. 0 if i = j l ij (0) = ∞ if i ≠ j Then l ij (m) = min{ l ij (m-1), min 1 ≤ k ≤ n {l ik (m-1) + w kj }} = min 1 ≤ k ≤ n {l ik (m-1) + w kj } (why?)

8
8 Recursive solution to the all- pairs shortest-path problem Since shortest path from i to j contains at most n-1 edges, δ(i,j) = l ij (n-1) = l ij (n) = l ij (n+1) = … Computing the shortest-path weight bottom up: –Compute L (1),L (2),…., L (n-1) where L (m) =(l ij (m) ) –Note that L (1) = W.

9
9 EXTENDED-SHORTEST- PATHS(L, W) Given matrices L (m-1) and W return L (m) 1n <- L.row 2Let L’ = (l’ ij ) be a new n x n matrix 3for i = 1 to n 4 for j = 1 to n 5 l’ ij = ∞ 6 for k = 1 to n 7 l’ ij = min(l’ ij, l ik + w kj ) 8return L’

10
Example: W =

11
11

12
12 Matrix Multiplication Let l (m-1) -> a w -> b l (m) -> c min -> + + -> ‧ time complexity : O(n 3 ) C ij = k=1 to n a ik ‧ b kj l ij (m) = min 1≤k≤n {l ik (m-1) + w kj }

13
13 SLOW-ALL-PAIRS- SHORTEST-PATHS(W) L (1) = L (0) ‧ W = W L (2) = L (1) ‧ W = W 2 L (3) = L (2) ‧ W = W 3 ‧ L (n-1) = L (n-2) ‧ W = W n-1

14
14 SLOW-ALL-PAIRS- SHORTEST-PATHS(W) 1 n = W.rows 2 L (1) = W 3for m = 2 to n-1 4 let L (m) be a new n x n matrix 5 L (m) = EXTENDED-SHORTEST- PATHS( L (m-1), W ) 6return L (n-1) Time complexity : O(n 4 )

15
15 Improving the running time L (1) = W L (2) = W 2 =W ． W L (4) =W 4 = W 2 ． W 2. i.e., using repeating squaring! Time complexity: O(n 3 lg n)

16
16 FASTER-ALL-PAIRS-SHORTEST-PATHS(W) 1. n =W.row 2. L (1) =W 3. m =1 4. while m < n-1 5. let L (2m) be a new n x n matrix 6. L (2m) = Extend-Shorest-Path(L (m), L (m) ) 7. m = 2m 8. return L (m) FASTER-ALL-PAIRS-SHORTEST- PATHS

17
17 The Floyd-Warshall algorithm A different dynamic programming formulation ‧ The structure of a shortest path: Let V(G)={1,2,…,n}. For any pair of vertices i, j єV, consider all paths from i to j whose intermediate vertices are drawn from {1, 2,…,k}, and let p be a minimum weight path among them.

18
18 The structure of a shortest path If k is not in p, then all intermediate vertices are in {1, 2,…,k-1}. If k is an intermediate vertex of p, then p can be decomposed into i ~ k ~ j where p 1 is a shortest path from i to k with all the intermediate vertices in {1,2,…,k-1} and p 2 is a shortest path from k to j with all the intermediate vertices in {1,2,…,k-1}.

19
19 A recursive solution to the all- pairs shortest path Let d ij k =the weight of a shortest path from vertex i to vertex j with all intermediate vertices in the set {1,2,…,k}. d ij (k) = w ij if k = 0 = min(d ij (k-1), d ik (k-1) + d kj (k-1) ) if k ≥ 1 D (n) = (d ij (n) ) if the final solution!

20
20 FLOYD-WARSHALL(W) 1. n = W.rows 2. D (0) = W 3. for k = 1 to n 4. Let D (k) = (d ij (k) ) be a new n x n matrix 5. for i = 1 to n 6. for j = 1 to n 7. d ij (k) = min (d ij (k-1), d ik (k-1) + d kj (k-1) ) 8. return D (n) Complexity: O(n 3 )

21
21 Constructing a shortest path π (0), π (1),…, π (n) π ij (k) : is the predecessor of the vertex j on a shortest path from vertex i with all intermediate vertices in the set {1,2,…,k}. π ij (0) = NIL if i=j or w ij = ∞ = i if i ≠j and w ij < ∞ π ij (k) = π ij (k-1) if d ij (k-1) ≤ d ik (k-1) + d kj (k-1) = π kj (k-1) if d ij (k-1) > d ik (k-1) + d kj (k-1)

22
22 Example

23
23

24
24

25
25 Transitive closure of a directed graph Given a directed graph G = (V, E) with V = {1,2,…, n} The transitive closure of G is G*= (V, E*) where E*={(i, j)| there is a path from i to j in G}. Modify FLOYD-WARSHALL algorithm: t ij (0) = 0 if i≠j and (i,j) ∉ E 1 if i=j or (i,j) є E for k ≥ 1 t ij (k) = t ij (k-1) (t ik (k-1) t kj (k-1) )

26
26 TRANSITIVE-CLOSURE(G) 1n = |G.V| 2Let T (0) = (t ij (0) ) be a new n x n matrix 3 for i = 1 to n 4 for j =1 to n 5 if i == j or (i, j) ∈ G.E 6 t ij (0) = 1 7 else t ij (0) = 0 8for k =1 to n 9 Let T (k) = (t ij (k) ) be a new n x n matrix 10 for i = 1 to n 11 for j =1 to n 12 t ij (k) = t ij (k-1) (t ik (k-1) t kj (k-1) ) 123 return T (n) Time complexity: O(n 3 )

27
27 Example ①② ③④

28
28 Some Algorithms When no negative edges –Using Dijkstra’s algorithm: O(V 3 ) –Using Binary heap implementation: O(VE lg V) –Using Fibonacci heap: O(VE + V 2 log V) When no negative cycles –Floyd-Warshall [1962] : O(V 3 ) time When negative cycles –Using Bellman-Ford algorithm: O(V 2 E) = O(V 4 ) – Johnson [1977] : O(VE + V 2 log V) time based on a clever combination of Bellman-Ford and Dijkstra

29
29 Johnson’s algorithm for sparse graphs If all edge weights in G are nonnegative, we can find all shortest paths in O(V 2 lg V + VE) by using Dijkstra’s algorithm with Fibonacci heap Bellman-Ford algorithm takes O(VE) Using reweighting technique

30
30 Reweighting technique If G has negative-weighted edge, we compute a new set of nonnegative weight that allows us to use the same method. The new set of edge weight ŵ satisfies: 1. For all pairs of vertices u, v єV, a shortest path from u to v using weight function w is also a shortest path from u to v using the weight function ŵ 2. ∀ (u,v) є E(G), ŵ(u,v) is nonnegative

31
31 Lemma: (Reweighting doesn’t change shortest paths) Given a weighted directed graph G = (V, E) with weight function w:E → R, let h:V → R be any function mapping vertices to real numbers. For each edge (u,v) є E, ŵ(u,v) = w(u,v) + h(u) – h(v) Let P= be a path from vertex v 0 to v k Then w(p) = δ(v 0,v k ) if and only if ŵ(p) =(v 0,v k ) Also, G has a negative-weight cycle using weight function w iff G has a negative weight cycle using weight function ŵ.

32
32 Proof ŵ(p) = w(p) + h(v 0 ) – h(v k ) ŵ(p) = ŵ(v i-1,v i ) = (w(v i-1,v i ) + h(v i-1 )-h(v i )) = w(v i-1,v i ) + h(v 0 )-h(v k ) =w(p) + h(v 0 ) – h(v k )

33
33 Proof Because h(v 0 ) and h(v k ) do not depend on the path, if one path from v 0 to v k is shorter than another using weight function w, then it is also shorter using ŵ. Thus, w(p) = δ(v 0,v k ) if and only if ŵ(p) = (v 0,v k )

34
34 Proof G has a negative-weight cycle using w iff G has a negative-weight cycle using ŵ. Consider any cycle C= with v 0 =v k. Then ŵ(C) = w(C) + h(v 0 ) – h(v k ) = w(C).

35
35 Producing nonnegative weight by reweighting Given a weighted directed graph G = (V, E) We make a new graph G’= (V’,E’), V’ = V ∪ {s}, E’ = E ∪ {(s,v): v є V} and w(s,v) = 0, for all v in V Let h(v) = δ(s, v) for all v V’ We have h(v) ≤ h(u) + w(u, v) (why?) ŵ(u, v) = w(u, v) + h(u) – h(v) ≥ 0

36
Example:

37
S

38
S h(v) = δ(s, v) ŵ(u, v) = w(u, v) + h(u) – h(v)

39
39 JOHNSON algorithm 1 Computing G’, where G’.V = G.V ∪ {s} and G’.E= G.E ∪ {(s, v): v є G.V} and w(s, v) = 0. 2 if BELLMAN-FORD(G’, w, s)= FALSE 3 print “the input graph contains negative weight cycle” 4 else for each vertex v є G’.V 5 set h(v) to be the value of δ(s, v) computed by the BF algorithm 6 for each edge (u, v) є G’.E, ŵ(u, v) = w(u, v) + h(u) – h(v)

40
40 JOHNSON algorithm 7 Let D = (d uv ) be a new n x n matrix 8 for each vertex u є G.V run DIJKSTRA (G, ŵ, u) to compute (u, v) for all v є V[G]. 10 for each vertex v є G.V 11 d uv = (u, v) + h(v) – h(u) 12 return D Complexity: O(V 2 lgV + VE)

41
/1 2/-3 2/2 0/-4 0/ /0 2/3 0/-4 0/12/ /0 0/4 2/7 2/3 0/5 2

42
/2 0/-1 0/-5 0/0 2/ /5 4/8 0/0 2/1 2/6

43
43 Homework Practice at home: Exercises: , , Practice at home : , Exercises: (Due: Jan. 4) Practice at home : Exercises: (Due: Jan. 4)

44
44 Quiz 5 Please show the ordering of the following vertices produced by Topological Sort Algorithm under-shorts pants belt shirt tie socks shoes jacket

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google