shortest paths with negative edge lengths

Post on 11-Feb-2022

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS 473: Fundamental Algorithms, Spring 2011

Bread First Search, Dijkstra’sAlgorithm for Shortest PathsLecture 4January 27, 2011

Sariel (UIUC) CS473 1 Spring 2011 1 / 40

Part I

Shortest Paths with Negative Length Edges

Sariel (UIUC) CS473 2 Spring 2011 2 / 40

Single-Source Shortest Paths with Negative Edge

Lengths

Single-Source ShortestPath ProblemsInput: A directed graphG = (V,E) with arbitrary(including negative) edgelengths. For edge e = (u, v),`(e) = `(u, v) is its length.

Given nodes s, t findshortest path from s to t.

Given node s find shortestpath from s to all othernodes.

s

2 3

4

5

6

7 t

9

15

6

10

-820

30

18

11

16

-16

19

6

6

44

Sariel (UIUC) CS473 3 Spring 2011 3 / 40

Single-Source Shortest Paths with Negative Edge

Lengths

Single-Source ShortestPath ProblemsInput: A directed graphG = (V,E) with arbitrary(including negative) edgelengths. For edge e = (u, v),`(e) = `(u, v) is its length.

Given nodes s, t findshortest path from s to t.

Given node s find shortestpath from s to all othernodes.

s

2 3

4

5

6

7 t

9

15

6

10

-820

30

18

11

16

-16

19

6

6

44

Sariel (UIUC) CS473 3 Spring 2011 3 / 40

Negative Length Cycles

DefinitionA cycle C is a negative length cycle if the sum of the edge lengths ofC is negative.

s

2 3

4

5

6

7 t

9

15

6

10

-820

30

18

11

16

-16

19

3

6

44

Sariel (UIUC) CS473 4 Spring 2011 4 / 40

Negative Length Cycles

DefinitionA cycle C is a negative length cycle if the sum of the edge lengths ofC is negative.

s

2 3

4

5

6

7 t

9

15

6

10

-820

30

18

11

16

-16

19

3

6

44

Sariel (UIUC) CS473 4 Spring 2011 4 / 40

Negative Length Cycles

DefinitionA cycle C is a negative length cycle if the sum of the edge lengths ofC is negative.

s

2 3

4

5

6

7 t

9

15

6

10

-820

30

18

11

16

-16

19

3

6

44

Sariel (UIUC) CS473 4 Spring 2011 4 / 40

Shortest Paths and Negative Cycles

Given G = (V,E) with edge lengths and s, t. Suppose

G has a negative length cycle C, ands can reach C and C can reach t.

Question: What is the shortest distance from s to t?

Define shortest distance to be undefined, that is −∞, ORDefine shortest distance to be the lenth of a shortest simple pathfrom s to t.

LemmaIf there is an efficient algorithm to find a shortest simple s→ t pathin a graph with negative edge lengths, then there is an efficientalgorithm to find the longest simple s→ t path in a graph withpositive edge lengths.

Finding the s→ t longest path is difficult. NP-Hard!Sariel (UIUC) CS473 5 Spring 2011 5 / 40

Shortest Paths and Negative Cycles

Given G = (V,E) with edge lengths and s, t. Suppose

G has a negative length cycle C, ands can reach C and C can reach t.

Question: What is the shortest distance from s to t?

Define shortest distance to be undefined, that is −∞, ORDefine shortest distance to be the lenth of a shortest simple pathfrom s to t.

LemmaIf there is an efficient algorithm to find a shortest simple s→ t pathin a graph with negative edge lengths, then there is an efficientalgorithm to find the longest simple s→ t path in a graph withpositive edge lengths.

Finding the s→ t longest path is difficult. NP-Hard!Sariel (UIUC) CS473 5 Spring 2011 5 / 40

Shortest Paths and Negative Cycles

Given G = (V,E) with edge lengths and s, t. Suppose

G has a negative length cycle C, ands can reach C and C can reach t.

Question: What is the shortest distance from s to t?

Define shortest distance to be undefined, that is −∞, ORDefine shortest distance to be the lenth of a shortest simple pathfrom s to t.

LemmaIf there is an efficient algorithm to find a shortest simple s→ t pathin a graph with negative edge lengths, then there is an efficientalgorithm to find the longest simple s→ t path in a graph withpositive edge lengths.

Finding the s→ t longest path is difficult. NP-Hard!Sariel (UIUC) CS473 5 Spring 2011 5 / 40

Shortest Paths and Negative Cycles

Given G = (V,E) with edge lengths and s, t. Suppose

G has a negative length cycle C, ands can reach C and C can reach t.

Question: What is the shortest distance from s to t?

Define shortest distance to be undefined, that is −∞, ORDefine shortest distance to be the lenth of a shortest simple pathfrom s to t.

LemmaIf there is an efficient algorithm to find a shortest simple s→ t pathin a graph with negative edge lengths, then there is an efficientalgorithm to find the longest simple s→ t path in a graph withpositive edge lengths.

Finding the s→ t longest path is difficult. NP-Hard!Sariel (UIUC) CS473 5 Spring 2011 5 / 40

Shortest Paths with Negative Edge Lengths:

Problems

Algorithmic Problems

Input A directed graph G = (V,E) with arbitrary (includingnegative) edge lengths. For edge e = (u, v),`(e) = `(u, v) is its length.

Given nodes s, t, either find a negative length cycle C that s canreach or find a shortest path from s to t.

Given node s, either find a negative length cycle C that s canreach or find shortest path distances from s to all reachablenodes.

Check if G has a negative length cycle or not.

Sariel (UIUC) CS473 6 Spring 2011 6 / 40

Shortest Paths with Negative Edge Lengths in

Undirected Graphs

Note: With negative lengths, shortest path problems and negativecycle detection in undirected graphs cannot be reduced to directedgraphs by bi-directing each undirected edge. Why?

Problem can be solved efficiently in undirected graphs but algorithmsare different and more involved than those for directed graphs.Beyond the scope of this class. If interested, ask instructor forreferences.

Sariel (UIUC) CS473 7 Spring 2011 7 / 40

Why Negative Lengths?

Several Applications

Shortest path problems useful in modeling many situations — insome negative lenths are natural

Negative length cycle can be used to find arbitrage opportunitiesin currency trading

Important sub-routine in algorithms for more general problem:minimum-cost flow

Sariel (UIUC) CS473 8 Spring 2011 8 / 40

Application to Currency Trading

Currency Trading

Input n currencies and for each ordered pair (a, b) theexchange rate for converting one unit of a into one unitof b.

Is there an arbitrage opportunity?

Given currencies s, t what is the best way to convert s to t(perhaps via other intermediate currencies)?

Sariel (UIUC) CS473 9 Spring 2011 9 / 40

Reducing Currency Trading to Shortest Paths

Observation: If we convert currency i to j via intermediatecurrencies k1, k2, . . . , kh then one unit of i yieldsexch(i, k1)× exch(k1, k2) . . .× exch(kh, j) units of j.

Create currency trading graph G = (V,E):

For each currency i there is a node vi ∈ VE = V × V: an edge for each pair of currencies

edge length `(vi, vj) = log(exch(i, j)) can be negative

Exercise: Verify that

There is an arbitrage opportunity if and only if G has a negativelength cycle.

The best way to convert currency i to currency j is via a shortestpath in G from i to j. If d is the distance from i to j then oneunit of i can be converted into 2d units of j.

Sariel (UIUC) CS473 10 Spring 2011 10 / 40

Reducing Currency Trading to Shortest Paths

Observation: If we convert currency i to j via intermediatecurrencies k1, k2, . . . , kh then one unit of i yieldsexch(i, k1)× exch(k1, k2) . . .× exch(kh, j) units of j.

Create currency trading graph G = (V,E):

For each currency i there is a node vi ∈ VE = V × V: an edge for each pair of currencies

edge length `(vi, vj) = log(exch(i, j)) can be negative

Exercise: Verify that

There is an arbitrage opportunity if and only if G has a negativelength cycle.

The best way to convert currency i to currency j is via a shortestpath in G from i to j. If d is the distance from i to j then oneunit of i can be converted into 2d units of j.

Sariel (UIUC) CS473 10 Spring 2011 10 / 40

Reducing Currency Trading to Shortest Paths

Observation: If we convert currency i to j via intermediatecurrencies k1, k2, . . . , kh then one unit of i yieldsexch(i, k1)× exch(k1, k2) . . .× exch(kh, j) units of j.

Create currency trading graph G = (V,E):

For each currency i there is a node vi ∈ VE = V × V: an edge for each pair of currencies

edge length `(vi, vj) = log(exch(i, j)) can be negative

Exercise: Verify that

There is an arbitrage opportunity if and only if G has a negativelength cycle.

The best way to convert currency i to currency j is via a shortestpath in G from i to j. If d is the distance from i to j then oneunit of i can be converted into 2d units of j.

Sariel (UIUC) CS473 10 Spring 2011 10 / 40

Reducing Currency Trading to Shortest Paths

Observation: If we convert currency i to j via intermediatecurrencies k1, k2, . . . , kh then one unit of i yieldsexch(i, k1)× exch(k1, k2) . . .× exch(kh, j) units of j.

Create currency trading graph G = (V,E):

For each currency i there is a node vi ∈ VE = V × V: an edge for each pair of currencies

edge length `(vi, vj) = log(exch(i, j)) can be negative

Exercise: Verify that

There is an arbitrage opportunity if and only if G has a negativelength cycle.

The best way to convert currency i to currency j is via a shortestpath in G from i to j. If d is the distance from i to j then oneunit of i can be converted into 2d units of j.

Sariel (UIUC) CS473 10 Spring 2011 10 / 40

Shortest Paths with Negative Edge Lengths:

Problems

Algorithmic Problems

Input A directed graph G = (V,E) with arbitrary (includingnegative) edge lengths. For edge e = (u, v),`(e) = `(u, v) is its length.

Given nodes s, t, either find a negative length cycle C that s canreach or find a shortest path from s to t.

Given node s, either find a negative length cycle C that s canreach or find shortest path distances from s to all reachablenodes.

Check if G has a negative length cycle or not.

Sariel (UIUC) CS473 11 Spring 2011 11 / 40

Dijkstra’s Algorithm and Negative Lengths

ObservationWith negative cost edges, Dijkstra’s algorithm fails

s

u

t

v

2

1

3

-6

s

u

1

v

1

False assumption: Dijkstra’s algorithm is based on the assumptionthat if s = v0 → v1 → v2 . . .→ vk is a shortest path from s to vkthen dist(s, vi) ≤ dist(s, vi+1) for 0 ≤ i < k. Holds true only fornon-negative edge lengths.

Sariel (UIUC) CS473 12 Spring 2011 12 / 40

Dijkstra’s Algorithm and Negative Lengths

ObservationWith negative cost edges, Dijkstra’s algorithm fails

s

u

t

v

2

1

3

-6

s

u

1

v

1

False assumption: Dijkstra’s algorithm is based on the assumptionthat if s = v0 → v1 → v2 . . .→ vk is a shortest path from s to vkthen dist(s, vi) ≤ dist(s, vi+1) for 0 ≤ i < k. Holds true only fornon-negative edge lengths.

Sariel (UIUC) CS473 12 Spring 2011 12 / 40

Dijkstra’s Algorithm and Negative Lengths

ObservationWith negative cost edges, Dijkstra’s algorithm fails

s

u

t

v

2

1

3

-6

s

u

1

v

1

False assumption: Dijkstra’s algorithm is based on the assumptionthat if s = v0 → v1 → v2 . . .→ vk is a shortest path from s to vkthen dist(s, vi) ≤ dist(s, vi+1) for 0 ≤ i < k. Holds true only fornon-negative edge lengths.

Sariel (UIUC) CS473 12 Spring 2011 12 / 40

Shortest Paths with Negative Lengths

LemmaLet G be a directed graph with arbitrary edge lengths. Ifs = v0 → v1 → v2 → . . .→ vk is a shortest path from s to vkthen for 1 ≤ i < k:

s = v0 → v1 → v2 → . . .→ vi is a shortest path from s to vi

False: dist(s, vi) ≤ dist(s, vk) for 1 ≤ i < k. Holds true onlyfor non-negative edge lengths.

Cannot explore nodes in increasing order of distance! We need amore basic strategy.

Sariel (UIUC) CS473 13 Spring 2011 13 / 40

Shortest Paths with Negative Lengths

LemmaLet G be a directed graph with arbitrary edge lengths. Ifs = v0 → v1 → v2 → . . .→ vk is a shortest path from s to vkthen for 1 ≤ i < k:

s = v0 → v1 → v2 → . . .→ vi is a shortest path from s to vi

False: dist(s, vi) ≤ dist(s, vk) for 1 ≤ i < k. Holds true onlyfor non-negative edge lengths.

Cannot explore nodes in increasing order of distance! We need amore basic strategy.

Sariel (UIUC) CS473 13 Spring 2011 13 / 40

Shortest Paths with Negative Lengths

LemmaLet G be a directed graph with arbitrary edge lengths. Ifs = v0 → v1 → v2 → . . .→ vk is a shortest path from s to vkthen for 1 ≤ i < k:

s = v0 → v1 → v2 → . . .→ vi is a shortest path from s to vi

False: dist(s, vi) ≤ dist(s, vk) for 1 ≤ i < k. Holds true onlyfor non-negative edge lengths.

Cannot explore nodes in increasing order of distance! We need amore basic strategy.

Sariel (UIUC) CS473 13 Spring 2011 13 / 40

A Generic Shortest Path Algorithm

Start with distance estimate for each node d(s, u) set to∞Maintain the invariant that there is an s→ u path of lengthd(s, u). Hence d(s, u) ≥ dist(s, u).Iteratively refine d(s, ·) values until they reach the correct valuedist(s, ·) values at termination

Question: How do we make progress?

DefinitionGiven distance estimates d(s, u) for each u ∈ V, an edge e = (u, v)is tense if d(s, v) > d(s, u) + `(u, v).

Relax(e=(u,v))

if (d(s,v) > d(s,u) + `(u, v)) then

d(s,v) = d(s,u) + `(u, v)

Proposition

Relax() maintains the invariant on d(s, u) values.Sariel (UIUC) CS473 14 Spring 2011 14 / 40

A Generic Shortest Path Algorithm

Start with distance estimate for each node d(s, u) set to∞Maintain the invariant that there is an s→ u path of lengthd(s, u). Hence d(s, u) ≥ dist(s, u).Iteratively refine d(s, ·) values until they reach the correct valuedist(s, ·) values at termination

Question: How do we make progress?

DefinitionGiven distance estimates d(s, u) for each u ∈ V, an edge e = (u, v)is tense if d(s, v) > d(s, u) + `(u, v).

Relax(e=(u,v))

if (d(s,v) > d(s,u) + `(u, v)) then

d(s,v) = d(s,u) + `(u, v)

Proposition

Relax() maintains the invariant on d(s, u) values.Sariel (UIUC) CS473 14 Spring 2011 14 / 40

A Generic Shortest Path Algorithm

Start with distance estimate for each node d(s, u) set to∞Maintain the invariant that there is an s→ u path of lengthd(s, u). Hence d(s, u) ≥ dist(s, u).Iteratively refine d(s, ·) values until they reach the correct valuedist(s, ·) values at termination

Question: How do we make progress?

DefinitionGiven distance estimates d(s, u) for each u ∈ V, an edge e = (u, v)is tense if d(s, v) > d(s, u) + `(u, v).

Relax(e=(u,v))

if (d(s,v) > d(s,u) + `(u, v)) then

d(s,v) = d(s,u) + `(u, v)

Proposition

Relax() maintains the invariant on d(s, u) values.Sariel (UIUC) CS473 14 Spring 2011 14 / 40

A Generic Shortest Path Algorithm

Start with distance estimate for each node d(s, u) set to∞Maintain the invariant that there is an s→ u path of lengthd(s, u). Hence d(s, u) ≥ dist(s, u).Iteratively refine d(s, ·) values until they reach the correct valuedist(s, ·) values at termination

Question: How do we make progress?

DefinitionGiven distance estimates d(s, u) for each u ∈ V, an edge e = (u, v)is tense if d(s, v) > d(s, u) + `(u, v).

Relax(e=(u,v))

if (d(s,v) > d(s,u) + `(u, v)) then

d(s,v) = d(s,u) + `(u, v)

Proposition

Relax() maintains the invariant on d(s, u) values.Sariel (UIUC) CS473 14 Spring 2011 14 / 40

A Generic Shortest Path Algorithm

d(s,s) = 0

for each node u 6= s do

d(s,u) = ∞

While there is a tense edge do

Pick a tense edge eRelax(e)

Output d(s,u) values

Technical assumption: If e = (u, v) is an edge andd(s, u) = d(s, v) =∞ then edge is not tense.

Sariel (UIUC) CS473 15 Spring 2011 15 / 40

Properties of the generic algorithm

Proposition

If u is not reachable from s then d(s, u) remains at∞ throughoutthe algorithm.

PropositionIf a negative length cycle C is reachable by s then there is always atense edge and hence the algorithm never terminates.

CorollaryIf the algorithm terminates then there is no negative length cycle Cthat is rechable from s.

LemmaIf the algorithm terminates then d(s, u) = dist(s, u) for each node u(and s cannot reach a negative cycle).

Proof of last lemma; see future slides.

Sariel (UIUC) CS473 16 Spring 2011 16 / 40

Dijkstra’s Algorithm with Relax()

for each node u 6= s

d(s,u) = ∞d(s,s) = 0

S = ∅While (S 6= 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)

Sariel (UIUC) CS473 17 Spring 2011 17 / 40

Generic Algorithm: Ordering Relax operations

d(s,s) = 0

for each node u 6= s do

d(s,u) = ∞

While there is a tense edge do

Pick a tense edge eRelax(e)

Output d(s,u) values

Question: How do we pick edges to relax?

Observation: Suppose s→ v1 → . . .→ vk is a shortest path.

If Relax(s, v1), Relax(v1, v2), . . ., Relax(vk−1, vk) are done in orderthen d(s, vk) = dist(s, vk)!

Sariel (UIUC) CS473 18 Spring 2011 18 / 40

Generic Algorithm: Ordering Relax operations

d(s,s) = 0

for each node u 6= s do

d(s,u) = ∞

While there is a tense edge do

Pick a tense edge eRelax(e)

Output d(s,u) values

Question: How do we pick edges to relax?

Observation: Suppose s→ v1 → . . .→ vk is a shortest path.

If Relax(s, v1), Relax(v1, v2), . . ., Relax(vk−1, vk) are done in orderthen d(s, vk) = dist(s, vk)!

Sariel (UIUC) CS473 18 Spring 2011 18 / 40

Ordering Relax operations

Observation: Suppose s→ v1 → . . .→ vk is a shortest path.

If Relax(s, v1), Relax(v1, v2), . . ., Relax(vk−1, vk) are done in orderthen d(s, vk) = dist(s, vk)! (Why?)

We don’t know the shortest paths so how do we know the order todo the Relax operations?

We don’t!

Relax all edges (even those not tense) in some arbitrary order

Iterate |V| − 1 times

First iteration will do Relax(s, v1) (and other edges), secondround Relax(v1, v2) and in iteration k we do Relax(vk−1, vk).

Sariel (UIUC) CS473 19 Spring 2011 19 / 40

Ordering Relax operations

Observation: Suppose s→ v1 → . . .→ vk is a shortest path.

If Relax(s, v1), Relax(v1, v2), . . ., Relax(vk−1, vk) are done in orderthen d(s, vk) = dist(s, vk)! (Why?)

We don’t know the shortest paths so how do we know the order todo the Relax operations?

We don’t!

Relax all edges (even those not tense) in some arbitrary order

Iterate |V| − 1 times

First iteration will do Relax(s, v1) (and other edges), secondround Relax(v1, v2) and in iteration k we do Relax(vk−1, vk).

Sariel (UIUC) CS473 19 Spring 2011 19 / 40

Ordering Relax operations

Observation: Suppose s→ v1 → . . .→ vk is a shortest path.

If Relax(s, v1), Relax(v1, v2), . . ., Relax(vk−1, vk) are done in orderthen d(s, vk) = dist(s, vk)! (Why?)

We don’t know the shortest paths so how do we know the order todo the Relax operations?

We don’t!

Relax all edges (even those not tense) in some arbitrary order

Iterate |V| − 1 times

First iteration will do Relax(s, v1) (and other edges), secondround Relax(v1, v2) and in iteration k we do Relax(vk−1, vk).

Sariel (UIUC) CS473 19 Spring 2011 19 / 40

Bellman-Ford Algorithm

for each u ∈ Vd(s,u) = ∞

d(s,s) = 0

for i = 1 to |V| − 1 do

for each edge e = (u, v) do

Relax(e)

for each u ∈ Vdist(s,u) = d(s,u)

Sariel (UIUC) CS473 20 Spring 2011 20 / 40

Bellman-Ford Algorithm: Scanning Edges

One possible way to scan edges in each iteration.

Q is an empty queue

for each u ∈ Vd(s,u) = ∞enqueue(Q, u)

d(s,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)

for each u ∈ Vdist(s,u) = d(s,u)

Sariel (UIUC) CS473 21 Spring 2011 21 / 40

Example

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

∞ ∞

∞ ∞

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

6 3

∞ ∞

4

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

6 3

4 9

2

a

d

−3

s

c

b

f

e

6 3

4

−1−3

0 5

8

−8 2

1

0

6 3

4 9

4

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

6 3

∞ ∞

4

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

1 3

4 9

2

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

1 3

4 9

2

s

a c

b

d f

e

6 3

4

−1−3

0 5

8

−3

−8 2

1

0

1 3

4 9

2

11

Figure: One iteration of Bellman-Ford that Relaxes all edges by processingnodes in the order s, a, b, c, d, e, f. Red edges indicate the prev pointers(in reverse)

Sariel (UIUC) CS473 22 Spring 2011 22 / 40

Example

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

1 3

4 9

2

11

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

−1 3

2 7

2

9

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

−1 3

1 7

2

9

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

−2 3

1 7

2

9

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

−2 3

1 7

2

9

s

a c

b

d f

e

6 3

4

−1

−3

0 5

8

−3

−8 2

1

0

−2 3

1 7

2

9

Figure: 6 iterations of Bellman-Ford starting with the first one fromprevious slidefigure. No changes in 5th iteration and 6th iteration.

Sariel (UIUC) CS473 23 Spring 2011 23 / 40

Correctness of Bellman-Ford Algorithm

LemmaLet G be a directed graph with arbitrary edge lengths. Let v be anode in V such that there is a shortest path in G from s to v with ihops/edges. Then, after i iterations of the for loop in theBellman-Ford algorithm, d(s, v) = dist(s, v)

Proof.By induction on i.

Base case: i = 0. d(s, s) = 0 and d(s, s) = dist(s, s).

Induction Step: Let s→ v1 . . .→ vi−1 → v be a shortest pathfrom s to v of i hops.

vi−1 has a shortest path from s of i− 1 hops or less. (Why?).By induction, d(s, vi−1) = dist(s, vi−1) after i− 1 iterations.In iteration i, Relax(vi−1, vi) sets d(s, vi) = dist(s, vi).Note: Relax does not change d(s, u) once d(s, u) = dist(s, u).

Sariel (UIUC) CS473 24 Spring 2011 24 / 40

Correctness of Bellman-Ford Algorithm

LemmaLet G be a directed graph with arbitrary edge lengths. Let v be anode in V such that there is a shortest path in G from s to v with ihops/edges. Then, after i iterations of the for loop in theBellman-Ford algorithm, d(s, v) = dist(s, v)

Proof.By induction on i.

Base case: i = 0. d(s, s) = 0 and d(s, s) = dist(s, s).

Induction Step: Let s→ v1 . . .→ vi−1 → v be a shortest pathfrom s to v of i hops.

vi−1 has a shortest path from s of i− 1 hops or less. (Why?).By induction, d(s, vi−1) = dist(s, vi−1) after i− 1 iterations.In iteration i, Relax(vi−1, vi) sets d(s, vi) = dist(s, vi).Note: Relax does not change d(s, u) once d(s, u) = dist(s, u).

Sariel (UIUC) CS473 24 Spring 2011 24 / 40

Correctness of Bellman-Ford Algorithm

LemmaLet G be a directed graph with arbitrary edge lengths. Let v be anode in V such that there is a shortest path in G from s to v with ihops/edges. Then, after i iterations of the for loop in theBellman-Ford algorithm, d(s, v) = dist(s, v)

Proof.By induction on i.

Base case: i = 0. d(s, s) = 0 and d(s, s) = dist(s, s).

Induction Step: Let s→ v1 . . .→ vi−1 → v be a shortest pathfrom s to v of i hops.

vi−1 has a shortest path from s of i− 1 hops or less. (Why?).By induction, d(s, vi−1) = dist(s, vi−1) after i− 1 iterations.In iteration i, Relax(vi−1, vi) sets d(s, vi) = dist(s, vi).Note: Relax does not change d(s, u) once d(s, u) = dist(s, u).

Sariel (UIUC) CS473 24 Spring 2011 24 / 40

Correctness of Bellman-Ford Algorithm

LemmaLet G be a directed graph with arbitrary edge lengths. Let v be anode in V such that there is a shortest path in G from s to v with ihops/edges. Then, after i iterations of the for loop in theBellman-Ford algorithm, d(s, v) = dist(s, v)

Proof.By induction on i.

Base case: i = 0. d(s, s) = 0 and d(s, s) = dist(s, s).

Induction Step: Let s→ v1 . . .→ vi−1 → v be a shortest pathfrom s to v of i hops.

vi−1 has a shortest path from s of i− 1 hops or less. (Why?).By induction, d(s, vi−1) = dist(s, vi−1) after i− 1 iterations.In iteration i, Relax(vi−1, vi) sets d(s, vi) = dist(s, vi).Note: Relax does not change d(s, u) once d(s, u) = dist(s, u).

Sariel (UIUC) CS473 24 Spring 2011 24 / 40

Correctness of Bellman-Ford Algorithm

LemmaLet G be a directed graph with arbitrary edge lengths. Let v be anode in V such that there is a shortest path in G from s to v with ihops/edges. Then, after i iterations of the for loop in theBellman-Ford algorithm, d(s, v) = dist(s, v)

Proof.By induction on i.

Base case: i = 0. d(s, s) = 0 and d(s, s) = dist(s, s).

Induction Step: Let s→ v1 . . .→ vi−1 → v be a shortest pathfrom s to v of i hops.

vi−1 has a shortest path from s of i− 1 hops or less. (Why?).By induction, d(s, vi−1) = dist(s, vi−1) after i− 1 iterations.In iteration i, Relax(vi−1, vi) sets d(s, vi) = dist(s, vi).Note: Relax does not change d(s, u) once d(s, u) = dist(s, u).

Sariel (UIUC) CS473 24 Spring 2011 24 / 40

Correctness of Bellman-Ford Algorithm

Corollary

After |V| − 1 iterations of Bellman-Ford, d(s, u) = dist(s, u) forany node u that has a shortest path from s.

Note: If there is a negative cycle C such that s can reach C then wedo not know whether d(s, u) = dist(s, u) or not even if dist(s, u) iswell-defined.

Question: How do we know whether there is a negative cycle Creachable from s?

Sariel (UIUC) CS473 25 Spring 2011 25 / 40

Correctness of Bellman-Ford Algorithm

Corollary

After |V| − 1 iterations of Bellman-Ford, d(s, u) = dist(s, u) forany node u that has a shortest path from s.

Note: If there is a negative cycle C such that s can reach C then wedo not know whether d(s, u) = dist(s, u) or not even if dist(s, u) iswell-defined.

Question: How do we know whether there is a negative cycle Creachable from s?

Sariel (UIUC) CS473 25 Spring 2011 25 / 40

Correctness of Bellman-Ford Algorithm

Corollary

After |V| − 1 iterations of Bellman-Ford, d(s, u) = dist(s, u) forany node u that has a shortest path from s.

Note: If there is a negative cycle C such that s can reach C then wedo not know whether d(s, u) = dist(s, u) or not even if dist(s, u) iswell-defined.

Question: How do we know whether there is a negative cycle Creachable from s?

Sariel (UIUC) CS473 25 Spring 2011 25 / 40

Bellman-Ford to detect Negative Cycles

for each u ∈ Vd(s,u) = ∞

d(s,s) = 0

for i = 1 to |V| − 1 do

for each edge e = (u, v) do

Relax(e)

for each edge e = (u, v) do

If e=(u,v) is tense then

Stop and output that s can reach a negative length cycle

Output for each u ∈ V dist(s,u) as d(s,u)

Sariel (UIUC) CS473 26 Spring 2011 26 / 40

Correctness

LemmaG has a negative cycle reachable from s if and only if there is a tenseedge e after |V| − 1 iterations of Bellman-Ford.

Proof Sketch.G has no negative length cycle reachable from s implies that allnodes u have a shortest path from s. Therefore d(s, u) = dist(s, u)after the |V| − 1 iterations. Therefore, there cannot be any tenseedges left.

If there is a negative cycle C then there is a tense edge after |V| − 1(in fact any number of) iterations. See Proposition (proof is exercise)about properties of the generic shortest path algorithm.

Sariel (UIUC) CS473 27 Spring 2011 27 / 40

Correctness

LemmaG has a negative cycle reachable from s if and only if there is a tenseedge e after |V| − 1 iterations of Bellman-Ford.

Proof Sketch.G has no negative length cycle reachable from s implies that allnodes u have a shortest path from s. Therefore d(s, u) = dist(s, u)after the |V| − 1 iterations. Therefore, there cannot be any tenseedges left.

If there is a negative cycle C then there is a tense edge after |V| − 1(in fact any number of) iterations. See Proposition (proof is exercise)about properties of the generic shortest path algorithm.

Sariel (UIUC) CS473 27 Spring 2011 27 / 40

Finding the Paths and a Shortest Path Tree

for each u ∈ Vd(s,u) = ∞prev(u) = null

endfor

d(s,s) = 0

for i = 1 to |V| − 1 do

for each edge e = (u, v) do

Relax(e)

endfor

If there is a tense edge e then

Output that s can reach a negative cycle C

Else

for each u ∈ Vdist(s,u) = d(s,u)

Relax(e=(u,v))

if (d(s,v) > d(s,u) + `(u, v)) then

d(s,v) = d(s,u) + `(u, v)prev(v) = u

Note: prev pointers induce a shortest path tree.Sariel (UIUC) CS473 28 Spring 2011 28 / 40

Negative Cycle Detection

Negative Cycle DetectionGiven directed graph G with arbirary edge lengths, does it have anegative length cycle?

Bellman-Ford checks whether there is a negative cycle C that isreachable from a specific vertex s. There may negative cyclesnot reachable from s.

Run Bellman-Ford |V| times, once from each node u?

Sariel (UIUC) CS473 29 Spring 2011 29 / 40

Negative Cycle Detection

Negative Cycle DetectionGiven directed graph G with arbirary edge lengths, does it have anegative length cycle?

Bellman-Ford checks whether there is a negative cycle C that isreachable from a specific vertex s. There may negative cyclesnot reachable from s.

Run Bellman-Ford |V| times, once from each node u?

Sariel (UIUC) CS473 29 Spring 2011 29 / 40

Negative Cycle Detection

Add a new node s′ and connect it to all nodes of G with zerolength edges. Bellman-Ford from s′ will fill find a negativelength cycle if there is one. Exercise: why does this work?

Negative cycle detection can be done with one Bellman-Fordinvocation.

Sariel (UIUC) CS473 30 Spring 2011 30 / 40

Running time for Bellman-Ford

Input graph G = (V,E) with m = |E| and n = |V|.n outer iterations and m Relax() operations in each iteration.Each Relax() operation is O(1) time.

Total running time: O(mn).

Sariel (UIUC) CS473 31 Spring 2011 31 / 40

Part II

Shortest Paths in DAGs

Sariel (UIUC) CS473 32 Spring 2011 32 / 40

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 find shortest path from s to t.

Given node s find shortest path from s to all other nodes.

Simplification of algorithms for DAGs

No cycles and hence no negative length cycles! Hence can findshortest paths even for negative length edges

Can order nodes using topological sort

Sariel (UIUC) CS473 33 Spring 2011 33 / 40

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 find shortest path from s to t.

Given node s find shortest path from s to all other nodes.

Simplification of algorithms for DAGs

No cycles and hence no negative length cycles! Hence can findshortest paths even for negative length edges

Can order nodes using topological sort

Sariel (UIUC) CS473 33 Spring 2011 33 / 40

Algorithm for DAGs

Want to find shortest paths from s. Ignore nodes not reachablefrom s.

Let s = v1, v2, vi+1, . . . , vn be a topological sort of G

Observation:

shortest path from s to vi cannot use any node fromvi+1, . . . , vn

can find shortest paths in topological sort order.

Sariel (UIUC) CS473 34 Spring 2011 34 / 40

Algorithm for DAGs

Want to find shortest paths from s. Ignore nodes not reachablefrom s.

Let s = v1, v2, vi+1, . . . , vn be a topological sort of G

Observation:

shortest path from s to vi cannot use any node fromvi+1, . . . , vn

can find shortest paths in topological sort order.

Sariel (UIUC) CS473 34 Spring 2011 34 / 40

Algorithm for DAGs

for i = 1 to nd(s,vi) = ∞

d(s,s) = 0

for i = 1 to n-1 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 edgelengths and hence can find longest paths in a DAG

Sariel (UIUC) CS473 35 Spring 2011 35 / 40

Takeaway Points

Shortest paths with potentially negative length edges arise in avariety of applications. Longest simple path problem is difficult(no known efficient algorithm and NP-Hard). We restrictattention to shortest walks and they are well defined only if thereare no negative length cycles reachable from the source.

A generic shortest path algorithm starts with distance estimatesto the source and iteratively refines them by considering edgesone at a time. The algorithm is guaranteed to terminate withcorrect distances if there are no negative length cycle. If anegative length cycle is reachable from the source it isguaranteed not to terminate.

Dijkstra’s algorithm can also be thought of as an instantiation ofthe generic algorithm.

Sariel (UIUC) CS473 36 Spring 2011 36 / 40

Points continued

Bellman-Ford algorithm is an instantiation of the genericalgorithm that in each iteration relaxes all the edges. Itrecognizes negative length cycles if there is a tense edges in then’th iteration. For a vertex u with a shortest path to the sourcewith i edges the algorithm has the correct distance after iiterations. Running time of Bellman-Ford algorithm is O(nm).

Bellman-Ford can be adapted to find a negative length cycle inthe graph by adding a new vertex.

If we have a DAG then it has no negative length cycle and henceshortest paths exists even with negative lengths. One cancompute single-source shortest paths in a DAG in linear time.This implies that one can also compute longest paths in a DAGin linear time.

Sariel (UIUC) CS473 37 Spring 2011 37 / 40

Notes

Sariel (UIUC) CS473 38 Spring 2011 38 / 40

Notes

Sariel (UIUC) CS473 39 Spring 2011 39 / 40

Notes

Sariel (UIUC) CS473 40 Spring 2011 40 / 40

Notes

Sariel (UIUC) CS473 41 Spring 2011 41 / 40

Notes

Sariel (UIUC) CS473 41 Spring 2011 41 / 40

top related