Presentation is loading. Please wait.

Presentation is loading. Please wait.

5/27/03CSE 202 - Shortest Paths CSE 202 - Algorithms Shortest Paths Problems.

Similar presentations


Presentation on theme: "5/27/03CSE 202 - Shortest Paths CSE 202 - Algorithms Shortest Paths Problems."— Presentation transcript:

1 5/27/03CSE 202 - Shortest Paths CSE 202 - Algorithms Shortest Paths Problems

2 CSE 202 - Shortest Paths2 Shortest Paths Problems Given a weighted directed graph, is a path of weight 29 from u to z. is another path from u to z; it has weight 16 and is the shortest path from u to z. u z t y w v x 5 1 3 2 8 6 1 1 10 4 13 4 2 9

3 CSE 202 - Shortest Paths3 Variants of Shortest Paths Problems A. Single pair shortest path problem Given s and d, find shortest path from s to d. B. Single source shortest paths problem Given s, for each d find shortest path from s to d. C. All-pairs shortest paths problem For each ordered pair s,d, find shortest path. (A) and (B) seem to have same asymptotic complexity. (C) takes longer, but not as long as repeating (B) for each s.

4 CSE 202 - Shortest Paths4 More Shortest Paths Variants 1.All weights are non-negative. 2.Weights may be negative, but no negative cycles (A cycle is a path from a vertex to itself.) 3.Negative cycles allowed. Algorithm reports “-  ” if there is a negative cycle on path from source to destination (2) and (3) seem to be harder than (1). u w v 5 -6 3 2 uvw u058 v-303 w-60 u w v 5 6 -3 2 uvw u0-- -- v  -- -- w  -- --

5 CSE 202 - Shortest Paths5 Single Source Shortest Paths Non-negative weights (problem B-1): From source, construct tree of shortest paths. Why is this a tree? Is this a Minimum Spanning Tree? Basic approach: label nodes with shortest path found so far. Relaxation step: choose any edge (u,v). If it gives a shorter path to v, update v ’s label. 9 4 5 s 1 9 4 5 2 9 7 4 5 s 1 9 4 5 2 9 relaxing this edge improves this node

6 CSE 202 - Shortest Paths6 Single Source Shortest Paths Simplest strategy: –Keep cycling through all edges –When all edges are relaxed, you’re done. What is upper bound on work? Improvement: Mark nodes when we’re sure we have best path. Key insight: if you relax all edges out all marked nodes, the unmarked node that is closest to source can be marked. You can relax edge (u,v) only once - just after u is marked. O(V 2 ) algorithm: mark node; relax its edges; find new min to mark. Dijkstra’s algorithm: Keep nodes on min-priority queue – need E Decrease-Key’s. Gives O(E lg V) - when is this faster? (Aside: can also get O(E + V lg V) using Fibonacci heap.)

7 CSE 202 - Shortest Paths7 Single Source Shortest Paths Negative weights allowed (problem B-2 and B-3) Why can’t we just add a constant to each weight? Why doesn’t Dijkstra’s algorithm work for B-2 ? Bellman-Ford: Cycle through edges V-1 times. O(VE) time. PERT chart: Arbitrary weights, graph is acyclic: Is there a better algorithm than Bellman-Ford?

8 CSE 202 - Shortest Paths8 All-pairs Shortest Paths Naïve #1: Solve V single-source problems. –O(V 2 E) for general problem. –O(V 3 ) or O(V E lg V) for non-negative weights. Naïve #2: Let d k (i,j) = shortest path from i to j involving  k edges. d 1 (i,j) = is original weight matrix. Compute d k+1 ’s from d k ’s by seeing if adding edge helps: d k+1 (i,j) = Min { d k (i,m) + d 1 (m,j) } Hmmm...this looks a lot like matrix multiplication If there are no negative cycles, d V-1 (i,j) is solution. If there is a negative cycle, then d V-1  d V Complexity is O(V 4 )

9 CSE 202 - Shortest Paths9 All-pairs Shortest Paths No negative cycles – Divide and Conquer says: “Shortest path from a to b with at most 2 k+1 hops is a shortest path from a to c with at most 2 k hops followed by one from c to b with at most 2 k hops.” T(k+1) = T(k) + V 3 so T(lg V) is O(V 3 lg V). This also looks like matrix multiplication, using d 2k = d k x d k instead of d k+1 = d k x d 1

10 CSE 202 - Shortest Paths10 All-pairs Shortest Paths No negative cycles – Dynamic Programming approach: Number cities 1 to V. Let S(a,b,k) be length of shortest path from a to b that uses only cities {1,2,...,k} as intermediate points. Intuition: At first (k=0), there are only direct flights between cities. Then (k=1) they allow city 1 to be an intermediate connection point. Then (k=2) they add city 2 as a possible connection point. Etc. Claim: S(a,b,k) = min{ S(a,b,k-1), S(a,k,k-1) + S(k,b,k-1) }. Intuition: either adding k as a possible connection point doesn’t shorten the trip, or it does. “No negative cycles”  needn’t consider paths going through k twice. Complexity: T(k+1)= T(k) + cV 2, and T(0) is 0. Thus, T(V) is  (V 3 ). This is the Floyd-Warshall algorithm

11 CSE 202 - Shortest Paths11 Johnson’s All-Pairs Shortest Paths Motivation: for sparse non-negative weights, “O(V E lg V)” is better than “O(V 3 )” We’ll convert “arbitrary weights” (C3) to “non- negative” (C1) problem via reweighting. Dijkstra V times Floyd-Warshall 5 -3 8 -10 1 -3-3 8 0 4 -9 0 Starting bonus or finishing penalty

12 CSE 202 - Shortest Paths12 Johnson’s Algorithm Reweighting can make all edges non-negative Make node-weight(x) = shortest path to x from anywhere. Sounds like all-pairs problem Slick trick use Bellman-Ford (only O(VE) time) 5 -3 8 -10 0 5 10 2 3 0 0 0 4 5 -3 8 -10 s 0 0 0 0

13 CSE 202 - Shortest Paths13 Glossary (in case symbols are weird)            subset  element of  infinity  empty set  for all  there exists  intersection  union  big theta  big omega  summation  >=  <=  about equal  not equal  natural numbers(N)  reals(R)  rationals(Q)  integers(Z)


Download ppt "5/27/03CSE 202 - Shortest Paths CSE 202 - Algorithms Shortest Paths Problems."

Similar presentations


Ads by Google