05 Dynamic Programming i i
05 Dynamic Programming i i
D YNAMIC P ROGRAMMING II
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance-vector protocols
‣ negative cycles
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance-vector protocols
‣ negative cycles
SECTION 6.6
String similarity
o c u r r a n c e – o c – u r r a n c e
o c c u r r e n c e o c c u r r e n c e
o c – u r r – a n c e
o c c u r r e – n c e
0 mismatches, 3 gaps
3
Edit distance
C T – G A C C T A C G
C T G G A C G A A C G
4
BLOSUM matrix for proteins
5
Dynamic programming: quiz 1
P A L E T T E P A L A T E
A. 1
B. 2
C. 3
D. 4
P A L E T T E
E. 5
P A L A – T E
1 mismatch, 1 gap
6
Sequence alignment
Goal. Given two strings x1 x2 ... xm and y1 y2 ... yn , find a min-cost alignment.
€ x1 x2 x3 x4 x5 x6
C T A C C – G
– T A C A T G
y1 y2 y3 y4 y5 y6
Def. OPT(i, j) = min cost of aligning prefix strings x1 x2 ... xi and y1 y2 ... yj.
Goal. OPT(m, n).
Pay gap for yj + min cost of aligning x1 x2 ... xi and y1 y2 ... yj–1.
8
Sequence alignment: bottom-up algorithm
FOR i = 0 TO m
M [i, 0] ← i δ.
FOR j = 0 TO n
M [0, j] ← j δ.
FOR i = 1 TO m
FOR j = 1 TO n
M [i, j] ← min { αxi yj + M [i – 1, j – 1],
δ + M [i – 1, j], already
computed
δ + M [i, j – 1] }.
9
Sequence alignment: traceback
S I M I L A R I T Y
0 2 4 6 8 10 12 14 16 18 20
I 2 4 1 3 2 4 6 8 7 9 11
D 4 6 3 3 4 4 6 8 9 9 11
E 6 8 5 5 6 6 6 8 10 11 11
N 8 10 7 7 8 8 8 8 10 12 13
T 10 12 9 9 9 10 10 10 10 9 11
I 12 14 8 10 8 10 12 12 9 11 11
T 14 16 10 10 10 10 12 14 11 8 11
Y 16 18 12 12 12 12 12 14 13 10 7
10
Sequence alignment: analysis
11
Abstract
Dynamic programming: quiz 3
C. Both A and B.
D. Neither A nor B.
j i=0
i j=0
OP T (i, j) = x i yj + OP T (i 1, j 1)
min + OP T (i 1, j)
+ OP T (i, j 1)
<latexit sha1_base64="60wUnMrLdnu23FwG/Nh5FwbDvxY=">AAADS3ichVJdaxNBFJ3dWq3xq62PvgwGpUUbdsViIRQKvvhmhKYtZEKYnb1JJp2dXWbu1oQlz/4aX/VX+AP8Hb6JD85uFslHwQsDl3PPPefOnYkyJS0GwU/P37qzfffezv3Gg4ePHj/Z3du/sGluBHRFqlJzFXELSmrookQFV5kBnkQKLqPr92X98gaMlak+x1kG/YSPtBxKwdFBgz2PfuycH8jXdHJIWfuUtRssgpHUhXCidt5gbTqhLAaFnL6kDGGKhRzOHZdKekoDylivdQxJv1EhtzMnG0yWSL3hxLjKxnxQTAdyNpiUna8ctZrvKHQTHoWHyyK11zrrP5yFToOBjmvnf9OmOAbzWVqYL5cHu82gFVRBN5OwTpqkjo7b6T6LU5EnoFEobm0vDDLsF9ygFKoUzy1kXFzzEfRcqnkCtl9UbzmnLxwS02Fq3NFIK3S5o+CJtbMkcsyE49iu10rwtlovx+FJv5A6yxG0WBgNc0UxpeXHoLE0IFDNXMKFkW5WKsbccIHu+6y4VNoZiJWbFNNcS5HGsIYqnKLh5RbD9Z1tJhdvWmHQCj+9bZ6d1PvcIc/Ic3JAQvKOnJEPpEO6RHhfvK/eN++7/8P/5f/2/yyovlf3PCUrsbX9Fz1PAsg=</latexit>
12
6. D YNAMIC P ROGRAMMING II
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance-vector protocols
‣ negative cycles
SECTION 6.7
Sequence alignment in linear space
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1
α xi y j
δ
x2 i–j
€ δ
x3 m–n
15
Hirschberg′s algorithm
=
= min{
min{ x
xii y
yjj
+
+ OP
OP T
T (i
(i 1,
1, jj 1),
1), +
+ OP
OP T
T (i
(i 1,
1, j),
j), +
+ OP
OP T
T (i,
(i, jj 1)}
1)}
inductive =
hypothesis
= OP
OP T
T (i,
(i, j)
j) ▪
Bellman
α xi y j
δ
equation
i–j
€ δ 16
Hirschberg’s algorithm
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1
x2 i–j
x3 m–n
17
Hirschberg’s algorithm
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1 i–j
x2
x3 m–n
18
Hirschberg’s algorithm
ε y1 y2 y3 y4 y5 y6
ε 0–0
δ
x1 i–j
δ xi+1 yj+1
x2
x3 m–n
19
Hirschberg’s algorithm
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1 i–j
x2
x3 m–n
20
Hirschberg’s algorithm
Observation 1. The length of a shortest path that uses (i, j) is f (i, j) + g(i, j).
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1 i–j
x2
x3 m–n
21
Hirschberg’s algorithm
n/2
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1 i–j q
x2
x3 m–n
22
Hirschberg’s algorithm
Divide. Find index q that minimizes f (q, n / 2) + g(q, n / 2); save node i–j as
part of solution.
n/2
ε y1 y2 y3 y4 y5 y6
ε 0–0
x1 i–j q
x2
x3 m–n
23
Hirschberg’s algorithm: space analysis
Pf.
Each recursive call uses Θ(m) space to compute f (·, n / 2) and g(·, n / 2).
Only Θ(1) space needs to be maintained per recursive call.
Number of recursive calls ≤ n. ▪
24
Dynamic programming: quiz 4
A. O(mn)
B. O(mn log m)
C. O(mn log n)
25
Hirschberg’s algorithm: running time analysis warmup
Pf.
T(m, n) is monotone nondecreasing in both m and n.
T(m, n) ≤ 2 T(m, n / 2) + O(m n)
⇒ T(m, n) = O(m n log n).
26
Hirschberg′s algorithm: running time analysis
28
6. D YNAMIC P ROGRAMMING II
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance-vector protocols
‣ negative cycles
SECTION 6.8
Shortest paths with negative weights
5
4 12
s 5
8
9
7
9 1
6
5 11
5
3 13
4 10 t
Dijkstra. May not produce shortest paths when edge lengths are negative.
s 6 v
t 3 w
s 14 v
t 11 w
33
Negative cycles
Def. A negative cycle is a directed cycle for which the sum of its edge
lengths is negative.
3 3
4 4
34
Shortest paths and negative cycles
Lemma 1. If some v↝t path contains a negative cycle, then there does not
exist a shortest v↝t path.
Pf. If there exists such a cycle W, then can build a v↝t path of arbitrarily
negative length by detouring around W as many times as desired. ▪
v t
W
(W) < 0
35
Shortest paths and negative cycles
Lemma 2. If G has no negative cycles, then there exists a shortest v↝t path
that is simple (and has ≤ n – 1 edges).
Pf.
Among all shortest v↝t paths, consider one that uses the fewest edges.
If that path P contains a directed cycle W, can remove the portion of P
corresponding to W without increasing its length. ▪
v t
W
(W) ≥ 0
36
Shortest-paths and negative-cycle problems
1 5
4 2 3 5 3 3
t 4 4
37
Dynamic programming: quiz 5
C. Neither A nor B.
38
Shortest paths with negative weights: dynamic programming
Bellman equation.
0 i=0 v=t
39
Shortest paths with negative weights: implementation
SHORTEST-PATHS(V, E, , t)
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
FOREACH node v ∈ V :
M [0, v] ← ∞.
M [0, t] ← 0.
FOR i = 1 TO n – 1
FOREACH node v ∈ V :
M [i, v] ← M [i – 1, v].
FOREACH edge (v, w) ∈ E :
M [i, v] ← min { M [i, v], M [i – 1, w] + vw }.
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
40
Shortest paths with negative weights: implementation
Pf.
Table requires Θ(n2) space.
Each iteration i takes Θ(m) time since we examine each edge once. ▪
41
Dynamic programming: quiz 6
C. Both A and B.
D. Neither A nor B.
42
Shortest paths with negative weights: practical improvements
43
Bellman–Ford–Moore: efficient implementation
BELLMAN–FORD–MOORE(V, E, c, t)
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
FOREACH node v ∈ V :
d[v] ← ∞.
successor[v] ← null.
d[t] ← 0.
FOR i = 1 TO n – 1
FOREACH node w ∈ V :
IF (d[w] was updated in previous pass)
FOREACH edge (v, w) ∈ E :
pass i
IF (d[v] > d[w] + O(m) time
vw)
successor[v] ← w.
IF (no d[⋅] value changed in pass i) STOP.
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
44
Dynamic programming: quiz 7
C. Both A and B.
D. Neither A nor B.
45
Bellman–Ford–Moore: analysis
Lemma 3. For each node v : d[v] is the length of some v↝t path.
Lemma 4. For each node v : d[v] is monotone non-increasing.
Lemma 5. After pass i, d[v] ≤ length of a shortest v↝t path using ≤ i edges.
Pf. [ by induction on i ]
Base case: i = 0.
Assume true after pass i.
Let P be any v↝t path with ≤ i + 1 edges.
Let (v, w) be first edge in P and let Pʹ be subpath from w to t.
By inductive hypothesis, at the end of pass i, d[w] ≤ c(Pʹ)
because Pʹ is a w↝t path with ≤ i edges.
and by Lemma 4,
After considering edge (v, w) in pass i + 1: d[w] does not increase
d[v] ≤ vw + d[w]
≤ vw + c(Pʹ)
and by Lemma 4,
d[v] does not increase = (P) ▪
46
Bellman–Ford–Moore: analysis
47
Dynamic programming: quiz 8
C. Both A and B.
D. Neither A nor B.
48
Bellman–Ford–Moore: analysis
successor[2] = 1 successor[1] = t
d[2] = 20 d[1] = 10 d[t] = 0
2 10 1 10 t
1 1
successor[3] = t 3
d[3] = 1
49
Bellman–Ford–Moore: analysis
successor[2] = 1 successor[1] = 3
d[2] = 20 d[1] = 2 d[t] = 0
2 10 1 10 t
1 1
successor[3] = t 3
d[3] = 1
50
Bellman–Ford–Moore: analysis
d[3] = 10 d[2] = 8
3 2 2
9 d[t] = 0
t
1 3
4 8 1
d[4] = 11 d[1] = 5 51
Bellman–Ford–Moore: analysis
d[3] = 10 d[2] = 8
3 2 2
9 d[t] = 0
t
1 3
4 8 1
d[4] = 11 d[1] = 3 52
Bellman–Ford–Moore: finding the shortest paths
(LHS and RHS are equal when successor[v] is set; d[w] can only decrease;
d[v] decreases only when successor[v] is reset)
Let v1 → v2 → … → vk → v1 be the sequence of nodes in a directed cycle W.
Assume that (vk, v1) is the last edge in W added to the successor graph.
Just prior to that: d[v1] ≥ d[v2] + (v1, v2)
d[v2] ≥ d[v3] + (v2, v3)
⋮ ⋮ ⋮
d[vk–1] ≥ d[vk] + (vk–1, vk)
holds with strict inequality
d[vk] > d[v1] + (vk, v1) since we are updating d[vk]
Adding inequalities yields (v1, v2) + (v2, v3) + … + (vk–1, vk) + (vk, v1) < 0. ▪
W is a negative cycle
53
Bellman–Ford–Moore: finding the shortest paths
(LHS and RHS are equal when successor[v] is set; d[·] did not change)
Thus, d[v1] = d[v2] + (v1, v2)
since algorithm
d[v2] = d[v3] + (v2, v3) terminated
⋮ ⋮ ⋮
d[vk–1] = d[vk] + (vk–1, vk)
Adding equations yields d[v] = d[t] + (v1, v2) + (v2, v3) + … + (vk–1, vk). ▪
length of path P
min length of any v↝t path 0
(Theorem 2)
54
Single-source shortest paths with negative weights
20xx
55
6. D YNAMIC P ROGRAMMING II
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance-vector protocols
‣ negative cycles
SECTION 6.9
Distance-vector routing protocols
Communication network.
Node ≈ router.
Edge ≈ direct communication link.
non-negative, but
Length of edge ≈ latency of link. Bellman–Ford–Moore used anyway!
57
Distance-vector routing protocols
Ex. RIP, Xerox XNS RIP, Novell’s IPX RIP, Cisco’s IGRP, DEC’s DNA Phase IV,
AppleTalk’s RTMP.
Caveat. Edge lengths may change during algorithm (or fail completely).
s 1 v 1 t
“counting to infinity” 58
Path-vector routing protocols
Ex. Border Gateway Protocol (BGP), Open Shortest Path First (OSPF).
59
6. D YNAMIC P ROGRAMMING II
‣ sequence alignment
‣ Hirschberg′s algorithm
‣ Bellman–Ford–Moore algorithm
‣ distance vector protocol
‣ negative cycles
SECTION 6.10
Detecting negative cycles
Negative cycle detection problem. Given a digraph G = (V, E), with edge
lengths vw, find a negative cycle (if one exists).
5 6
3 2 4 3 3
4 4
61
Detecting negative cycles: application
1.4
66
741
1.3
0. 1.
33
12
6
0.657
USD GBP
1. 1.521
06
1 8
53
1.0
14
1
11
1.6
32
0
0.9
0.6
2
0.7
0.6
5
.6
95
0. 98
0 94
3
1.049
CAD CHF
0.953
62
Detecting negative cycles
Lemma 8. If OPT(n, v) < OPT(n – 1, v) for some node v, then (any) shortest v↝t
path of length ≤ n contains a cycle W. Moreover W is a negative cycle.
x
v t
W
c(W) < 0 63
Detecting negative cycles
Theorem 4. Can find a negative cycle in Θ(mn) time and Θ(n2) space.
Pf.
Add new sink node t and connect all nodes to t with 0-length edge.
G has a negative cycle iff G ʹ has a negative cycle.
Case 1. [ OPT(n, v) = OPT(n – 1, v) for every node v ]
By Lemma 7, no negative cycles.
Case 2. [ OPT(n, v) < OPT(n – 1, v) for some node v ]
Using proof of Lemma 8, can extract negative cycle from v↝t path.
(cycle cannot contain t since no edge leaves t) ▪
5 6 G′
0
0
3 2 4 3 3 t
0
0
4 4
64
Detecting negative cycles
Theorem 5. Can find a negative cycle in O(mn) time and O(n) extra space.
Pf.
Run Bellman–Ford–Moore on G ʹ for nʹ = n + 1 passes (instead of nʹ – 1).
If no d[v] values updated in pass nʹ, then no negative cycles.
Otherwise, suppose d[s] updated in pass nʹ.
Define pass(v) = last pass in which d[v] was updated.
Observe pass(s) = nʹ and pass(successor[v]) ≥ pass(v) – 1 for each v.
Following successor pointers, we must eventually repeat a node.
Lemma 6 ⇒ the corresponding cycle is a negative cycle. ▪
Remark. See p. 304 for improved version and early termination rule.
(Tarjan’s subtree disassembly trick)
65
Dynamic programming: quiz 9
A. O(m log n)
B. O(mn)
C. O(mn + n2 log n)
D. O(n2.38)
Chapter 46
E. No poly-time algorithm is known.
Chapter Data
46 Structures for Weighted Matching and
Nearest Common Ancestors with Linking
Structures
Data Harold for Weighted
N. Gabow* Matching and
Nearest Common Ancestors with Linking
Harold N. Gabow*
Abstract. This paper shows that the weighted match- optimization; detailed discussions are in [L, LP, PS].
ing problem on general graphs can be solved in time Edmonds gave the first polynomial-time algorithm for
O(n(m + n log n)), f or n and m the number of vertices weighted matching [El. Several implementations of Ed-
and edges, respectively. This was previously known monds’ algorithm have been proposed, with increas-
Abstract. only This forpaper that the Itweighted
shows graphs.
bipartite also showsmatch- optimization;
that a sequence detailedfast discussions
ingly running times: [L, LP,[G73,
are in O(n3) PS]. L], O(mn log n)
on general graphs can be solved
ing problemof m nca and link operations on n nodes can be pro-in time Edmonds gave [BD, GMG], O(n(m log Tog log 2++,nfor + n log n))
the first polynomial-time algorithm
O(n(m + n log n)), f or n and m the number
cessed on-line in time O(ma(m, n)+n). of vertices weighted
This was previ- Several implementations
‘[GGS].[El.Edmonds’
matching Ed-
algorithm is aofgeneralization of the’
and edges,ously respectively. This was previously known
known only for a restricted type of link operation. monds’ algorithm have been proposed, with increas-
Hungarian algorithm, due to Kuhn, for weighted match-
L], O(mn log n)
[G73, [K55,
O(n3) graphs
only for bipartite graphs. It also shows that a sequence ingly fast running ing ontimes:bipartite K56]. Fredman and Tar-
of m nca and link operations on n nodes can be pro- [BD, GMG], O(n(m log Tog log
jan implement the Hungarian2++,n + n log n))algorithm in O(n(m +
1. Introduction. This was previ- ‘[GGS]. Edmonds’ algorithm is a generalization of the’
cessed on-line in time O(ma(m, n)+n).
This paper solves two well-known problems in data n logn)) time using Fibonacci heaps [FT]. They ask if
ously known only for a restricted type of link operation. Hungarian algorithm, due to Kuhn, for weighted match-
structures and gives some related results. The ing starting general matching can be done in this
Tar- time. Our first
on bipartite graphs [K55, K56]. Fredman and 66
point is the matching problem for graphs, which jan leads to
implement result is an affirmative
the Hungarian algorithm answer: We
in O(n(m + show that a search
1. Introduction.
in Edmonds’ algorithm can be implemented in time