9.2 Knapsack Problem There are many other versions
Interpretation n = 1,2,..., N - Item-type V = Volume of knapsack v n = Volume of an item of type n w n = Weight of an item of type n x n = Number of items of type n put in the knapsack
Observation If the integrality constraint {x n is in {0,1,2,...}} is ignored, and is replaced by say a nonnegativity constraints, then the problem is trivial (why?) There are many variations on this theme, eg 0-1 knapsack problems.
DP Strategy Let, f(s):= maximum weight of knapsack with available volume of s units, s=0,1,2,...,V. We are interested in f(V). Clearly, f(0)=0 Clearly, f(s) =0 for all s smaller than the smallest item (volume-wise).
Clearly, if f(s)>0, then f(s) = f(s-v n ) + w n for some item-type n. Obvious question: What is the best n ? Answer: The n that maximizes f(s-v n ) + w n.
Hence DP functional Equation
(NILN) V s n f(s) = ?
(NILN) V s n s-v n f(s) = f(s-v n ) + w n V s n f(s) = ?
Minor “Correction ” The functional equation is not valid for s smaller than the smallest v n. Fixes: –Set f(s)=0 for all s <min{v 1,...,v N } –Introduce a dummy item-type, with w n =0 and v n =1. In the Lecture Notes we use the latter.
Notation As we solve the functional equation for s=0,1,2,...,V - in this order (why?) we store the optimal decisions. ie.
Since there is no item-type with v n =1, we introduce a dummy variable n=4, with w 4 =0 and v 4 = Example
V =13
Procedure f(0)=0 N(1)={4} N(2)={4}
N(3)={2}
N(4)={3}
N(5)={3,4}
Recovery of Optimal Solution Using the table we select the optimal items from N(s) and then go to N(s-v n ). x (0) =(0,0,0,0), s=V=13 x (1) =(0,0,1,0) (N(13)={2,3}), next s = s-v 3 = 9 x (2) =(0,1,1,0) (N(9)={2}, next s = s-v 2 = 6 x (3) =(0,2,1,0) (N(6)={2}, next s = s-v 2 = 3 x (4) =(0,3,1,0) (N(3)={2}, next s = s-v 2 = 0 x*=(0,3,1,0)
Important Reminder Do not forget to check the results!