Download presentation
Presentation is loading. Please wait.
1
9.2 Knapsack Problem There are many other versions
2
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
3
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.
4
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).
5
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.
6
Hence DP functional Equation
7
(NILN) V s n f(s) = ?
8
(NILN) V s n s-v n f(s) = f(s-v n ) + w n V s n f(s) = ?
9
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.
10
Notation As we solve the functional equation for s=0,1,2,...,V - in this order (why?) we store the optimal decisions. ie.
11
Since there is no item-type with v n =1, we introduce a dummy variable n=4, with w 4 =0 and v 4 =1 9.2.2 Example
12
V =13
13
Procedure f(0)=0 N(1)={4} N(2)={4}
14
N(3)={2}
15
N(4)={3}
16
N(5)={3,4}
18
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)
19
Important Reminder Do not forget to check the results!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.