21 Knapsack Problem
21 Knapsack Problem
1
KNAPSACK PROBLEM
2
KNAPSACK PROBLEM VARIANTS &
ITS DIFFERENCES
Variants:
• 0/1 Knapsack. • Fractional Knapsack
• can break items for maximizing the
• not allowed to break items. We total value of knapsack
either take the whole item or don’t
take it.
4kg 8kg
$2 $17
3
0/1 KNAPSACK PROBLEM
In 0/1 Knapsack Problem,
Example:
Consider the knapsack instance n = 3, (w1, w2, w3) = (2, 3, 4), (p1, p2, p3) = (1, 2, 5)
and m = 6.
Probability of Chosen Items (xi) = [{0, 0, 0}, {0, 0, 1}, ……, {1, 1, 1}]
5
0/1 KNAPSACK PROBLEM
3. The formulas that are used while solving 0/1 knapsack
problem.
Let fn(m) be the value of an optimal solution, then
fn(m)= max { fn-1(m), fn-1( m - wn) + pn }
General formula
fi(y)= max { fi-1(y), fi-1( y - wi) + pi }
6
0/1 Knapsack Problem – Solution
Consider the knapsack instance n = 3, (w1, w2, w3) = (2, 3, 4), (p1, p2, p3) = (1, 2, 5) and m = 6.
f2(6)= max { f1(6), f1( 6-3) + 2 } f2(2)= max { f1(2), f1( 2-3) + 2 }
f2(6)= max { f1(6), f1( 3) + 2 } f2(2)= max { f1(2), f1( -1) + 2 }
0/1 Knapsack Problem – Solution
Consider the knapsack instance n = 3, m=6
(w1, w2, w3) = (2, 3, 4) & (p1, p2, p3) = (1, 2, 5).
f1(6)= max { f0(6), f0( 6-2) + 1 } f1(3)= max { f0(3), f0( 3-2) + 1 }
f1(6)= max { f0(6), f0( 4) + 1 } f1(3)= max { f0(3), f0( 1) + 1 }
f1(6)= max { 0, 0 + 1 } = 1 f1(3)= max { 0, 0 + 1 } = 1
0/1 Knapsack Problem – Solution
Consider the knapsack instance n = 3, m=6
(w1, w2, w3) = (2, 3, 4) & (p1, p2, p3) = (1, 2, 5).
f1(6)= max { f0(6), f0( 6-2) + 1 } f1(3)= max { f0(3), f0( 3-2) + 1 }
f1(6)= max { f0(6), f0( 4) + 1 } f1(3)= max { f0(3), f0( 1) + 1 }
f1(6)= max { 0, 0 + 1 } = 1 f1(3)= max { 0, 0 + 1 } = 1
0/1 Knapsack Problem – Solution
Consider the knapsack instance n = 3, m=6
(w1, w2, w3) = (2, 3, 4) & (p1, p2, p3) = (1, 2, 5).
f3(6)= max { 3, 1 + 5 }
f3(6)= max { 3, 1 + 5 } = 6
0/1 Knapsack Problem – Solution: Set
Method
Initially S0 = {(0,0)}
Purging or dominance rule: if Si+1 contains two pairs (pj, wj ) and (pk, wk ) with
the property that pj ≤ pk and wj ≥ wk then the pair (pj, wj ) can be discarded.
When generating Si, we can also purge all pairs ( p, w ) with w > m as these pairs
Initially S0 = {(0,0)}
S0 = {(0,0)} S10 ={ ( 1, 2) }
S2 = {(0, 0), (1, 2), (2, 3), ( 3, 5) } S12 ={ (5, 4), ( 6, 6), (7, 7), (8, 9) }
S3 = {(0, 0), (1, 2), (2, 3), ( 3, 5), (5, 4), ( 6, 6), (7, 7), (8, 9) }
S0 = {(0,0)} S10 ={ ( 1, 2) }
S2 = {(0, 0), (1, 2), (2, 3), ( 3, 5) } S12 ={ (5, 4), ( 6, 6), (7, 7), (8, 9) }
S3 = {(0, 0), (1, 2), (2, 3), ( 3, 5), (5, 4), ( 6, 6)}
S0 = {(0,0)} S10 ={ ( 1, 2) }
S2 = {(0, 0), (1, 2), (2, 3), ( 3, 5) } S12 ={ (5, 4), ( 6, 6), (7, 7), (8, 9) }
S0 = {(0,0)} S10 ={ ( 1, 2) }
S2 = {(0, 0), (1, 2), (2, 3), ( 3, 5) } S12 ={ (5, 4), ( 6, 6), (7, 7), (8, 9) }
Therefore ,
S3 ={(0,0)(1,2)(2,3)(5,4)(6,6)}
X=(1,0,1)
25
0/1 KNAPSACK ALGORITHM
Algorithm DKP(p,w,n,m)
{
S0 := {(0,0)};
for i := 1 to n-1 do
{
Si-1 ={(P,W)|(P-pi ,W-wi ) ϵ Si-1 and W <= m);
Si = MergePurge(Si-1 , S1i-1);
26
(PX,WX) = last pair in Sn-1 ;
(PY,WY)=(P1+ pn ,W1+wn ) where W1 is the largest W in any pair in Sn-1 suc
that W+ wn <= m;
27
COMPLEXITY ANALYSIS
28
29
30
Consider the knapsack instance n = 3, m=50, (w1, w2, w3) = (10,20,30)
31
SAMPLE QUESTIONS
32