cs3000: algorithms & data paul handkhoury.northeastern.edu/.../day19.pdf · cs3000: algorithms...

20
CS3000: Algorithms & Data Paul Hand Lecture 18: Bellman-Ford Algorithm Apr 1, 2019

Upload: others

Post on 19-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

CS3000: Algorithms & DataPaul Hand

Lecture 18:

• Bellman-Ford Algorithm

Apr 1, 2019

Page 2: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Dijkstra Recap

• Input: Directed, weighted graph ! = #, %, ℓ' , source node (• Non-negative edge lengths ℓ' ≥ 0

• Output: Two arrays +, ,• +[.] is the length of the shortest ( ↝ . path• ,[.] is the final hop on shortest ( ↝ . path

• Running time: 1 2 log 6• Implement using binary heaps

Page 3: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

What About Negative Lengths?

• Models various phenomena• Transactions (credits and debits)• Currency exchange (log(exchange rate) can be + or -)• Chemical reactions (can be exo or endothermic)

• Leads to interesting algorithms• Variants of Bellman-Ford are used in internet routing

Page 4: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Bellman-Ford

• Input: Directed, weighted graph ! = #, %, ℓ' , source node (• Possibly negative edge lengths ℓ' ∈ ℝ• No negative-length cycles!

• Output: Two arrays +, ,• +[.] is the length of the shortest ( ↝ . path• ,[.] is the final hop on shortest ( ↝ . path

Page 5: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Activity

• Suppose we try the following algorithm• Take a graph ! = #, %, ℓ ' with negative lengths• Add some valueC to all lengths to make them non-negative• Run Dijkstra on the new graph

• Activity: Come up with a graph where this fails

Page 6: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Structure of Shortest Paths

• If !, # ∈ %, then & ', # ≤ & ', ! + ℓ !, # for every node ' ∈ +

• For every #, there exists an edge !, # ∈ % such that & ', # = & ', ! + ℓ !, #

• If !, # ∈ %, and & ', # = & ', ! + ℓ(!, #) then there is a shortest ' ↝ #-path ending with (!, #)

Page 7: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Dynamic Programming

• Subproblems: Let OPT $ be the length of the shortest path from % to $

Page 8: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Bottom-Up Implementation?

s

b

e

c d

-1

4

12

-3

2

5

3

v s b c d eOPT(v) 0

Page 9: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Dynamic Programming Take II

• Subproblems: Let OPT $, & be the length of the shortest path from ' to $ with at most & hops

Page 10: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Recurrence

• Subproblems: Let OPT $, & be the length of the shortest path from ' to $ with at most & hops • Case (: (*, $) is final edge on the shortest &-hop ' ↝ $ path

OPT $, & = min OPT $, 1 − 1 , min(4,5)∈7 OPT *, 1 − 1 + ℓ4,5

OPT $, 0 = ∞ for every $OPT ', & = 0 for every &

Recurrence:

Page 11: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Finding the paths

• OPT $, & is the length of the shortest ' ↝ $ path with at most & hops• ) $, & is the last hop on some shortest ' ↝ $ path with at

most & hops

OPT $, & = min OPT $, . − 1 , min(2,3)∈6 OPT 7, . − 1 + ℓ2,3Recurrence:

Page 12: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Example

s

b

e

c d

-1

4

12

-3

2

5

3

0 1 2 3 4

s 0 0 0 0 0

b ∞c ∞d ∞e ∞

0

∞ ∞

Page 13: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Example

s

b

e

c d

-1

4

12

-3

2

5

3

0 1 2 3 4

s 0 0 0 0 0

b ∞ -1

c ∞ 4

d ∞ ∞e ∞ ∞

0

-1

4 ∞

Page 14: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Example

s

b

e

c d

-1

4

12

-3

2

5

3

0 1 2 3 4

s 0 0 0 0 0

b ∞ -1 -1

c ∞ 4 2

d ∞ ∞ 1

e ∞ ∞ 1

0

-1

2 1

1

Page 15: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Example

s

b

e

c d

-1

4

12

-3

2

5

3

0 1 2 3 4

s 0 0 0 0 0

b ∞ -1 -1 -1

c ∞ 4 2 2

d ∞ ∞ 1 -2

e ∞ ∞ 1 1

0

-1

2 -2

1

Page 16: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Example

s

b

e

c d

-1

4

12

-3

2

5

3

0 1 2 3 4

s 0 0 0 0 0

b ∞ -1 -1 -1 -1

c ∞ 4 2 2 2

d ∞ ∞ 1 -2 -2

e ∞ ∞ 1 1 1

0

-1

2 -2

1

Page 17: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Implementation (Bottom Up DP)Shortest-Path(G, s)

foreach node v Î VD[v,0] ¬ ¥P[v,0] ¬ ⊥

D[s,0] ¬ 0

for i = 1 to n-1foreach node v Î VD[v,i] ¬ D[v,i-1]P[v,i] ¬ P[v,i-1]foreach edge (u,v) Î E

if (D[u,i-1] + ℓuv < D[v,i]) D[v,i] ¬ D[u,i-1] + ℓuvP[v,i] ¬ u

Running time: Space:

Page 18: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Optimizations

• One array ! " containing shortest path found so far• No need to check edges #, " unless ! # has changed• Stop if no ! " has changed for a full pass through %

Page 19: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Implementation IIEfficient-Shortest-Path(G, s)

foreach node v Î VD[v] ¬ ¥P[v] ¬ ⊥

D[s] ¬ 0

for i = 1 to nforeach node u Î Vif (D[u] changed in the last iteration)

foreach edge (u,v) Î Eif (D[u] + ℓuv < D[v])

D[v] ¬ D[u] + ℓuvP[v] ¬ u

if (no D[u] changed): return (D,P)

Running time: O(mn) but O(m) in practiceSpace: O(n)

Page 20: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/.../day19.pdf · CS3000: Algorithms & Data Paul Hand Lecture 18: •Bellman-Ford Algorithm Apr 1, 2019

Shortest Paths Summary

• Input: Directed, weighted graph ! = #, %, ℓ' , source node (• Output: Two arrays ), *• )[,] is the length of the shortest ( ↝ , path• *[,] is the final hop on shortest ( ↝ , path

• Non-negative lengths: Dijkstra’s Algorithm solves in /(1 log 5) time• Negative lengths: Bellman-Ford solves in / 51

time / 5 +1 space, or finds a negative cycle