dynamic programming ii - technical university of denmark · dynamic programming ii inge li gørtz...

41
Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1

Upload: others

Post on 15-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Dynamic Programming IIInge Li Gørtz

KT section 6.6 and 6.8Thank you to Kevin Wayne for inspiration to slides �1

Page 2: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Optimal substructure• Last time

• Weighted interval scheduling• Segmented least squares

• Today • Sequence alignment• Shortest paths with negative weights

Dynamic Programming

�2

Page 3: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence Alignment

�3

Page 4: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

A C A A G T C - C A T G T -

• How similar are ACAAGTC and CATGT.• Align them such that

• all items occurs in at most one pair. • no crossing pairs.

• Cost of alignment• gap penalty δ• mismatch cost for each pair of letters α(p,q).

• Goal: find minimum cost alignment.

Sequence alignment

A C A A - G T C - C A - T G T -

1 mismatch, 2 gaps 0 mismatches, 4 gaps

A C A A G T C - C A T G T - A C A A G T C - C A T G T -

�4

Page 5: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Subproblem property.

• SA(Xi,Yj) = min cost of aligning strings X[1…i] and Y[1…j].

• Case 1. Align xi and yj.• Pay mismatch cost for xi and yj + min cost of aligning Xi-1 and Yj-1.

• Case 2. Leave xi unaligned.• Pay gap cost + min cost of aligning Xi-1 and Yj.

• Case 3. Leave yj unaligned.• Pay gap cost + min cost of aligning Xi and Yj-1.

Sequence Alignment

xiXi-1

yjYj-1

�5

Page 6: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C

CATGT

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

SA(X5, Y3)

�6

Page 7: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C

CATGT

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

SA(X5, Y3)Depends on ?

�7

Page 8: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C

CATGT

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

SA(X5, Y3)Depends on ?

�8

Page 9: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

�9

Page 10: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min( )1+0, 1+1, 1+1

�10

Page 11: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min( )1+0, 1+1, 1+1

�11

Page 12: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min( )0+1, 1+2, 1+1

�12

Page 13: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min( )0+1, 1+2, 1+1

�13

Page 14: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min(1+2, 1+3, 1+1)

�14

Page 15: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min(1+2, 1+3, 1+1)

�15

Page 16: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min(1+3, 1+4, 1+2)

�16

Page 17: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2 3A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min(1+3, 1+4, 1+2)

�17

Page 18: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2 3 4A 2T 3G 4T 5

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

min(2+4, 1+5, 1+3)

�18

Page 19: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2 3 4 5 6A 2 1 2 1 2 3 4 5T 3 2 3 2 3 3 3 4G 4 3 4 3 4 3 4 5T 5 4 5 4 5 4 3 4

A C G TA 0 1 2 2

C 1 0 2 3

G 2 2 0 1

T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise

δ = 1

�19

Page 20: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

SA(X[1…m],Y[1…n],δ,A){for i=0 to mM[i,0] := iδ

for j=0 to nM[0,j] := jδ

for i=1 to mfor j = 1 to nM[i,j] := min{ A[i,j] + M[i-1,j-1],

δ + M[i-1,j], δ + M[i,j-1]}

Return M[m,n]}

Sequence alignment

�20

• Time: Ɵ(mn)• Space: Ɵ(mn)

Page 21: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Sequence alignment: Finding the solution

A C A A G T C0 1 2 3 4 5 6 7

C 1 1 1 2 3 4 5 6A 2 1 2 1 2 3 4 5T 3 2 3 2 3 3 3 4G 4 3 4 3 4 3 4 5T 5 4 5 4 5 4 3 4

A C G TA 0 1 2 2C 1 0 2 3G 2 2 0 1T 2 3 1 0

Penalty matrix

SA(Xi, Yj) =

8>>>>>><

>>>>>>:

j� if i = 0

i� if j = 0

min

8><

>:

↵(xi, yj) + SA(Xi�1, Yj�1),

� + SA(Xi, Yj�1),

� + SA(Xi�1, Yj)}otherwise δ = 1

A C A A G T C← ← ← ← ← ← ←

C ↑ ↖ ↖ ← ← ← ← ↖

A ↑ ↖ ↖ ↖ ↖ ← ← ←

T ↑ ↑ ↑ ↑ ↖ ↖ ↖ ←

G ↑ ↑ ↖ ↑ ↖ ↖ ↖ ↖

T ↑ ↑ ↑ ↑ ↖ ↑ ↖ ←

�21

Page 22: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Use dynamic programming to compute an optimal alignment.• Time: Ɵ(mn)• Space: Ɵ(mn)

• Find actual alignment by backtracking (or saving information in another matrix).

• Linear space? • Easy to compute value (save last and current row)• How to compute alignment? Hirschberg. (not part of the curriculum).

Sequence alignment

�22

Page 23: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Shortest paths

�23

Page 24: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• All-Pairs Shortest Path Problem (APSP)• Given directed weighted graph G=(V,E). • Weights of edges cij are real numbers (might be negative). • Let n = |V| and m= |E|.• Weight of a path is the sum of the weights on its edges.• Goal: Compute the shortest path for from node s to node t.

Shortest Paths

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

�24

Page 25: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• All-Pairs Shortest Path Problem (APSP)• Given directed weighted graph G=(V,E). • Weights of edges cij are real numbers (might be negative). • Let n = |V| and m= |E|.• Weight of a path is the sum of the weights on its edges.• Goal: Compute the shortest path for from node s to node t.

Shortest Paths

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

�25

Page 26: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• All-Pairs Shortest Path Problem (APSP)• Given directed weighted graph G=(V,E). • Weights of edges cij are real numbers (might be negative). • Let n = |V| and m= |E|.• Weight of a path is the sum of the weights on its edges.• Goal: Compute the shortest path for from node s to node t.

Shortest Paths

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

-

�26

Page 27: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Dijkstra

• Re-weighting

Failed attempts

s t

3

2

-3

3

25 5

6

0

6

�27

s

2

1-10

2

t1

Page 28: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Negative cycle. If some path from s to t contains a negative cost cycle, then there does not exist a shortest s-t path. Otherwise, there exists one that is simple.

• Optimal substructure. Subpaths of shortest paths are shortest paths

Observations

s t

�28

Page 29: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• OPT(i,v) = length of shortest v-t path P using at most i edges. • Case 1: P uses at most i-1 edges.

• Case 2: P uses exactly i edges.

• If no negative cycles then OPT(n-1,v) = length of shortest path

Recurrence

tv w

shortest w ➝ t path using at most i-1 edges

cvw

v t

shortest v ➝ t path using at most i-1 edges

OPT(i,v) = OPT(i-1,v)

OPT(i,v) = OPT(i-1,w) + cvw

OPT(i, v) = {0 if i = 0min{OPT(i − 1,v), min(v,w)∈E{OPT(i − 1,w) + cvw}} otherwise

�29

Page 30: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Bellman-Ford

OPT(i, v) = {0 if i = 0min{OPT(i − 1,v), min(v,w)∈E{OPT(i − 1,w) + cvw}} otherwise

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ EM[i,v] = min(M[i,v], M[i-1,w] + cvw)

�30

Page 31: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

�31

0 1 2 3 4 5 6 7s ∞a ∞b ∞c ∞d ∞e ∞f ∞t 0

a

d

b

e

c

f

Page 32: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

�32

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞a ∞b ∞c ∞d ∞e ∞f ∞t 0

∞∞∞44166190

0

∞∞

∞6

16

44

19

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 33: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

19

6

44

16

59

29

36

0

�33

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞ ∞a ∞ ∞b ∞ ∞c ∞ 44d ∞ 16e ∞ 6f ∞ 19t 0 0

5929364416600

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 34: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

36

29

59 6

44

16

38

10

18

0

�34

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞ ∞ 59a ∞ ∞ 29b ∞ ∞ 36c ∞ 44 44d ∞ 16 16e ∞ 6 6f ∞ 19 0t 0 0 0

3810184416600

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 35: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

18

10

38 6

44

16

19

0

�35

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞ ∞ 59 38a ∞ ∞ 29 10b ∞ ∞ 36 18c ∞ 44 44 44d ∞ 16 16 16e ∞ 6 6 6f ∞ 19 0 0t 0 0 0 0

1910184416600

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 36: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

18

10

19 6

44

16

0

�36

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞ ∞ 59 38 19a ∞ ∞ 29 10 10b ∞ ∞ 36 18 18c ∞ 44 44 44 44d ∞ 16 16 16 16e ∞ 6 6 6 6f ∞ 19 0 0 0t 0 0 0 0 0

1910184416600

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 37: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Example

s

t

9

10

6

18

15-8

30

20

44

-16

11

6 19

616

0

18

10

19 6

44

16

0

�37

a

d

b

e

c

f

0 1 2 3 4 5 6 7s ∞ ∞ 59 38 19 19 19 19a ∞ ∞ 29 10 10 10 10 10b ∞ ∞ 36 18 18 18 18 18c ∞ 44 44 44 44 44 44 44d ∞ 16 16 16 16 16 16 16e ∞ 6 6 6 6 6 6 6f ∞ 19 0 0 0 0 0 0t 0 0 0 0 0 0 0 0

can stop when no changes in a round

Bellmann-Ford(G,s,t)

for each node v ∈ V M[0,v] = ∞

M[0,t] = 0.for i=1 to n-1

for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ E

M[i,v] = min(M[i,v], M[i-1,w] + cvw

Page 38: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

Bellman-Ford

Bellmann-Ford(G,s,t)

for each node v ∈ V M[v] = ∞

M[t] = 0.for i=1 to n-1for each node v ∈ VM[i,v] = M[i-1,v]for each edge (v,w) ∈ EM[i,v] = min(M[i,v], M[i-1,w] + cvw

• Running time. O(mn)• Space. O(n2)

�38

Page 39: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Improvements to basic implementation• Maintain only one array• No need to check edges of form (v,w) if M[w] didn’t change in previous iteration.

• Space: O(m+n)• Running time: O(mn) worst case, but substantially faster in practice.

Bellman-Ford

Bellmann-Ford-push-based(G,s,t)for each node v ∈ V

M[v] = ∞succ[v] = nil

M[t] = 0.for i=1 to n-1

for each node w ∈ Vif M[w] was updated in previous iteration do

for each node v such that (v,w) ∈ Eif M[v] > M[w] + cvw do

M[v] = M[w] + cvwsucc[v] = w

if no M[w] changed in iteration i, stop.

�39

Page 40: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Lemma. If OPT(n,v) < OPT(n-1,v) for some node, then (any) shortest path from v to t contains a cycle C with negative cost.

• Proof. By contradiction.• OPT(n,v) < OPT(n-1,v) => P has exactly n edges• => P contains a cycle C.• Deleting C gives a v-t path with < n edges => C makes v-t path shorter => C has

negative cost.

• Lemma. If OPT(n,v) = OPT(n-1,v) for all v, then no negative cycles.

Detecting negative cycles

v t

C

�40

Page 41: Dynamic Programming II - Technical University of Denmark · Dynamic Programming II Inge Li Gørtz KT section 6.6 and 6.8 Thank you to Kevin Wayne for inspiration to slides 1 • Optimal

• Detect negative cost cycles in O(mn) time.• Add new node t and connect all nodes to t with 0-cost edge.• Check if OPT(n,v) = OPT(n-1,v) for all nodes v.

• Yes: No negative cycles.• No: Can find negative cycle from shortest path from v to t.

Detecting negative cycles

t

18

-830

20

0

-16

11

6

000

-

0

�41