Algorithm 6
Algorithm 6
Algorithm 6
算法設計要論 第 6 回
Yasushi Kawase
河瀬 康志
Divide-and-Conquer
• Divide up problem into several independent problems
• Solve each subproblems recursively
• Combine solutions to subproblems into overall solution
Dynamic Programming
• Divide up problem into several overlapping problems
• Solve each subproblems recursively (or bottom up)
• Combine solutions to subproblems into overall solution
4 Sequence alignment
Problem
• Input: jobs J = {1, 2, . . . , n},
job j starts at sj , finishes at fj , and has value vj > 0
• Goal: find maximum value subset of mutually compatible jobs
two jobs that don’t overlap
Example
v=2
4
4
7
2
1
time
Problem
• Input: jobs J = {1, 2, . . . , n},
job j starts at sj , finishes at fj , and has value vj > 0
• Goal: find maximum value subset of mutually compatible jobs
two jobs that don’t overlap
Example
v=2
4
4
7
2
1
time
Recursive formula
0 if j = 0,
(
OPT(j) =
max{vj + OPT(p(j)), OPT(j − 1)} if j > 0
maximum i such that f (i) ≤ s(j) (found in O(log n) via binary search)
v=2
4
p(j) 4
7
2
j 1
time
j
1 v=2
2 4
3 4
4 7
5 2
6 1
time
j 0 1 2 3 4 5 6
OPT(j)
j
1 v=2
2 4
3 4
4 7
5 2
6 1
time
j 0 1 2 3 4 5 6
OPT(j) 0 2 4 6 7 8 8
4 Sequence alignment
Problem
• Input: positive integers a1 , a2 , . . . , an and W
• Goal: find I ⊆ {1, 2, . . . , n} such that i∈I ai = W
P
Examples
• a = (1, 3, 7, 8, 11), W = 20 possible (1 + 8 + 11 = 20)
True if k = 0 and w = 0
if k = 0 and w > 0
False
ψ(k, w) =
ψ(k − 1, w) if w < ak
ψ(k − 1, w) ∨ ψ(k − 1, w − ak ) otherwise
Example: a1 = 2, a2 = 4, a3 = 3, W = 5
k \ w 0 1 2 3 4 5
0 — — — — — —
a1 = 2 1 — — — — — —
a2 = 4 2 — — — — — —
a3 = 3 3 — — — — — —
Yasushi Kawase Advanced Core in Algorithm Design 11 / 24
Dynamic programming for subset sum problem
ψ(k, w): solution for a1 , a2 , . . . , ak and w (True or False)
Recursive formula
True if k = 0 and w = 0
if k = 0 and w > 0
False
ψ(k, w) =
ψ(k − 1, w) if w < ak
ψ(k − 1, w) ∨ ψ(k − 1, w − ak ) otherwise
Example: a1 = 2, a2 = 4, a3 = 3, W = 5
k \ w 0 1 2 3 4 5
0 True False False False False False
a1 = 2 1 — — — — — —
a2 = 4 2 — — — — — —
a3 = 3 3 — — — — — —
Yasushi Kawase Advanced Core in Algorithm Design 11 / 24
Dynamic programming for subset sum problem
ψ(k, w): solution for a1 , a2 , . . . , ak and w (True or False)
Recursive formula
True if k = 0 and w = 0
if k = 0 and w > 0
False
ψ(k, w) =
ψ(k − 1, w) if w < ak
ψ(k − 1, w) ∨ ψ(k − 1, w − ak ) otherwise
Example: a1 = 2, a2 = 4, a3 = 3, W = 5
k \ w 0 1 2 3 4 5
0 True False False False False False
a1 = 2 1 True False True False False False
a2 = 4 2 — — — — — —
a3 = 3 3 — — — — — —
Yasushi Kawase Advanced Core in Algorithm Design 11 / 24
Dynamic programming for subset sum problem
ψ(k, w): solution for a1 , a2 , . . . , ak and w (True or False)
Recursive formula
True if k = 0 and w = 0
if k = 0 and w > 0
False
ψ(k, w) =
ψ(k − 1, w) if w < ak
ψ(k − 1, w) ∨ ψ(k − 1, w − ak ) otherwise
Example: a1 = 2, a2 = 4, a3 = 3, W = 5
k \ w 0 1 2 3 4 5
0 True False False False False False
a1 = 2 1 True False True False False False
a2 = 4 2 True False True False True False
a3 = 3 3 — — — — — —
Yasushi Kawase Advanced Core in Algorithm Design 11 / 24
Dynamic programming for subset sum problem
ψ(k, w): solution for a1 , a2 , . . . , ak and w (True or False)
Recursive formula
True if k = 0 and w = 0
if k = 0 and w > 0
False
ψ(k, w) =
ψ(k − 1, w) if w < ak
ψ(k − 1, w) ∨ ψ(k − 1, w − ak ) otherwise
Example: a1 = 2, a2 = 4, a3 = 3, W = 5
k \ w 0 1 2 3 4 5
0 True False False False False False
a1 = 2 1 True False True False False False
a2 = 4 2 True False True False True False
a3 = 3 3 True False True True True True
Yasushi Kawase Advanced Core in Algorithm Design 11 / 24
Knpsack problem
Problem
• Input: items E = {1, 2, . . . , n} and a capacity W ∈ Z+
item i has value vi ∈ Z+ and size wi ∈ Z+
• Goal: maximize i∈I vi subject to I ⊆ {1, 2, . . . , n} i∈I wi ≤ W
P P
Examples
W = 16
i vi wi
1 55 4
2 61 2
3 82 9
4 38 1
5 63 3
Problem
• Input: items E = {1, 2, . . . , n} and a capacity W ∈ Z+
item i has value vi ∈ Z+ and size wi ∈ Z+
• Goal: maximize i∈I vi subject to I ⊆ {1, 2, . . . , n} i∈I wi ≤ W
P P
Examples
W = 16 optimal value is 61 + 82 + 38 + 63 = 244
i vi wi
1 55 4
2 61 2
3 82 9
4 38 1
5 63 3
Recursive formula
0 if k = 0,
OPT(k, w) = OPT(k − 1, w) if wk > w
max{OPT(k − 1, w), OPT(k − 1, w − wk ) + vk } otherwise
Example (wi , vi ) = (4, 55), (2, 61), (9, 82), (1, 38), (3, 63), W = 16
k\w 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 55 55 55 55 55 55 55 55 55 55 55 55 55
2 0 0 61 61 61 61 116 116 116 116 116 116 116 116 116 116 116
3 0 0 61 61 61 61 116 116 116 116 116 143 143 143 143 198 198
4 0 38 61 99 99 99 116 154 154 154 154 154 181 181 181 198 236
5 0 38 61 99 101 124 162 162 162 179 217 217 217 217 217 244 244
4 Sequence alignment
• Input: n points p1 , p2 , . . . , pi , . . . , pn ∈ R2
• Goal: find a line y = ax + b that minimizes the sum of the squared error
Pn
i=1 (yi − axi − b)2
Example
y
x
Pn Pn Pn Pn Pn
n i=1 xi yi −( i=1 xi )( i=1 yi ) yi −a i=1 xi
Optimal solution: a = Pn n 2 ,b= i=1
n
n i=1 xi2 − i=1 xi
P
O(n) time
• Input: n points p1 , p2 , . . . , pi , . . . , pn ∈ R2
• Goal: find a line y = ax + b that minimizes the sum of the squared error
Pn
i=1 (yi − axi − b)2
Example
y
x
Pn Pn Pn Pn Pn
n i=1 xi yi −( i=1 xi )( i=1 yi ) yi −a i=1 xi
Optimal solution: a = Pn n 2 ,b= i=1
n
n i=1 xi2 − i=1 xi
P
O(n) time
• Input: n points p1 , p2 , . . . , pi , . . . , pn ∈ R2
• Goal: find a line y = ax + b that minimizes the sum of the squared error
Pn
i=1 (yi − axi − b)2
Example
y
x
Pn Pn Pn Pn Pn
n i=1 xi yi −( i=1 xi )( i=1 yi ) yi −a i=1 xi
Optimal solution: a = Pn n 2 ,b= i=1
n
n i=1 xi2 − i=1 xi
P
O(n) time
Examples
y
Examples
y
L=2
Recursive formula
0 if j = 0
(
OPT(j) =
min1≤i≤j {eij + c + OPT(i − 1)} if j > 0
x
Yasushi Kawase Advanced Core in Algorithm Design 17 / 24
Improve the computational time
Pj
Bottleneck: computing eij = k=i (yk − aij xk − bij )2 takes O(n) time
(j−i+1)n jk=i xk yk −( jk=i xk )( jk=i yk )
P P P
• aij = 2
(j−i+1) jk=i xk2 −
Pj
k=i xk
P
Pj
yk −aij jk=i xk
P
• bij = k=i
j−i+1
Approach
Pj Pj Pj 2
Pj
• precompute k=1 xk , k=1 yk , k=1 xk , k=1 xk yk (∀j) in O(n) time
• using cumulative sums, eij can be computed in O(1) time
segmented least squares is solvable in O(n 2 ) time
4 Sequence alignment
Alignment of X and Y
set of ordered pairs xi –yj such that each character appears in at most one
pair and no crossing
o c u r r a n c e – 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 c u r r e – n c e
6 mismatches, 1 gap 1 mismatch, 1 gap 0 mismatches, 3 gaps
Example
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
cost = δ + αae cost = 3δ
Problem
• Input: two strings X = x1 x2 . . . xm and Y = y1 y2 . . . yn , penalty δ, αpq
• Goal: compute the edit distance
Recursive formula
jδ if i = 0
iδ if j = 0
OPT(i, j) = αxi yj + OPT(i − 1, j − 1)
OPT(i 1, j) otherwise
min δ + −
δ + OPT(i, j − 1)
X: o c u r r a n c e
Y: 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
o c u r r a n c e
0 2 4 6 8 10 12 14 16 18
o 2 0 2 4 6 8 10 12 14 16
c 4 2 0 2 4 6 8 10 12 14
c 6 4 2 1 3 5 7 9 10 12
u 8 6 4 2 2 4 6 8 10 11
r 10 8 6 4 2 2 4 6 8 10
r 12 10 8 6 4 2 3 5 7 9
e 14 12 10 8 6 4 3 4 6 7
n 16 14 12 10 8 6 5 3 5 7
c 18 16 14 12 10 8 7 5 3 5
e 20 18 16 14 12 10 9 7 5 3
o c u r r a n c e
o
c
c
u
r
r
e
n
c
e