Weighted Interval Scheduling Segmented Least Squares Knapsack Problem RNA Secondary Structure
Weighted Interval Scheduling Segmented Least Squares Knapsack Problem RNA Secondary Structure
D YNAMIC P ROGRAMMING
I
‣ weighted interval scheduling
‣ segmented least
squares
‣ knapsack problem
‣ RNA secondary structure
Greed. Process the input in some order, myopically making irrevocable decisions.
2
Dynamic programming history
Etymology.
3
Dynamic programming applications
Application areas.
SECTIONS 6.1–6.2
Weighted interval scheduling
sj wj fj
h
time
0 1 2 3 4 5 6 7 8 9 10 11
7
Earliest-finish-time first algorithm
weight = 999 b
weight = 1
weight = 1 a
h
time
0 1 2 3 4 5 6 7 8 9 10 11
8
Weighted interval scheduling
8
time
0 1 2 3 4 5 6 7 8 9 10 11
9
Dynamic programming: binary choice
Def. OPT( j ) = max weight of any subset of mutually compatible jobs for subproblem
consisting only of jobs 1, 2, ..., j.
Bellman equation. OP T (j ) = 0 j = 0
max { OP T (j 1), wj + OP T (p(j )) } j >
10
0
Weighted interval scheduling: brute force
COMPUTE-
OPT( j )
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ IF
(j = 0)
RETURN 0.
ELSE
RETURN max {COMPUTE-OPT( j – 1), + COMPUTE-
wj OPT( p[ j ]) }.
11
Dynamic programming: quiz 1
A. Θ (n log n)
B. Θ (n2) (1) n= 1
n
T (n) =
C. Θ (1.618 ) 2T (n 1) + (1) n> 1
D. Θ (2 n)
COMPUTE-
OPT( j )
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ IF
(j = 0)
RETURN 0.
ELSE
RETURN max {COMPUTE-OPT( j – 1), + COMPUTE-
wj OPT( p[ j ]) }.
12
Weighted interval scheduling: brute force
Ex. Number of recursive calls for family of “layered” instances grows like Fibonacci
sequence.
1 4 3
2
3 2 2 1
3
4
2 1 1 0 1 0
5
1 0
p(1) = 0, p(j) = j - 2
recursion tree
13
Weighted interval scheduling: memoization
0.
RETURN M-COMPUTE-OPT(n).
M-COMPUTE-
OPT( j )
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
IF (M [ j ] is
uninitialized)
M [ j ] ← max { M-COMPUTE-OPT ( j – 1), wj + M-COMPUTE-OPT( p[ j ])
}.
RETURN M
[ j ].
14
Weighted interval scheduling: running time
15
16
Weighted interval scheduling: finding a solution
FIND-
SOLUTION( j )
_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________
IF (j = 0)
RETURN ∅.
ELSE IF (wj + M [ p[ j ]] > M [ j –
1])
RETURN { j } ∪ FIND-
SOLUTION(p[ j ]). ELSE
RETURN FIND-SOLUTION( j –
1).
M [ j ] = max { M[j – 1], wj +
M[p[ j ]] }.
1 5 − 3 −6 5 2 −5 5 9 −9 −2 8 −1 6
2 1 1 1 9 6 3 8 7 3 3 4 5
187
19
MAXIMUM RECTANGLE PROBLEM
2 5 0 5 2 2
3
4 3 1 3 2 1 1
5 6 3 5 1 4 2
A = 1 1 3 1 4 1 1
3 3 2 0 3 3 2
2 1 2 1 1 3 1
2 4 0 1 0 3 1
13
21
6. D YNAMIC P ROGRAMMING
I
‣ weighted interval scheduling
‣ segmented least
squares
‣ knapsack problem
‣ RNA secondary structure
SECTION 6.3
Least squares
n ix i yi − ( i x i )( i yi ) i i yi − a i xi
a = , b =
n x i 2− ( i x i ) 2 n
24
Segmented least squares
x 25
Segmented least squares
x 26
Dynamic programming: multiway choice
Notation.OP
・ T( j ) = minimum cost for points p1, p2, …, pj. =
・ eij SSE for for points pi, pi+1, …, pj.
To compute OPT( j ):
・Last segment uses points pi, pi+1, …, pj for some i ≤ j.
・Cost = eij + c + OPT(i – 1). optimal substructure property
(proof via exchange argument)
Bellman equation.
0 j = 0
OP T (j ) = min { ei j + c + OP T (i 1) } j >
01 i j
27
Segmented least squares algorithm
RETURN M
[ n].
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
28
Segmented least squares analysis
Theorem. [Bellman 1961] DP algorithm solves the segmented least squares problem in
3 2
O(n ) time and O(n ) space.
Pf.
・Bottleneck = computing SSE eij for each i and j.
n kx k yk − ( k x k )( k yk ) , k yk − ai j k xk
ai j = 2 2 ij =
n k xk − ( k xk ) b n
2
Remark. Can be improved to O(n ) time. i i i i
k=1
x k yk .
SECTION 6.4
Knapsack problem
Goal. Pack knapsack so as to maximize total value of items taken. There are n
・ items: item i provides value vi > 0 and weighs wi > 0. Value of a subset of
・ items = sum of values of individual items. Knapsack has weight limit of W.
・
Ex. The subset { 1, 2, 5 } has value $35 (and weight 10).
Ex. The subset { 3, 4 } has value $40 (and weight 11).
g
5 $28 7 kg
$28 7k
$1 g
1k
knapsack instance
Creative Commons Attribution-Share Alike 2.5
(weight limit W = 11)
by Dake 31
Dynamic programming: quiz 2
A.
Greedy-by-value: repeatedly add item with maximum vi. Greedy-by-
B. weight: repeatedly add item with minimum wi. Greedy-by-ratio: repeatedly
C. add item with maximum ratio vi / wi. None of the above.
D.
i vi wi
1 $1 1 kg
$18 g
5k
$22 g
6k
2 $6 2 kg
11 kg
3 $18 5 kg
$6 2 k g 4 $22 6 kg
g
5 $28 7 kg
$28 7k
$1 g
1k
knapsack instance
Creative Commons Attribution-Share Alike 2.5
(weight limit W = 11)
by Dake
32
Dynamic programming: quiz 3
Which subproblems?
A.
OPT(w) = optimal value of knapsack problem with weight limit w. OPT(i) =
B. optimal value of knapsack problem with items 1, …, i. OPT(i, w) = optimal
C. value of knapsack problem with items 1, …, i
subject to weight limit w.
33
Dynamic programming: two variables
OP T (i, w )
= OP T (i 1, w ) wi
> w max { OP T (i 1, w ), vi + OP T (i 1, w wi ) }
34
Knapsack problem: bottom-up dynamic programming
IF (wi > w) M [ i, w ] ← M [ i – 1,
w ].
ELSE M [ i, w ] ← max { M [ i – 1, w ], vi + M [ i – 1, w –
wi] }.
RETURN M [ n,
W].
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
0 i= 0
OP T (i, w )
= OP T (i 1, w ) wi
> w max { OP T (i 1, w ), vi + OP T (i 1, w wi ) }
35
Knapsack problem: bottom-up dynamic programming demo
i vi wi
1 $1 1 kg
0 i =0
2 $6 2 kg OP T (i, w ) OP T (i 1, w ) wi
=
3 $18 5 kg > w max { OP T (i 1, w ), vi + OP T (i 1, w wi }
4 $22 6 kg
5 $28 7 kg
weight limit w
0 1 2 3 4 5 6 7 8 9 10 11
{ } 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
subset { 1, 2 } 0 1 6 7 7 7 7 7 7 7 7 7
of items
1, …, i { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25
{ 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40
{ 1, 2, 3, 4, 5 } 0 1 6 7 7 18 22 28 29 34 35 40
OPT(i, w) = optimal value of knapsack problem with items 1, …, i, subject to weight limit w
36
Knapsack problem: running time
Theorem. The DP algorithm solves the knapsack problem with n items and
maximum weight W in Θ (n W) time and Θ (n W) space.
Pf. weights are integers
between 1 and W
・Takes O(1) time per table entry.
・There are Θ (n W) table entries.
・After computing optimal values, can trace back to find solution: OPT(i, w)
takes item i iff M [i, w] > M [i – 1, w]. ▪
Remarks.
37
Dynamic programming: quiz 4
B. No, because Θ (n W) is not a polynomial function of the input size. No, because
C. the problem is NP-hard.
D. Unknown.
38
COIN CHANGING
Problem. Given n coin denominations { c1, c2, …, cn } and a target value V, find the
fewest coins needed to make change for V (or report impossible).
Recall. Greedy cashier’s algorithm is optimal for U.S. coin denominations, but not for
arbitrary coin denominations.
39
6. D YNAMIC P ROGRAMMING
I
‣ weighted interval scheduling
‣ segmented least
squares
‣ knapsack problem
‣ RNA secondary structure
SECTION 6.5
RNA secondary structure
Secondary structure. RNA is single-stranded so it tends to loop back and form base
pairs with itself. This structure is essential for understanding behavior of molecule.
C A
A A
A U G C
base
C G U A A G
G
U A U U A
base pair G
A C G C U
G
C G C G A G C
G
A U
G G
C U
C G
A C G U G G C C A U
U A
B = ACGUGGCCCA U S
= { (b1, b10), (b2, b9), (b3, b8) } 43
RNA secondary structure
G G
C G
A U G G G G C A U
A U
B = A U GG G G C
AU
S = { (b1, b9), (b2, b8), (b3, b7) } 44
RNA secondary structure
< j < ℓ.
G G
C U
C U
A G
A G U U G G C C A U
< j < ℓ.
G G
C U
C G
A U
A U G U G G C C A U
< j < ℓ.
Free-energy hypothesis. RNA molecule will form the secondary structure with the
minimum total free energy.
47
Dynamic programming: quiz 5
A. Yes.
G C
C G U A A G
A U U A
G
G C U
G
C G A G C
A U
G 48
Dynamic programming: quiz 6
Which subproblems?
D. Neither A nor B.
49
RNA secondary structure: subproblems
Goal. OPT(n).
1 t j last base
50
Dynamic programming over intervals
Case 1. If i ≥ j – 4.
・OPT(i, j ) = 0 by no-sharp-turns condition.
i t j 51
Dynamic programming: quiz 7
A.
Increasing i, then j.
B. Increasing j, then i.
C. Either A or B.
D. Neither A nor B.
i t j
52
Bottom-up dynamic programming over intervals
RNA-SECONDARY-STRUCTURE(n, b1, …, bn ) j
6 7 8 9 10
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
FOR k = 5 TO n – 1 4 0 0 0
all needed values
FOR i = 1 TO n – k are already computed 3 0 0
i
j ← i + k. 2 0
1
Compute M[i, j] using formula.
RETURN M[1, n]. order in which to solve subproblems
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Theorem. The DP algorithm solves the RNA secondary structure problem in O(n ) time
and 3O(n ) space. 2
53
Dynamic programming summary
Techniques.
54