Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22.

Similar presentations


Presentation on theme: "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22."— Presentation transcript:

1 CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22

2 Shortest Paths Shortest path problem: Given a directed graph G = (V, E), with edge weights c vw, find shortest path from node s to node t –Ex: Nodes represent agents in a financial setting and c vw is cost of transaction in which we buy from agent v and sell immediately to w s 3 t 2 6 7 4 5 10 18 -16 9 6 15 -8 30 20 44 16 11 6 19 6

3 Shortest Paths: Failed Attempts Dijkstra: Can fail if negative edge costs Re-weighting: Adding a constant to every edge weight can fail u t sv 2 1 3 -6 st 2 3 2 -3 3 5 5 6 6 0 A path that starts on an expensive edge, but then compensates with subsequent edges of negative cost, can be cheaper than a path that starts on a cheap edge. Dijskstra-style greedy approach will not work! Unfortunately, changing the costs also changes the minimum-cost path!

4 Shortest Paths: Negative Cost Cycles Negative cost cycle Observation: If some path from s to t contains a negative cost cycle, there does not exist a shortest s-t path; otherwise, there exists one that is simple. Why? –Shortest paths have at most n-1 edges -6 7 -4 st W c(W) < 0

5 Shortest Paths: Dynamic Programming Let’s use DP to solve problem of finding shortest path from s to t when there are negative edge costs but no negative cycles We could try an idea that has worked for us so far: subproblem i could be to find a shortest path using only the first i nodes –This idea does not immediately work, but can be made to work with some effort –We will discuss a simpler and more efficient solution, the Bellman-Ford Algorithm. This algorithm is based on the crucial observation that if G has no negative cycles, then there is a shortest path from s to t that is simple, and hence has at most n- 1 edges

6 Shortest Paths: Dynamic Programming Def: OPT(i, v) = length of shortest v-t path P using at most i edges –Case 1: P uses at most i-1 edges OPT(i, v) = OPT(i-1, v) –Case 2: P uses exactly i edges if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t path using at most i-1 edges Write final recurrence (DONE IN CLASS) Remark: By previous observation, if no negative cycles, then OPT(n-1, v) = length of shortest v-t path v wt

7 Shortest Paths: Implementation Shortest-Path (G, s, t) { foreach node v  V Opt[0, v]   Opt[0, t]  0 for i = 1 to n-1 foreach node v  V Compute Opt[i, v] using the recurrence return Opt[n-1, s] } Example (DONE IN CLASS) Analysis: O(n 3 ) time,  (n 2 ) space –Can be implemented in O(mn) time if G does not have too many edges (i.e., G is sparse). Memory requirements can also be reduced to O(n)

8 Detecting Negative Cycles Lemma: If OPT(n,v) = OPT(n-1,v) for all v, then no negative cycles –Pf: Bellman-Ford algorithm Lemma: If OPT(n,v) < OPT(n-1,v) for some node v, then (any) shortest path from v to t contains a cycle W. Moreover W has negative cost. Pf. (by contradiction) –Since OPT(n,v) < OPT(n-1,v), we know P has exactly n edges –By pigeonhole principle, P must contain a directed cycle W. –Deleting W yields a v-t path with < n edges  W has negative cost v t W c(W) < 0

9 Detecting Negative Cycles: Application Currency conversion: Given n currencies and exchange rates between pairs of currencies, is there an arbitrage opportunity? –Remark: Fastest algorithm very valuable! F$ £ ¥ DM 1/7 3/10 2/3 2 17056 3/50 4/3 8 IBM 1/10000 800


Download ppt "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22."

Similar presentations


Ads by Google