dynamic programming. decomposes a problem into a series of sub- problems builds up correct...
DESCRIPTION
3 DP is a method for solving certain kind of problems DP can be applied when the solution of a problem includes solutions to subproblems We need to find a recursive formula for the solution We can recursively solve subproblems, starting from the trivial case, and save their solutions in memory In the end we’ll get the solution of the whole problemTRANSCRIPT
Dynamic Programming
Decomposes a problem into a series of sub-problems
Builds up correct solutions to larger and larger sub-problems
Examples of Dynamic Programming: Coins Binomial Scheduling
Knapsack Problem Viterbi Algorithm Sequence Alignment
3
DP is a method for solving certain kind of problems
DP can be applied when the solution of a problem includes solutions to subproblems
We need to find a recursive formula for the solution We can recursively solve subproblems, starting
from the trivial case, and save their solutions in memory
In the end we’ll get the solution of the whole problem
4
Simple Subproblems We should be able to break the original problem to
smaller subproblems that have the same structure Optimal Substructure of the problems
The solution to the problem must be a composition of subproblem solutions
Subproblem Overlap Optimal subproblems to unrelated problems can
contain subproblems in common
How can we find the minimum number of coins to make up a specific denomination We could use the greedy method to find the minimum
number of US coins to make any amount At each step, just choose the largest coin that does not overshoot
the desired amount: 31¢=25 What if the nickel (5¢) did not exist
Greedy method would produce 7 coins (25+1+1+1+1+1+1)
The optimal solution is actually 4 coins (10+10+10+1) How can we find the minimum number of coins for
any given coin set?
For the following examples, we will assume coins in the following denominations: 1¢ 5¢ 10¢ 21¢ 25¢
We’ll use 63¢ as our goal
To make K cents: If there is a K-cent coin, then that one coin is the minimum Otherwise, for each value i < K,
Find the minimum number of coins needed to make i cents
Find the minimum number of coins needed to make K - i cents
Choose the i that minimizes this sum This algorithm can be viewed as divide-and-conquer,
or as brute force This solution is very recursive It requires exponential work It is infeasible to solve for 63¢
We can reduce the problem recursively by choosing the first coin, and solving for the amount that is left
For 63¢: One 1¢ coin plus the best solution for 62¢ One 5¢ coin plus the best solution for 58¢ One 10¢ coin plus the best solution for 53¢ One 21¢ coin plus the best solution for 42¢ One 25¢ coin plus the best solution for 38¢
Choose the best solution from among the 5 given above Instead of solving 62 recursive problems, we solve 5 This is still a very expensive algorithm
Idea: Solve first for one cent, then two cents, then three cents, etc., up to the desired amount Save each answer in an array !
For each new amount N, compute all the possible pairs of previous answers which sum to N For example, to find the solution for 13¢,
First, solve for all of 1¢, 2¢, 3¢, ..., 12¢ Next, choose the best solution among:
Solution for 1¢ + solution for 12¢ Solution for 2¢ + solution for 11¢ Solution for 3¢ + solution for 10¢ Solution for 4¢ + solution for 9¢ Solution for 5¢ + solution for 8¢ Solution for 6¢ + solution for 7¢
Suppose coins are 1¢, 3¢, and 4¢ There’s only one way to make 1¢ (one coin) To make 2¢, try 1¢+1¢ (one coin + one coin = 2 coins) To make 3¢, just use the 3¢ coin (one coin) To make 4¢, just use the 4¢ coin (one coin) To make 5¢, try
1¢ + 4¢ (1 coin + 1 coin = 2 coins) 2¢ + 3¢ (2 coins + 1 coin = 3 coins) The first solution is better, so best solution is 2 coins
To make 6¢, try 1¢ + 5¢ (1 coin + 2 coins = 3 coins) 2¢ + 4¢ (2 coins + 1 coin = 3 coins) 3¢ + 3¢ (1 coin + 1 coin = 2 coins) – best solution
Etc.
The first algorithm is recursive, with a branching factor of up to 62 Possibly the average branching factor is
somewhere around half of that (31) The algorithm takes exponential time, with a
large base The second algorithm is much better—it has a
branching factor of 5 This is exponential time, with base 5
The dynamic programming algorithm is O(N*K), where N is the desired amount and K is the number of different kinds of coins
12
(x + y)2 = x2 + 2xy + y2, coefficients are 1,2,1 (x + y)3 = x3 + 3x2y + 3xy2 + y3, coefficients are 1,3,3,1 (x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4,
coefficients are 1,4,6,4,1 (x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5,
coefficients are 1,5,10,10,5,1 The n+1 coefficients can be computed for (x + y)n according
to the formula nCx = n! / (x! * (n – x)!)
The repeated computation of all the factorials gets to be expensive
We can use dynamic programming to save the factorials as we go
n nC0 nC1 nC2 nC3 nC4 nC5 nC6
0 11 1 12 1 2 13 1 3 3 14 1 4 6 4 15 1 5 10 10 5 16 1 6 15 20 15 6 1
Each row depends only on the preceding rowOnly linear space and quadratic time are neededThis algorithm is known as Pascal’s Triangle
Dynamic programming is a technique for finding an optimal solution
The principle of optimality applies if the optimal solution to a problem always contains optimal solutions to all subproblems
Example: Consider the problem of making N¢ with the fewest number of coins Either there is an N¢ coin, or The set of coins making up an optimal solution for N¢
can be divided into two nonempty subsets, n1¢ and n2¢If either subset, n1¢ or n2¢, can be made with fewer
coins, then clearly N¢ can be made with fewer coins
The principle of optimality holds if Every optimal solution to a problem contains optimal
solutions to all subproblems The principle of optimality does not say
If you have optimal solutions to all subproblems then you can combine them to get an optimal solution
Example: In US coinage, The optimal solution to 7¢ is 5¢ + 1¢ + 1¢, and The optimal solution to 6¢ is 5¢ + 1¢, but The optimal solution to 13¢ is not 5¢ + 1¢ + 1¢ + 5¢ + 1¢
But there is some way of dividing up 13¢ into subsets with optimal solutions (say, 11¢ + 2¢) that will give an optimal solution for 13¢ Hence, the principle of optimality holds for this problem
Dynamic programming relies on working “from the bottom up” and saving the results of solving simpler problems These solutions to simpler problems are then used to compute the
solution to more complex problems Dynamic programming solutions can often be quite
complex and tricky Dynamic programming is used for optimization problems,
especially ones that would otherwise take exponential time Only problems that satisfy the principle of optimality are suitable
for dynamic programming solutions Since exponential time is unacceptable for all but the
smallest problems, dynamic programming is sometimes essential
Problem scheduling a lecture hall each request has start time, end time, value goal: maximize value
Basic idea sort requests by end time calculate p( request )
the next previous request that does not overlap optimal schedule for requests 1..n:
if n part of optimal solution: optimal schedule for p( n ) and n else: optimal schedule for n-1
use sub solutions to build up complete solution
Weighted interval scheduling problem. Given a collection of intervals I1,…,In with weights w1,
…,wn, choose a maximum weight set of non-overlapping intervals
Time0 1 2 3 4 5 6 7 8 9 10 11
f
g
h
e
a
b
c
d
Weighted interval scheduling problem. Given a collection of intervals I1,…,In with weights w1,
…,wn, choose a maximum weight set of non-overlapping intervals
p(j) = largest index i < j such that job i is compatible with j
Time
p(1) = 0p(2) = 0p(3) = 1p(4) = 0p(5) = 3p(6) = 3
Previous job
0 1 2 3 4 5 6 7 8 9 10 11
w6 = 1
w5 = 2
w1 = 2
w2 = 4
w3 = 4
w4 = 7
1
2
3
4
5
6
Assume we know optimal solution O: either contains or doesn’t contain the last request n
Opt(j) = max(wj + Opt(p(j)), Opt(j-1))
Time0 1 2 3 4 5 6 7 8 9 10 11
w6 = 1
w5 = 2
w1 = 2
w2 = 4
w3 = 4
w4 = 7
1
2
3
4
5
6
p(1) = 0p(2) = 0p(3) = 1p(4) = 0p(5) = 3p(6) = 3
Opt(j) = max(wj + Opt(p(j)), Opt(j-1)) Opt(6) = max(w6 + Opt(3), Opt(5)) = max(1 + 6, 8) = 8 Opt(3) = max(w3 + Opt(1), Opt(2)) = max(4 + 2, 4) = 6 Opt(5) = max(w5 + Opt(3), Opt(4)) = max(2 + 6, 7) = 8 Opt(1) = max(w1 + Opt(0), Opt(0)) = max(2 + 0, 0) = 2 Opt(2) = max(w2 + Opt(0), Opt(1)) = max(4 + 0, 2) = 4 Opt(4) = max(w4 + Opt(0), Opt(3)) = max(7 + 0, 6) = 7
Time0 1 2 3 4 5 6 7 8 9 10 11
w6 = 1
w5 = 2
w1 = 2
w2 = 4
w3 = 4
w4 = 7
1
2
3
4
5
6
p(1) = 0p(2) = 0p(3) = 1p(4) = 0p(5) = 3p(6) = 3
A thief is robbing a house. He has a bag to fill with items. This bag has a
maximum weight capacity of W. Each item he is considering stealing has a value
vi and a weight wi. His objective is to steal the items with maximum
total value while staying within the weight limit of his bag
In this case, what would a sub-problem be? Does a solution for item n include the solution for item n-1?
Assume we know optimal solution O: either contains or doesn’t contain the last item n It Does
Add value of item n (vn) to the total value O must contain an optimal solution to the problem consisting of
item n-1 and total weight W – wn It Doesn’t
O is equal to the optimal solution to the problem consisting of item n-1 and total weight W
If w < wi then Opt(i, w) = Opt(i – 1, w) otherwise Opt(i, w) = max(Opt(i – 1, w), vi + Opt(i -1, w – wi))
If w < wi then Opt(i, w) = Opt(i – 1, w) otherwise Opt(i, w) = max(Opt(i – 1, w), vi + Opt(i -1, w – wi))
i = n = 4, w = W = 5 5 < 5 ? No Opt(4, 5) = max(Opt(3, 5), 6 + Opt(3,
0)) i = 3, w = 5
5 < 4? No Opt(3, 5) = max(Opt(2, 5), 5 + Opt(3, 1))
i = 3, w = 0 0 < 4? Yes Opt(3, 0) = Opt(2, 0)....
Knapsack problem. Given n objects and a "knapsack." Item i weighs wi > 0 kilograms and has value vi >
0. Knapsack has capacity of W kilograms. Goal: fill knapsack so as to maximize total value.
Ex: { 3, 4 } has value 40.1
Value
18
22
28
1
Weight
5
6
6 2
7
Item
1
3
4
5
2W = 11
Def. OPT(i) = max profit subset of items 1, …, i.
Case 1: OPT does not select item i.OPT selects best of { 1, 2, …, i-1 }
Case 2: OPT selects item i.accepting item i does not immediately
imply that we will have to reject other items
without knowing what other items were selected before i, we don't even know if we have enough room for I
Conclusion. Need more sub-problems!
Def. OPT(i, w) = max profit subset of items 1, …, i with weight limit w. Case 1: OPT does not select item i.
OPT selects best of { 1, 2, …, i-1 } using weight limit w
Case 2: OPT selects item i.new weight limit = w – wiOPT selects best of { 1, 2, …, i–1 } using
this new weight limit
n + 1
1
Value
182228
1
Weight
56
6 2
7
Item
1
345
2
{ 1, 2 }
{ 1, 2, 3 }
{ 1, 2, 3, 4 }
{ 1 }
{ 1, 2, 3, 4, 5 }
0
0
0
0
0
0
0
1
0
1
1
1
1
1
2
0
6
6
6
1
6
3
0
7
7
7
1
7
4
0
7
7
7
1
7
5
0
7
18
18
1
18
6
0
7
19
22
1
22
7
0
7
24
24
1
28
8
0
7
25
28
1
29
9
0
7
25
29
1
34
10
0
7
25
29
1
34
11
0
7
25
40
1
40
W + 1
W = 11
OPT: { 4, 3 }value = 22 + 18 = 40