CS 473: Algorithms

Chandra Chekuri chekuri@cs.uiuc.edu 3228 Siebel Center
University of Illinois, Urbana-Champaign

Fall 2008

Chekuri

CS473ug

Part I Dijkstra’s Algorithm Recap

Chekuri

CS473ug

Dijkstra’s Algorithm using Priority Queues

Shortest paths from node s to all nodes in V :
Q = makePQ() insert(Q, (s,0)) for each node u = s insert(Q, (u,∞)) S = ∅ for i = 1 to |V| do (v, dist(s,v)) = extractMin(Q) S = S ∪ {v} For each u in Adj(v) do decreaseKey(Q, (u, min (dist(s,u), dist(s,v) + (v,u))))

Algorithm adds nodes to S in order of increasing distance from s

Chekuri

CS473ug

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 t 11 19 4 6 2 18 6 3 0 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 t 11 19 4 6 2 18 6 3 0 6 6 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 t 11 19 4 6 2 18 6 3 9 0 6 9 6 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 t 11 19 4 6 2 18 6 3 9 0 6 9 6 13 13 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 10 9 0 6 6 13 9 19 t 11 19 4 6 2 18 6 3 13 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 10 9 0 6 6 6 13 25 9 19 t 11 19 4 6 2 18 6 3 13 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 10 9 0 6 6 6 13 25 11 36 9 19 t 11 19 4 6 2 18 6 3 13 Chekuri CS473ug .

Example 10 9 s 6 6 6 13 8 20 30 5 16 25 7 10 9 0 6 6 6 13 25 11 36 9 19 t 11 19 4 6 2 18 6 3 25 13 38 Chekuri CS473ug .

Question: How do we ﬁnd the paths themselves? Chekuri CS473ug .Shortest Path Tree Dijkstra’s algorithm ﬁnds the shortest path distances from s to V .

Shortest Path Tree Dijkstra’s algorithm ﬁnds the shortest path distances from s to V . Question: How do we ﬁnd the paths themselves? Q = makePQ() insert(Q.u) ) then decreaseKey(Q. (u.0)) prev(s) = null for each node u = s insert(Q. (s.v) + (v. S = For 1 to |V| do dist(s.∞)) prev(u) = null S = ∅ for i = (v. (u.v)) = extractMin(Q) S ∪ {v} each u in Adj(v) do if (dist(s. dist(s.u) < dist(s.v) + (v.u))) prev(u) = v Chekuri CS473ug .

Shortest Path Tree Lemma The edge set (u. the reverse of the path from u to s in the tree is a shortest path from s to u. The edgeset {(u. prev (u)) | u ∈ V } induces a directed in-tree rooted at s (Why?) Use induction on |S| to argue that the tree is a shortest path tree for nodes in V . Proof Sketch. Chekuri CS473ug . For each u. prev (u)) is the reverse of a shortest path tree rooted at s.

How do we ﬁnd shortest paths from all of V to s? Chekuri CS473ug .Shortest paths to s Dijkstra’s algorithm gives shortest paths from s to all ndoes in V .

How do we ﬁnd shortest paths from all of V to s? In undirected graphs shortest path from s to u is a shortest path from u to s so there is no need to disjtinguish. use Dijkstra’s algorithm in G rev ! Chekuri CS473ug .Shortest paths to s Dijkstra’s algorithm gives shortest paths from s to all ndoes in V . In directed graphs.

Part II Shortest Paths with Negative Length Edges Chekuri CS473ug .

For edge e = (u. v ).Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input A directed graph G = (V . 10 9 s 6 -16 6 15 -8 20 30 5 16 44 7 t 11 19 4 6 2 18 6 3 Chekuri CS473ug . Given nodes s. Given node s ﬁnd shortest path from s to all other nodes. E ) with arbitrary (including negative) edge lengths. (e) = (u. t ﬁnd shortest path from s to t. v ) is its length.

Given nodes s. Given node s ﬁnd shortest path from s to all other nodes. v ) is its length. t ﬁnd shortest path from s to t. For edge e = (u.Single-Source Shortest Paths with Negative Edge Lengths Single-Source Shortest Path Problems Input A directed graph G = (V . v ). E ) with arbitrary (including negative) edge lengths. (e) = (u. 10 9 s 6 -16 6 15 -8 20 30 5 16 44 7 t 11 19 4 6 2 18 6 3 Chekuri CS473ug .

Negative Length Cycles Deﬁnition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. 10 9 s 6 -16 6 15 -8 20 30 5 16 44 7 t 11 4 19 6 2 18 3 3 Chekuri CS473ug .

Negative Length Cycles Deﬁnition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. 10 9 s 6 -16 6 15 -8 20 30 5 16 44 7 t 11 4 19 6 2 18 3 3 Chekuri CS473ug .

Negative Length Cycles Deﬁnition A cycle C is a negative length cycle if the sum of the edge lengths of C is negative. 10 9 s 6 -16 6 15 -8 20 30 5 16 44 7 t 11 4 19 6 2 18 3 3 Chekuri CS473ug .

Suppose G has a negative length cycle C . Question: What is the shortest distance from s to t? Chekuri CS473ug .Shortest Paths and Negative Cycles Given G = (V . E ) with edge lengths and s. and s can reach C and C can reach t. t.

Chekuri CS473ug . OR Deﬁne shortest distance to be the lenth of a shortest simple path from s to t. t.Shortest Paths and Negative Cycles Given G = (V . that is −∞. E ) with edge lengths and s. and s can reach C and C can reach t. Suppose G has a negative length cycle C . Question: What is the shortest distance from s to t? Deﬁne shortest distance to be undeﬁned.

t. Chekuri CS473ug . E ) with edge lengths and s. Suppose G has a negative length cycle C . and s can reach C and C can reach t. Question: What is the shortest distance from s to t? Deﬁne shortest distance to be undeﬁned. Lemma If there is an eﬃcient algorithm to ﬁnd a shortest simple s → t path in a graph with negative edge lengths. that is −∞.Shortest Paths and Negative Cycles Given G = (V . then there is an eﬃcient algorithm to ﬁnd the longest simple s → t path in a graph with positive edge lengths. OR Deﬁne shortest distance to be the lenth of a shortest simple path from s to t.

Lemma If there is an eﬃcient algorithm to ﬁnd a shortest simple s → t path in a graph with negative edge lengths.Shortest Paths and Negative Cycles Given G = (V . Suppose G has a negative length cycle C . that is −∞. E ) with edge lengths and s. then there is an eﬃcient algorithm to ﬁnd the longest simple s → t path in a graph with positive edge lengths. NP-Hard! Chekuri CS473ug . OR Deﬁne shortest distance to be the lenth of a shortest simple path from s to t. Finding the s → t longest path is diﬃcult. and s can reach C and C can reach t. Question: What is the shortest distance from s to t? Deﬁne shortest distance to be undeﬁned. t.

Check if G has a negative length cycle or not. t. either ﬁnd a negative length cycle C that s can reach and t can reached from. For edge e = (u. Given node s. (e) = (u. E ) with arbitrary (including negative) edge lengths. either ﬁnd a negative length cycle C that s can reach or ﬁnd shortest path distances from s to all other nodes. v ). or ﬁnd a shortest path from s to t. Given nodes s. Chekuri CS473ug . v ) is its length.Shortest Paths with Negative Edge Lengths: Problems Algorithmic Problems Input A directed graph G = (V .

Why Negative Lengths? Several Applications Shortest path problems useful in modeling many situations — in some negative lenths are natural Negative length cycle can be used to ﬁnd arbitrage opportunities in currency trading Important sub-routine in algorithms for more general problem: minimum-cost ﬂow Chekuri CS473ug .

b) the exchange rate for converting one unit of a into one unit of b.Application to Currency Trading Currency Trading Input n currencies and for each ordered pair (a. t what is the best way to convert s to t (perhaps via other intermediate currencies)? Chekuri CS473ug . Is there an arbitrage opportunity? Given currencies s.

. . . .Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k1 . k2 . kh then one unit of i yields exch(i. k1 ) × exch(k1 . Chekuri CS473ug . k2 ) . × exch(kh . j) units of j. . .

× exch(kh . j) units of j. k2 ) . k1 ) × exch(k1 . . . .Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k1 . . . Create currency trading graph G = (V . E ): For each currency i there is a node vi ∈ V E = V × V : an edge for each pair of currencies edge length (vi . k2 . kh then one unit of i yields exch(i. . vj ) = Chekuri CS473ug .

k1 ) × exch(k1 . E ): For each currency i there is a node vi ∈ V E = V × V : an edge for each pair of currencies edge length (vi . . . . Create currency trading graph G = (V . k2 ) . j)) can be negative Chekuri CS473ug . j) units of j. × exch(kh . k2 . . . kh then one unit of i yields exch(i. vj ) = log(exch(i.Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k1 . .

. kh then one unit of i yields exch(i. k2 ) . . The best way to convert currency i to currency j is via a shortest path in G from i to j. Chekuri CS473ug . E ): For each currency i there is a node vi ∈ V E = V × V : an edge for each pair of currencies edge length (vi . .Reducing Currency Trading to Shortest Paths Observation: If we convert currency i to j via intermediate currencies k1 . . . j)) can be negative Exercise: Verify that There is an arbitrage opportunity if and only if G has a negative length cycle. k1 ) × exch(k1 . vj ) = log(exch(i. × exch(kh . k2 . . If d is the shortest path distance from i to j then one unit of i can be converted into 2d units of j. j) units of j. Create currency trading graph G = (V .

either ﬁnd a negative length cycle C that s can reach and t can reached from. v ) is its length. (e) = (u. Given node s. E ) with arbitrary (including negative) edge lengths. Chekuri CS473ug . either ﬁnd a negative length cycle C that s can reach or ﬁnd shortest path distances from s to all other nodes. or ﬁnd a shortest path from s to t. Given nodes s. v ).Shortest Paths with Negative Edge Lengths: Problems Algorithmic Problems Input A directed graph G = (V . Check if G has a negative length cycle or not. t. For edge e = (u.

Dijkstra’s algorithm fails u 2 s 1 -6 t 3 v s v u Chekuri CS473ug .Dijkstra’s Algorithm and Negative Lengths Observation With negative cost edges.

Dijkstra’s algorithm fails u 2 s 1 -6 t 1 3 v s 1 v u Chekuri CS473ug .Dijkstra’s Algorithm and Negative Lengths Observation With negative cost edges.

. Holds true only for non-negative edge lengths. . vi ) ≤ dist(s.Dijkstra’s Algorithm and Negative Lengths Observation With negative cost edges. Dijkstra’s algorithm fails u 2 s 1 -6 t 1 3 v s 1 v u False assumption: Dijkstra’s algorithm is based on the assumption that if s = v0 → v1 → v2 . Chekuri CS473ug . → vk is a shortest path from s to vk then dist(s. vi+1 ) for 0 ≤ i < k.

Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths. . . If s = v0 → v1 → v2 → . . → vk is a shortest path from s to vk then for 1 ≤ i < k: s = v0 → v1 → v2 → . . → vi is a shortest path from s to vi Chekuri CS473ug .

. → vk is a shortest path from s to vk then for 1 ≤ i < k: s = v0 → v1 → v2 → . vi ) ≤ dist(s. . . . → vi is a shortest path from s to vi False: dist(s. vk ) for 1 ≤ i < k. If s = v0 → v1 → v2 → . Holds true only for non-negative edge lengths. Chekuri CS473ug .Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths.

→ vi is a shortest path from s to vi False: dist(s. Holds true only for non-negative edge lengths. . . . If s = v0 → v1 → v2 → . . Cannot explore nodes in increasing order of distance! We need a more basic strategy. Chekuri CS473ug . vi ) ≤ dist(s. vk ) for 1 ≤ i < k. → vk is a shortest path from s to vk then for 1 ≤ i < k: s = v0 → v1 → v2 → .Shortest Paths with Negative Lengths Lemma Let G be a directed graph with arbitrary edge lengths.

u). u) ≥ dist(s. ·) values at termination Question: How do we make progress? Chekuri CS473ug . Iteratively reﬁne d(s. ·) values until they reach the correct value dist(s. Hence d(s. u) set to ∞ Maintain the invariant that there is an s → u path of length d(s. u).A Generic Shortest Path Algorithm Start with distance estimate for each node d(s.

v ) is tense if d(s. Chekuri CS473ug . ·) values until they reach the correct value dist(s. v ). u). u) + (u. u). u) for each u ∈ V . Iteratively reﬁne d(s. u) set to ∞ Maintain the invariant that there is an s → u path of length d(s. ·) values at termination Question: How do we make progress? Deﬁnition Given distance estimates d(s.A Generic Shortest Path Algorithm Start with distance estimate for each node d(s. an edge e = (u. u) ≥ dist(s. Hence d(s. v ) > d(s.

u).A Generic Shortest Path Algorithm Start with distance estimate for each node d(s.v) > d(s. u) for each u ∈ V . u) + (u.v)) if (d(s. Relax(e=(u. Hence d(s. ·) values until they reach the correct value dist(s. v )) then d(s. u) ≥ dist(s. an edge e = (u.u) + (u. v ) > d(s. u) set to ∞ Maintain the invariant that there is an s → u path of length d(s. v ).v) = d(s. v ) is tense if d(s.u) + (u. ·) values at termination Question: How do we make progress? Deﬁnition Given distance estimates d(s. v ) Chekuri CS473ug . Iteratively reﬁne d(s. u).

A Generic Shortest Path Algorithm Start with distance estimate for each node d(s.v) = d(s. v ) is tense if d(s.u) + (u. v ) Proposition Relax() maintains the invariant on d(s. Chekuri CS473ug . Hence d(s. u) for each u ∈ V . v ) > d(s. u) ≥ dist(s. v ). ·) values until they reach the correct value dist(s. Iteratively reﬁne d(s. u) values. an edge e = (u.v) > d(s. u) set to ∞ Maintain the invariant that there is an s → u path of length d(s.v)) if (d(s. u) + (u. u).u) + (u. Relax(e=(u. u). v )) then d(s. ·) values at termination Question: How do we make progress? Deﬁnition Given distance estimates d(s.

u) = ∞ While there is a tense edge do Pick an edge e Relax(e) Output d(s.A Generic Shortest Path Algorithm d(s.s) = 0 for each node u = s do d(s.u) values Chekuri CS473ug .

u) for each node u and s cannot reach a negative cycle.u) = ∞ While there is a tense edge do Pick an edge e Relax(e) Output d(s. u) = dist(s. Proof is left as an exercise after seeing future slides.s) = 0 for each node u = s do d(s.A Generic Shortest Path Algorithm d(s. Chekuri CS473ug .u) values Lemma If the algorithm terminates then d(s.

u) = ∞ d(s.s) = 0 S = ∅ While (S = V) do Let v be node in V − S with min d value S = S ∪ {v} For each edeg e in Adj(v) do Relax(e) Chekuri CS473ug .Dijkstra’s Algorithm with Relax() for each node u = s d(s.

Generic Algorithm: Ordering Relax operations d(s.s) = 0 for each node u = s do d(s.u) = ∞ While there is a tense edge do Pick an edge e Relax(e) Output d(s.u) values Question: How do we pick edges to relax? Chekuri CS473ug .

v1 ).. .u) values Question: How do we pick edges to relax? Observation: Suppose s → v1 → . . vk ) = dist(s. Relax(v1 .s) = 0 for each node u = s do d(s. v2 ). vk )! Chekuri CS473ug . vk ) are done in order then d(s. → vk is a shortest path.Generic Algorithm: Ordering Relax operations d(s. . Relax(vk−1 . . If Relax(s. .u) = ∞ While there is a tense edge do Pick an edge e Relax(e) Output d(s.

. . vk ) = dist(s. v1 ). vk )! (Why?) Chekuri CS473ug .Ordering Relax operations Observation: Suppose s → v1 → . vk ) are done in order then d(s. Relax(vk−1 . v2 ). If Relax(s. .. . . → vk is a shortest path. Relax(v1 .

Relax(vk−1 . .Ordering Relax operations Observation: Suppose s → v1 → . → vk is a shortest path. vk ) = dist(s. v1 ). . . .. . vk )! (Why?) We don’t know the shortest paths so how do we know the order to do the Relax operations? Chekuri CS473ug . Relax(v1 . v2 ). vk ) are done in order then d(s. If Relax(s.

vk ). → vk is a shortest path. v2 ) and in iteration k we do Relax(vk−1 . . . v1 ) (and other edges). v1 ). v2 ). vk ) are done in order then d(s. vk ) = dist(s.. Relax(vk−1 . .Ordering Relax operations Observation: Suppose s → v1 → . Relax(v1 . second round Relax(v1 . . If Relax(s. . vk )! (Why?) We don’t know the shortest paths so how do we know the order to do the Relax operations? We don’t! Relax all edges in some arbitrary order Iterate |V | − 1 times First iteration will do Relax(s. Chekuri CS473ug .

u) Chekuri CS473ug .u) = d(s.Bellman-Ford Algorithm for each u ∈ V d(s. v ) do Relax(e) for each u ∈ V dist(s.u) = ∞ d(s.s) = 0 for i = 1 to |V | − 1 do for each edge e = (u.

u) d(s. Q is an empty queue for each u ∈ V d(s.u) for each u ∈ V dist(s.u) = d(s.u) Chekuri CS473ug .s) = 0 for i = 1 to |V | − 1 do for i = 1 to |V| do u = dequeue(Q) for each edge e in Adj(u) do Relax(e) enqueue(Q.u) = ∞ enqueue(Q.Bellman-Ford Algorithm: Scanning Edges One possible way to scan edges in each iteration.

a. Red edges indicate the prev pointers (in reverse) Chekuri CS473ug . d.Example ∞ e −8 d ∞ 0 −3 8 a ∞ 6 0 s 4 3 b ∞ −1 ∞ c a 6 6 0 s 1 5 −3 −3 8 4 3 2 ∞ f d ∞ 0 b 4 −1 3 c a 6 6 0 s −8 1 5 −3 −3 8 4 3 ∞ e 2 ∞ f d ∞ 0 b 4 −1 3 c a 6 6 0 s −8 1 5 −3 −3 8 4 3 ∞ e 2 ∞ f d 4 0 b 4 −1 3 c −8 1 5 −3 ∞ e 2 9 f 11 e −8 d 4 0 −3 8 a 1 6 0 s 4 3 b 2 −1 3 c a 1 6 1 5 −3 −3 8 2 9 f d 4 0 −8 ∞ e 2 1 5 b 2 −1 4 3 0 s 3 c a 1 6 −3 −3 8 9 f d 4 0 −8 ∞ e 2 1 5 b 2 −1 4 3 0 s 3 c a 6 6 −3 −3 8 9 f d 4 0 −8 ∞ e 2 1 5 b 2 −1 4 3 0 s 3 c −3 9 f Figure: One iteration of Bellman-Ford that Relaxes all edges by processing nodes in the order s. e. f . b. c.

Chekuri CS473ug .Example 11 e −8 d 4 0 −3 8 a 1 6 0 s 4 3 b 2 −1 3 c a −1 6 0 s 1 5 −3 −3 8 4 3 2 9 f d 2 0 b 2 −1 3 c a −1 6 0 s −8 1 5 −3 −3 8 4 3 9 e 2 7 f d 1 0 b 2 −1 3 c −8 1 5 −3 9 e 2 7 f 11 e −8 d 1 0 −3 8 a −2 6 0 s 4 3 b 2 −1 3 c a −2 6 1 5 −3 −3 8 2 9 f d 1 0 −8 11 e 2 1 5 b 2 −1 4 3 0 s 3 c a −2 6 −3 −3 8 9 f d 1 0 −8 11 e 2 1 5 b 2 −1 4 3 0 s 3 c −3 9 f Figure: 6 iterations of Bellman-Ford starting with the ﬁrst one from previous slideﬁgure. No changes in 5th iteration and 6th iteration.

Correctness of Bellman-Ford Algorithm Lemma Let G be a directed graph with arbitrary edge lengths. after i iterations of the for loop in the Bellman-Ford algorithm. By induction on i. d(s. v ) Proof. Let v be a node in V such that there is a shortest path in G from s to v with i hops/edges. d(s. Base case: i = 0. s) = 0 and d(s. Then. v ) = dist(s. s) = dist(s. Chekuri CS473ug . s).

v ) Proof. By induction on i. Induction Step: Let s → v1 . s) = dist(s. Let v be a node in V such that there is a shortest path in G from s to v with i hops/edges. s) = 0 and d(s. .Correctness of Bellman-Ford Algorithm Lemma Let G be a directed graph with arbitrary edge lengths. after i iterations of the for loop in the Bellman-Ford algorithm. . d(s. → vi−1 → v be a shortest path from s to v of i hops. v ) = dist(s. s). Base case: i = 0. Chekuri CS473ug . d(s. Then.

after i iterations of the for loop in the Bellman-Ford algorithm. Let v be a node in V such that there is a shortest path in G from s to v with i hops/edges. . vi−1 ) = dist(s.Correctness of Bellman-Ford Algorithm Lemma Let G be a directed graph with arbitrary edge lengths. By induction on i. Induction Step: Let s → v1 . s) = 0 and d(s. vi−1 ) after i − 1 iterations. d(s. v ) Proof. s) = dist(s. s). Then. d(s. vi−1 has a shortest path from s of i − 1 hops or less. → vi−1 → v be a shortest path from s to v of i hops. (Why?). Chekuri CS473ug . v ) = dist(s. Base case: i = 0. . By induction. d(s.

Correctness of Bellman-Ford Algorithm Lemma Let G be a directed graph with arbitrary edge lengths. vi−1 ) = dist(s. Induction Step: Let s → v1 . Then. vi ) sets d(s. vi−1 ) after i − 1 iterations. s) = 0 and d(s. d(s. . s). In iteration i. Base case: i = 0. Let v be a node in V such that there is a shortest path in G from s to v with i hops/edges. d(s. By induction. after i iterations of the for loop in the Bellman-Ford algorithm. vi ). d(s. → vi−1 → v be a shortest path from s to v of i hops. Relax(vi−1 . By induction on i. Chekuri CS473ug . v ) Proof. (Why?). s) = dist(s. v ) = dist(s. vi ) = dist(s. vi−1 has a shortest path from s of i − 1 hops or less. .

Relax(vi−1 . By induction on i. Chekuri CS473ug . Induction Step: Let s → v1 . s). vi−1 ) after i − 1 iterations. (Why?). Then. s) = dist(s. . u). d(s.Correctness of Bellman-Ford Algorithm Lemma Let G be a directed graph with arbitrary edge lengths. after i iterations of the for loop in the Bellman-Ford algorithm. u) once d(s. Note: Relax does not change d(s. v ) = dist(s. vi ) sets d(s. In iteration i. → vi−1 → v be a shortest path from s to v of i hops. u) = dist(s. v ) Proof. vi−1 ) = dist(s. d(s. Base case: i = 0. vi ) = dist(s. vi ). By induction. s) = 0 and d(s. vi−1 has a shortest path from s of i − 1 hops or less. d(s. . Let v be a node in V such that there is a shortest path in G from s to v with i hops/edges.

d(s. Chekuri CS473ug . u) = dist(s.Correctness of Bellman-Ford Algorithm Corollary After |V | − 1 iterations of Bellman-Ford. u) for any node u that has a shortest path from s.

u) is not deﬁned. u) for any node u that has a shortest path from s. Note: If there is a negative cycle C such that s can reach C and C can reach s then dist(s. u) = dist(s.Correctness of Bellman-Ford Algorithm Corollary After |V | − 1 iterations of Bellman-Ford. d(s. Chekuri CS473ug .

Correctness of Bellman-Ford Algorithm Corollary After |V | − 1 iterations of Bellman-Ford. u) = dist(s. Question: How do we know whether there is a negative cycle C reachable from s and whether d(s. d(s. u) for any node u that has a shortest path from s. Note: If there is a negative cycle C such that s can reach C and C can reach s then dist(s. u) is not deﬁned. u) is valid or not? Chekuri CS473ug .

v ) do Relax(e) for each u ∈ V dist(s.u) = ∞ d(s.Bellman-Ford to detect Negative Cycles for each u ∈ V d(s.u) = d(s.s) = 0 for i = 1 to |V | − 1 do for each edge e = (u.v) = −∞ Chekuri CS473ug . v ) do If e=(u.v) is tense then (* s can reach a negative length cycle C and C can reach v *) dist(s.u) for each edge e = (u.

u) after the |V | − 1 iterations. Chekuri CS473ug . Proof Sketch. Therefore d(s. G has no negative length cycle reachable from s implies that all nodes u have a shortest path from s.Correctness Lemma G has a negative cycle reachable from s if and only if there is a tense edge e after |V | − 1 iterations of Bellman-Ford. Therefore. u) = dist(s. there cannot be any tense edges left.

Therefore. See exercise after the generic shortest path algorithm. then d(s. Therefore d(s. If there are no tense edges after |V | − 1 iterations. Proof Sketch.Correctness Lemma G has a negative cycle reachable from s if and only if there is a tense edge e after |V | − 1 iterations of Bellman-Ford. Chekuri CS473ug . u) = dist(s. G has no negative length cycle reachable from s implies that all nodes u have a shortest path from s. u) after the |V | − 1 iterations. u) = dist(s. there cannot be any tense edges left. u) for all nodes.

s) = 0 for i = 1 to |V | − 1 do for each edge e = (u. v ) prev(v) = u Chekuri CS473ug .v) = d(s.v) > d(s.u) + (u. v )) then d(s.u) = ∞ prev(u) = null d(s. v ) do Relax(e) If there is a tense edge e then Output that s can reach a negative cycle C Else for each u ∈ V dist(s.u) = d(s.v)) if (d(s.u) + (u.u) Modiﬁed Relax() Relax(e=(u.Finding the Paths for each u ∈ V d(s.

Negative Cycle Detection Negative Cycle Detection Given directed graph G with arbirary edge lengths. does it have a negative length cycle? Chekuri CS473ug .

does it have a negative length cycle? Bellman-Ford checks whether there is a negative cycle C that is reachable from a speciﬁc vertex s. Run Bellman-Ford |V | times. once from each node u? Chekuri CS473ug . There may negative cycles not reachable from s.Negative Cycle Detection Negative Cycle Detection Given directed graph G with arbirary edge lengths.

Chekuri CS473ug . Bellman-Ford from s will ﬁll ﬁnd a negative length cycle if there is one. Exercise: why does this work? Negative cycle detection can be done with one Bellman-Ford invocation.Negative Cycle Detection Add a new node s and connect it to all nodes of G with zero length edges.

Each Relax() operation is O(1) time. Chekuri CS473ug . Total running time: O(|V ||E |).Running time for Bellman-Ford O(|V |) iterations and O(|E |) Relax() operations in each iteration.

Part III Shortest Paths in DAGs

Chekuri

CS473ug

Shortest Paths in a DAG
Single-Source Shortest Path Problems Input A directed acyclic graph G = (V , E ) with arbitrary (including negative) edge lengths. For edge e = (u, v ), (e) = (u, v ) is its length. Given nodes s, t ﬁnd shortest path from s to t. Given node s ﬁnd shortest path from s to all other nodes.

Chekuri

CS473ug

Shortest Paths in a DAG
Single-Source Shortest Path Problems Input A directed acyclic graph G = (V , E ) with arbitrary (including negative) edge lengths. For edge e = (u, v ), (e) = (u, v ) is its length. Given nodes s, t ﬁnd shortest path from s to t. Given node s ﬁnd shortest path from s to all other nodes. Simpliﬁcation of algorithms for DAGs No cycles and hence no negative length cycles! Hence can ﬁnd shortest paths even for negative length edges Can order nodes using topological sort

Chekuri

CS473ug

v2 . Let s = v1 . Ignore nodes not reachable from s. . vi+1 .Algorithm for DAGs Want to ﬁnd shortest paths from s. vn be a topological sort of G Chekuri CS473ug . . . .

vn be a topological sort of G Observation: shortest path from s to vi cannot use any node from vi+1 . . v2 . . Chekuri CS473ug . . . . Let s = v1 . Ignore nodes not reachable from s. .Algorithm for DAGs Want to ﬁnd shortest paths from s. . . vn can ﬁnd shortest paths in topological sort order. vi+1 .

Algorithm for DAGs for i = 1 to n d(s.s) = 0 for i = 1 to n do for each edge e in Adj(vi ) do Relax(e) return d values Correctness: induction on i and observation in previous slide. Running time: O(m + n) time algorithm! Works for negative edge lengths and hence can ﬁnd longest paths in a DAG Chekuri CS473ug .vi ) = ∞ d(s.