Professional Documents
Culture Documents
Lecture 13: The Knapsack Problem: Outline of This Lecture
Lecture 13: The Knapsack Problem: Outline of This Lecture
0-1 Knapsack Problem Informal Description: We have computed data les that we want to store, and we have available bytes 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?
and
and
subject to
Remark: This is an optimization problem. Brute force: Try all possible subsets
G H
F"
maximizes
(#'!& %
.
-tuples of positive
EC %D EC D ! 6
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 . For , and , the entry will store the maximum (combined) computing time of any subset of les of (combined) size at most . If we can compute all the entries of this array, then the array entry will contain the maximum computing time of les that can t into the storage, that is, the solution to our problem.
A 6 4 W@ 87 53
9
G G 0 T Q 0 SRQ 0 P I
T SUP I
@VUP I G GT 4
Developing a DP Algorithm for Knapsack Step 2: Recursively dene the value of an optimal solution in terms of solutions to smaller problems.
for
G G 0 G G 4 p q FT a @ 4 a UP I @ @ T 4 a UP I ige d ST @VUP I h f Y
0 c G G 0
, .
b a ST V`P I Y 0 HT X 0 P I Y
no item illegal
10
Take le (only possible if ): Then we gain of computing time, but have spent bytes of our storage. The best we can do with remaining les and storage is . Totally, we get . Note that if , then so the lemma is correct in any case.
T V 4 a UP I
T V`P I q T a 4 a UP I p T X 4 a UP I ige d rT @VUP I h f Y G G 0 G G 4 b a HT & a V 4 a UP I p " Y q a h T & a X 4 a UP I p v & UP I A 4 a T a u7 4 t3 a 6 4 G s A 6 4 a @ 87 53 4 )
11
T @VUP I
Developing a DP Algorithm for Knapsack Step 3: Bottom-up computing not recursion). Bottom:
for all
row by row.
p q FT a 4 a `P I x X T 4 a `P I iwe d rT V`P I h f Y
V[i,w] w=0 i= 0 0 1 2 . . . n
1 0
2 0
3 0
G G 0
... ...
T VUP I
(using iteration,
0 rT 0 P I Y
... W ... 0
bottom
up
12
1 2 3 4
0 0 0 0 0
1 0 0 0 0 0
Remarks:
The nal output is
The method described does not tell which subset gives the in this example). optimal solution. (It is
y
g
& "
2 0 0 0 0 0
D y
04 Y
and 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
13
q h w
KnapSack(
else
return
14
The algorithm for computing 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 auxiliary boolean array which is 1 if we decide to take the -th le in and 0 otherwise.
Question: How do we use all the values to determine the subset of les having the maximum computing time?
P zyy T (V`B@5x
T VUP I
T V`P I P z y y T {VUB@5x
15
Constructing the Optimal Solution Question: How do we use the values to determine the subset of items having the maximum computing time? If keep[ ] is 1, then this argument for keep[ If keep[ ] is 0, the ment for keep[ ].
P zyy T 7VU|@q5x
4 a 1 ~} ! a 4 a 1 }
4 Y YHT V`P Y Y
T UP a Y
S
r
16
! y e Q p v
output i; ; ;
KnapSack(
return
17
))