ata Structures and Algorithms MPS LTUR ellman-ford Shortest Paths Algorithm etecting Negative ost ycles Adam Smith // A. Smith; based on slides by K. Wayne,. Leiserson,. emaine L.
Negative Weight dges S A - This graph causes ijkstra s algorithm to fail (incorrectly estimate the distance and shortest path to A) 0//0
Shortest Paths: Negative ost ycles Negative cost cycle. - - 7 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. s W t c(w) < 0
Shortest Paths: ynamic Programming ef. OPT(i, v) = length of shortest v-t path P using at most i edges ( if no such path exists) ase : P uses at most i- edges. OPT(i, v) = OPT(i-, v) ase : 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- edges OPT(i, v) = 0 or if i = 0 min OPT(i, v), min { OPT(i, w)+ c vw } otherwise (v, w) Remark. y previous observation, if no negative cycles, then OPT(n-, v) = length of shortest v-t path.
Shortest Paths: Implementation Shortest-Path(G, t) { foreach node v V M[0, v] M[0, t] 0 } for i = to n- foreach node v V M[i, v] M[i-, v] foreach edge (v, w) M[i, v] min { M[i, v], M[i-, w] + c vw } Analysis. Θ(mn) time, Θ(n ) space. Finding the shortest paths. Maintain a "successor" for each table entry.
xample of ellman-ford A The demonstration is for a sligtly different version of the algorithm (see LRS) that computes distances from the sourse node rather than distances to the destination node. 0//0
xample of ellman-ford 0 A Initialization. 0//0
xample of ellman-ford 0 7 A Order of edge relaxation. 0//0
xample of ellman-ford 0 7 A 0//0
xample of ellman-ford 0 7 A 0//0
xample of ellman-ford 0 7 A 0//0
xample of ellman-ford - 0 7 A 0//0
xample of ellman-ford - 0 7 A 0//0
xample of ellman-ford - 0 7 A 0//0
xample of ellman-ford - 0 7 A 0//0
xample of ellman-ford - 0 7 A 0//0
xample of ellman-ford - 0 7 A nd of pass. 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7 0//0
xample of ellman-ford - 0 A 7-0//0
xample of ellman-ford - 0 A 7 - nd of pass (and and ). 0//0
Shortest Paths: Improvements Maintain only one array M[v] = length of shortest v-t path found so far. No need to check edges of the form (v, w) unless M[w] changed in previous iteration. Theorem. Throughout the algorithm, M[v] is length of some v-t path, and after i rounds of updates, the value M[v] is no larger than the length of shortest v-t path using i edges. Overall impact. Memory: O(m + n). Running time: O(mn) worst case, but substantially faster in practice.
ellman-ford: fficient Implementation ellman-ford-shortest-path(g, s, t) { foreach node v V { M[v] successor[v] φ } } M[t] = 0 for i = to n- { foreach node w V { if (M[w] has been updated in previous iteration) { foreach node v such that (v, w) { if (M[v] > M[w] + c vw ) { M[v] M[w] + c vw successor[v] w } } } If no M[w] value changed in iteration i, stop. }
istance Vector Protocol ommunication network. Nodes routers. dges direct communication links. ost of edge delay on link. naturally nonnegative, but ellman-ford used anyway! ijkstra's algorithm. Requires global information of network. ellman-ford. Uses only local knowledge of neighboring nodes. Synchronization. We don't expect routers to run in lockstep. The order in which each foreach loop executes in not important. Moreover, algorithm still converges even if updates are asynchronous.
istance Vector Protocol istance vector protocol. ach router maintains a vector of shortest path lengths to every other node (distances) and the first hop on each path (directions). Algorithm: each router performs n separate computations, one for each potential destination node. "Routing by rumor." x. RIP, Xerox XNS RIP, Novell's IPX RIP, isco's IGRP, 's NA Phase IV, AppleTalk's RTMP. aveat. dge costs may change during algorithm (or fail completely). s v t "counting to infinity" deleted
Path Vector Protocols Link state routing. ach router also stores the entire path. not just the distance and first hop ased on ijkstra's algorithm. Avoids "counting-to-infinity" problem and related difficulties. Requires significantly more storage. x. order Gateway Protocol (GP), Open Shortest Path First (OSPF).
etecting Negative ycles: Application urrency conversion. Given n currencies and exchange rates between pairs of currencies, is there an arbitrage opportunity? Remark. Fastest algorithm very valuable! $ /7 F 00 / / /0 /0 IM /0000 70 M
etecting Negative ycles ellman-ford is guaranteed to work if there are no negative cost cycles. How can we tell if neg. cost cycles exist? We could pick a destination vertex t and check if the cost estimates in ellman-ford converge or not. What is wrong with this? (What if a cycle isn t on any path to t?) - - v -
etecting Negative ycles Theorem. an find negative cost cycle in O(mn) time. Add new node t and connect all nodes to t with 0-cost edge. heck if OPT(n, v) = OPT(n-, v) for all nodes v. if yes, then no negative cycles if no, then extract cycle from shortest path from v to t t 0 0 0 0 0 - - v -
etecting Negative ycles Lemma. If OPT(n,v) = OPT(n-,v) for all v, then no negative cycles are connected to t. Pf. If OPT(n,v)=OPT(n-,v) for all v, then distance estimates won t change again even with many executions of the for loop. In particular, OPT(n,v)=OPT(n-,v). So there are no negative cost cycles. Lemma. If OPT(n,v) < OPT(n-,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-,v), we know P has exactly n edges. y pigeonhole principle, P must contain a directed cycle W. eleting W yields a v-t path with < n edges W has negative cost. v W t c(w) < 0
etecting Negative ycles: Summary ellman-ford. O(mn) time, O(m + n) space. Run ellman-ford for n iterations (instead of n-). Upon termination, ellman-ford successor variables trace a negative cycle if one exists. See p. 0 for improved version and early termination rule.