Académique Documents
Professionnel Documents
Culture Documents
CLRS-Chapter 16
2 pd 2 pd 3 pd
2 pounds of item A
Solution = +
2 pounds of item C
2 pds 2 pds
A C
$100 $80
2.
0-1 Knapsack Problem
Thief can only take or leave item. He can’t
take a fraction.
Solution = 3 pounds of item C
3 pds
C
$120
Fractional Knapsack has a greedy solution
B
C
1 A 3 2 5 150
pd pd 240 pd 140 pd
200
cost 200 80 70 30
weight
1. Calculate vi = ci / wi for i = 1, 2, …, n
2. Sort the item by decreasing vi
3. Find j, s.t.
w1 + w2 +…+ wj ≤ k < w1 + w2 +…+ wj+1
wi pds item i, for i ≤ j
Answer is
{ K-Σ i≤ j wi pds item j+1
The 0-1 Knapsack Problem
does not have a greedy solution!
Example:
A
B C
3 2 2
pd 300 pd 190 pd 180
cost 100 95 90
weight
K=4
Solution is item B + item C
Best algorithm known is the O(nK) DP one
developed earlier.