Professional Documents
Culture Documents
0-1 Knapsack Problem Informal Description: We have computed data les bytes that we want to store, and we have available of storage. File has size bytes and takes minutes to recompute. We want to avoid as much recomputing as possible, so we want to nd a subset of les to store such that The les have combined size at most . The total computing time of the stored les is as large as possible. We can not store parts of les, it is the whole le or nothing. How should we select the les?
-tuples of positive
"!$# % &!'#( and ) 0 , we wish to determine the subset and 1 2 354 768 9@BA (of les to store) that maximizes "F DC%E subject to HG DCE
Remark: This is an optimization problem. Brute force: Try all
6 !
possible subsets
Remark: If the subproblems are not independent, i.e. subproblems share subsubproblems, then a divideand-conquer algorithm repeatedly solves the common subsubproblems. Thus, it does more work than necessary! Question: Any better solution? YesDynamic programming (DP)!
4
The Idea of Dynamic Programming Dynamic programming is a method for solving optimization problems. The idea: Compute the solutions to the subsub-problems once and store the solutions in a table, so that they can be reused (repeatedly) later. Remark: We trade space for time.
Step1: Structure: Characterize the structure of an optimal solution. Decompose the problem into smaller problems, and nd a relation between the structure of the optimal solution of the original problem and the solutions of the smaller problems.
Step2: Principle of Optimality: Recursively dene the value of an optimal solution. Express the solution of the original problem in terms of optimal solutions for smaller problems.
Step 3: Bottom-up computation: Compute the value of an optimal solution in a bottom-up fashion by using a table structure.
Step 4: Construction of optimal solution: Construct an optimal solution from computed information.
Step 1: Decompose the problem into smaller problems. We construct an array I P QRS Q . 4 G G , and 0 G G , the entry For I PUV@ T will store the maximum (combined) 354 computing time of any subset of les 768 @WA of (combined) size at most . If we can compute all the entries of this array, then T will contain the maximum the array entry I PUS computing time of les that can t into the storage, that is, the solution to our problem.
0 T
Developing a DP Algorithm for Knapsack Step 2: Recursively dene the value of an optimal solution in terms of solutions to smaller problems.
THY 0 I P0 X S I P`V T Y a b
for for
0 G G c 0,
no item illegal
10
4 G G ,0 G G , r T Y d e g i f h a T a a T q 4 4 I PUV@ I PU p I PU X T Proof: To compute I P`V we note that we have only two choices for le :
Leave le : The best we can do with les 354 768 @ a 4 A and storage limit is I
I PUV@ T
a T 4 UP V .
Take le (only possible if s G ): Then we gain of computing time, but have spent bytes of our storage. The best we can do with remaining 3t4 76u a 4 A and storage h a q is les I PU a 4 a & T . a 4 X a & T . Totally, we get v p I PU
a 4 V a & THY a b
11
Developing a DP Algorithm for Knapsack Step 3: Bottom-up computing I not recursion). Bottom:
PUV T
(using iteration,
I P0 TrY 0
for all
0 G G
row by row.
1 0 2 0 3 0 ... ... ... W ... 0
bottom
Y 0 4 Let
and
y D 0
1 2 3 4 0 0 0 0 1 0 0 0 0 0
" &
2 0 0 0 0 0
1 10 5
3 0 0 0 0 50
2 3 4 40 30 50 4 6 3
4 0 0 40 40 50 5 0 10 40 40 50 6 0 10 40 40 50 7 0 10 40 40 90 8 0 10 40 40 90 9 0 10 50 50 90 10 0 10 50 70 90
Remarks:
y g
The method described does not tell which subset gives the in this example). optimal solution. (It is
13
d ! fe ) y ghi ; for ( to e ) for ( to ! ) for ( to e ) if (y Q'j ) Dk l mon y wp qhr Qts y w p qh p QQ ; else y Dk ys wp qh ; y ! ue ; return v g w h q.
14
described in the previous slide does not keep record of which subset of items gives the optimal solution.
To compute the actual subset, we can add an T auxiliary boolean array x5yy@zBPUV{ which is 1 if we T decide to take the -th le in I P`V and 0 otherwise.
Question: How do we use all the values x5yy@zBP`V( to 1 determine the subset of les having the maximum computing time?
15
Constructing the Optimal Solution Question: How do we use the values x5yqy@z|PUV7 to 1 determine the subset of items having the maximum computing time? If keep[ S ] is 1, then a this argument for keep[ If keep[ r ] is 0, the } ~ a 4 ]. ment for keep[
16
d ! fe ) y wh ; for ( to e ) for ( to ! ) for ( to e ) Qts y g QQ y w Q j p q h p p qh )) if (( ) and ( g ys Dh Qts y gp qh Q Q ; p D ; keep v else g ys Dh y wp qh ; D ; keep v e ; for ( ! downto 1) if g (keep D t ) output i; p Q ; v y ! e ; return
17