Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 25: All-Pairs Shortest Paths

Similar presentations


Presentation on theme: "Chapter 25: All-Pairs Shortest Paths"— Presentation transcript:

1 Chapter 25: All-Pairs Shortest Paths
Application: Computing distance table for a road atlas. Atlanta Chicago Detroit … Atlanta Chicago Detroit One Approach: Run single-source SP algorithm |V| times. Nonnegative Edges: Use Dijkstra. Time complexity: O(V3) with linear array O(VElg V) with binary heap O(V2lg V + VE) with Fibonacci heap Three algorithms in this chapter: “Repeated Squaring”: O(V3lg V) Floyd-Warshall: O(V3) Johnson’s: O(V2lg V + VE) Negative Edges: Use Bellman-Ford. Time Complexity: O(V2E) = O(V4) for dense graphs negative edges allowed, but no negative cycles CSE246 Marmara Uni

2 “Repeated Squaring” Algorithm
A dynamic-programming algorithm. Assume input graph is given by an adjacency matrix. W = (wij) Let dij(m) = minimum weight of any path from vertex i to vertex j, containing at most m edges. dij(0) = dij(m) = min(dij(m-1), min{dik(m-1) + wkj}) = min1  k  n{dik(m-1) + wkj}, since wjj = 0. Assuming no negative-weight cycles: δ(i,j) = dij(n-1) = dij(n) = dij(n+1) = … 0 if i = j  if i  j CSE246 Marmara Uni

3 “Repeated Squaring” (Continued)
So, given W, we can simply compute a series of matrices D(1), D(2), …, D(n-1) where: D(1) = W D(m) = (dij(m)) [We’ll improve on this shortly.] n := rows[W]; D(1) := W; for m := 2 to n – 1 do D(m) := Extend-SP(D(m-1), W) od; return D(n-1) Extend-SP(D, W) n := rows[D]; 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, dik + wkj) od od; return D´ CSE246 Marmara Uni

4 “Repeated Squaring” and Matrix Mult.
Running time is O(V4). Note the similarity to matrix multiplication: Matrix-Multiply(A, B) n := rows[A]; for i := 1 to n do for j :=1 to n do cij := 0; for k := 1 to n do cij := cij + aikbkj od od; return C SP algorithm computes the following matrix “products”: D(1) = D(0)W = W D(2) = D(1)W = W2 D(3) = D(2)W = W3 D(n-1) = D(n-2)W = Wn-1 CSE246 Marmara Uni

5 Improving the Running Time
Can improve time to O(V3 lg V) by computing “products” as follows: D(1) = W D(2) = W2 = WW D(4) = W4 = W2W2 D(8) = W8 = W4W4 D(2lg(n-1)) = W(2lg(n-1) ) = W 2lg(n-1) -1W 2lg(n-1) -1 D(n-1) = D(2lg(n-1)) Called repeated squaring. n := rows[W]; D(1) := W; m := 1; while n – 1 > m do D(2m) := Extend-SP(D(m), D(m)); m := 2m od; return D(m) Can modify algorithm to use only two matrices. Can also modify to compute predecessor matrix π. Exercise: Run on example graph. CSE246 Marmara Uni

6 Floyd-Warshall Algorithm
Also dynamic programming, but with different recurrence. Runs in O(V3) time. Let dij(k) = weight of SP from vertex i to vertex j with all intermediate vertices in the set {1, 2, …, k}. dij(k) = wij if k = 0 min(dij(k-1), dik(k-1) + dkj(k-1)) if k  1 two possibilities i j i k j all in {1, …, k–1} all in {1, …, k–1} CSE246 Marmara Uni

7 FW (Continued) δ(i,j) = dij(n). So, want to compute D(n) = (dij(n))
n := rows[D]; D(0) := W; for k := 1 to n do for i :=1 to n do for j := 1 to n do dij(k) := min(dij(k-1), dik(k-1) + dkj(k-1)) od od; return D(n) Exercise: Run on example graph. Can reduce space from O(V3) to O(V2) — see Exercise Can also modify to compute predecessor matrix. CSE246 Marmara Uni

8 Predecessor Matrix Let πij(k) = predecessor of vertex j on SP from vertex i with all intermediate vertices in {1, 2, …, k}. NIL if i = j or wij =  i otherwise πij(0) = πij(k-1) if dij(k-1)  dik(k-1) + dkj(k-1) πkj(k-1) otherwise πij(k) = Exercise: Add computation of  matrix to the algorithm. CSE246 Marmara Uni

9 Transitive Closure G = (V, E) T.C. Alg G* = (V, E*)
E* = {(i,j):  path from i to j in G}. Can compute T.C. using Floyd-Warshall in O(V3) time using edge weights of 1. (i,j)  E* iff dij < n. CSE246 Marmara Uni

10 Example 1 2 D(3) = 3 4 D(0) = W = D(4) = D(1) = D(0) 1 2 D(2) = 3 4
   D(3) = 3 4    2  4   0    D(0) = W = D(4) = D(1) = D(0)    2  4  1 2 D(2) = 3 4 CSE246 Marmara Uni

11 Another O(V3) T.C. Algorithm
Uses only bits, so is better in practice. Let tij(k) = 1 iff there exists a path in G from i to j going through intermediate vertices in {1, 2, …, k}. if i  j and (i,j)  E otherwise tij(0) = tij(k) = tij(k-1)  (tik(k-1)  tkj(k-1)) CSE246 Marmara Uni

12 Code See book for how this algorithm runs on previous example. TC(G)
n := |V[G]|; for i := 1 to n do for j :=1 to n do if i = j or (i,j)  E then tij(0) := 1 else tij(0) := 0 fi od od; for k := 1 to n do for i :=1 to n do for j := 1 to n do tij(k) := tij(k-1)  (tik(k-1)  tkj(k-1)) return T(n) See book for how this algorithm runs on previous example. CSE246 Marmara Uni

13 Johnson’s Algorithm An O(V2 lg V + VE) algorithm.
Good for sparse graphs. Uses Dijkstra and Bellman-Ford as subroutines. Basic Idea: Reweight edges to be nonnegative. Then, run Dijkstra’s algorithm once per vertex. Use Bellman-Ford to compute new edge weights w. Must have: For all u and v, a SP from u to v using w is also a SP form u to v using w. For all u and v, w(u,v) is nonnegative. ^ ^ ^ CSE246 Marmara Uni

14 Counterexample Subtracting the minimum weight from every weight doesn’t work. Consider: Paths with more edges are unfairly penalized. -2 -1 1 CSE246 Marmara Uni

15 Johnson’s Insight Add a vertex s to the original graph G, with edges of weight 0 to each vertex in G: Assign new weights ŵ to each edge as follows: ŵ(u, v) = w(u, v) + d(s, u) - d(s, v) s CSE246 Marmara Uni

16 Question 1 Are all the ŵ’s non-negative? Yes:
Otherwise, s  u  v would be shorter than the shortest path from s to v. d(s, u) u w(u, v) s d(s, v) v CSE246 Marmara Uni

17 Question 2 Does the reweighting preserve shortest paths? Yes: Consider any path the sum telescopes A value that depends only on the endpoints, not on the path. In other words, we have adjusted the lengths of all paths by the same amount. So this will not affect the relative ordering of the paths— shortest paths will be preserved. CSE246 Marmara Uni

18 Johnson’s: Running Time
Computing G’: Q(V) Bellman-Ford: Q(VE) Reweighting: Q(E) Running (Modified) Dijkstra: (V2lgV +VElgV) Adjusting distances: Q(V2) —————————————————— Total is dominated by Dijkstra: (V2lgV+VElgV) CSE246 Marmara Uni

19 A General Result about Reweighting
^ Define: w(u,v) = w(u,v) + h(u) – h(v), where h: V  . Lemma 25.1: Let p = ‹v0, v1, …, vk›. Then, (i) w(p) = δ(v0, vk) iff w(p) = δ(v0, vk). (ii) G has a negative-weight cycle using w iff G has a negative-weight cycle using w. ^ ^ ^ Proof of (i): CSE246 Marmara Uni

20 Reweighting in Johnson’s Algorithm
2 –1 Want to define h s.t. w(u,v)  0. Do it like this: ^ 3 4 1 3 8 -5 2 7 1 -4 -5 –4 2 6 5 4 –1 5 1 4 1 3 13 Define h(v) = δ(s, v) v  V. By Lemma 24.10,  (u,v)  E: h(v)  h(u) + w(u,v). Thus, w(u,v) = w(u,v) + h(u) – h(v)  0. –5 2 10 4 –4 2 5 4 ^ CSE246 Marmara Uni

21 LP, shortest paths BF v1 -1 v0 1 v5 v2 -3 -3 5 4 v4 v3 -1 It is possible to find a feasible solution to a system of difference constraints by finding shortest-path weights (from v0) in the corresponding constraint graph, CSE246 Marmara Uni

22 Find a vector: x = <x1,x2,…,xn>
such that max. an objective function: c1x1 + c2x2 + … + cnxn subject to m constraints: Ax  b For some special cases, a linear program can be solved more efficiently than with a general purpose LP algorithm Example: the maximal flow problem has an LP representation, starting with any feasible augmenting solution BF. CSE246 Marmara Uni

23 the difference constraints:
equivalent to solving the difference constraints: Find a vector: x = <x1,x2 x3,x4,x5> that: One solution is x = (-5, -3, 0, -1, -4) Another solution is x = (0, 2, 5, 4, 1) In fact, for any d, (d-5, d-3, d, d-1, d-4) is a solution! CSE246 Marmara Uni Design and Analysis of Algorithms © Sigal Ar Linear Programming, Slide 23

24 Code for Johnson’s Algorithm
Compute G´, where V[G´] = V[G]  {s}, E[G´] = E[G]  {(s,v): v  V[G]}; if Bellman-Ford(G´, w, s) = false then negative-weight cycle else for each v  V[G´] do set h(v) to (s, v) computed by Bellman-Ford od; for each (u,v)  E[G´] do w(u,v) := w(u,v) + h(u) – h(v) for each u  V[G] do run Dijkstra(G, w, u) to compute (u, v) for all v  V[G]; for each v  V[G] do duv := (u, v) + h(v) – h(u) od fi Running time is O(V2 lg V + VE). See Book. ^ ^ ^ ^ CSE246 Marmara Uni

25 Example 2 2/3 2/-1 0/1 0/0 0/-4 1 2 3 4 5 13 10 2/1 4 1 3 13 0/0 2/-3 2 10 0/-4 2/2 2 5 4 2/3 2/-1 0/1 0/0 0/-4 1 2 3 4 5 13 10 4/8 0/0 2/6 2/5 2/1 1 3 4 5 13 10 2 2/2 2/-2 0/0 0/-1 0/-5 1 2 3 4 5 13 10 CSE246 Marmara Uni


Download ppt "Chapter 25: All-Pairs Shortest Paths"

Similar presentations


Ads by Google