0% found this document useful (0 votes)
340 views5 pages

Greedy Knapsack

1. Greedy algorithm that selects the most valuable item first. This does not always find an optimal solution. 2. Greedy algorithm that selects the lighter item first. This does not always find an optimal solution. 3. Greedy algorithm that selects the item with the highest value-to-weight ratio. This algorithm can find an optimal solution by always selecting the item with the best ratio.

Uploaded by

Rajeev Vishal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views5 pages

Greedy Knapsack

1. Greedy algorithm that selects the most valuable item first. This does not always find an optimal solution. 2. Greedy algorithm that selects the lighter item first. This does not always find an optimal solution. 3. Greedy algorithm that selects the item with the highest value-to-weight ratio. This algorithm can find an optimal solution by always selecting the item with the best ratio.

Uploaded by

Rajeev Vishal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Knapsack problems Knapsack (knapsack)

We have n objects and a knapsack.


Maximum Integer Knapsack (max-knapsack) The i-th object has positive weight wi , and the knapsack has capacity C.
We have n objects and a knapsack. It is possible to select a set of objects to put in the knapsack, so that is total
The i-th object has positive weight wi and positive value vi . weight is C.
The knapsack capacity is C.
We wish to select a set of objects to put in the knapsack so that the total values
is maximum and without breaking the knapsack.

Maximum Fractional Knapsack (max-f-knapsack)


We have n objects and a knapsack. function FracKnapsackGreedy(w[1 .. n], v[1 .. n], M ) : array [1 .. n] of rational

The i-th object has positive weight wi and positive unit value vi .
The objects are sorted in increasing order of (*) · · ·
The knapsack capacity is C. for i := 1 to n do
We wish to select a set of proportions of objects to put in the knapsack so that x[i] := 0
the total values is maximum and without breaking the knapsack. end for;
weight := 0; i := 1;
while weight < M ∧ i 6 n do
if weight + w[i] 6 M
then x[i] := 1; weight := weight + w[i];
else x[i] := (M − weight)/w[i]; weight := M
end if ;
i := i + 1
end while;
return (x)
end

(*) Different selections will provide different greedy algorithms.


In any reasonable sorting criteria the algorithm’s cost is polynomial.
Fractional Knapsack an example Select always the lighter object
Input: 5 objects, C = 100 object 1 2 3 4 5
w 10 20 30 40 50 selected 1 1 1 1 0
v 20 30 66 40 60 Total selected weight 100 and total value 156.

Input: 5 objects, C = 100

w 10 20 30 40 50
v 20 30 66 40 60

Select always the most valuable object Select always the object with highest ratio value/weight
object 1 2 3 4 5 object 1 2 3 4 5
selected 0 0 1 0.5 1 ratio 2.0 1.5 2.2 1.0 1.2
Total selected weight 100 and total value 146. selected 1 1 1 0 0.8
Total selected weight 100 and total value 164.
Input: 5 objects, C = 100

w 10 20 30 40 50 Input: 5 objects, C = 100


v 20 30 66 40 60 w 10 20 30 40 50
v 20 30 66 40 60
Theorem: The greedy algorithm that always selects the most valuable object does Let Y = (y1 , . . . , yn ) be any feasible solution, we have
not always find an optimal solution to the Fractional Knapsack problem. n n
X X
yi wi 6 W = xi wi
i=1 i=1

therefore
n
X n
X
xi wi − yi wi > 0.
Theorem: The greedy algorithm that always selects the lighter object does not i=1 i=1

always find an optimal solution to the Fractional Knapsack problem. Let V (Z) denote the total value of a feasible solution.
n n
X X vi
V (X) − V (Y ) = (xi − yi )vi = (xi − yi )wi .
i=1 i=1
wi

vi vj
Theorem: The greedy algorithm that always selects the object with better ratio If i < j, xi = 1 then xi − yi > 0 and wi > wj and we have
value/weight always finds an optimal solution to the Fractional Knapsack problem.
vi vj
(xi − yi ) > (xi − yi )
Proof. Assume that the objects are {1, . . . , n} and that wi wj

v1 v2 vn
> > ··· > . vi vj
w1 w2 wn If i > j, xi = 0 then xi − yi 6 0 but wi 6 wj therefore
vi vj
(xi − yi ) > (xi − yi )
Let X = (x1 , . . . , xn ) be the solution computed by the greedy algorithm. wi wj

If xi = 1 for all i, the solution is optimal.


Otherwise, let j be the smallest value for which xj < 1.
Plugging the inequality we have
According with the algorithm we have:
If i < j then xi = 1, and if i > j then xi = 0. n n n
X vi X vj vj X
Pn
Furthermore i=1 xi wi = W V (X) − V (Y ) = (xi − yi )wi > (xi − yi )wi > (xi − yi )wi > 0
i=1
wi i=1
wj wj i=1

Therefore X is an optimal solution.


Exercise 3 0-1 Knapsack
Consider the algorithms We have a set I of n items, item i has weight wi and worth vi . We can carry at
most weight W in our knapsack. Considering that we can NOT take fractions of
function KnapsackGreedy(w[1 .. n], v[1 .. n], M ) : array [1 .. n] of rational items, what items should we carry to maximize the profit?

The objects are sorted in increasing order of (*) · · · Let v[i, j] be the maximum value we can get from objects {1, 2, . . . , i} and taking a
for i := 1 to n do maximum weight of 0 6 j 6 W .
x[i] := 0
end for;
We wish to compute v[n, W ].
weight := 0; i := 1; To compute v[i, j] we have two possibilities: The i-th element is or is not part of
while weight < M ∧ i 6 n do
the solution.
if weight + w[i] 6 M
then x[i] := 1; weight := weight + w[i]; This gives the recurrence,
end if ; 
i := i + 1 v[i − 1, j − w ] + v if the i-th element is part of the solution
i i
end while; v[i, j] =
v[i − 1, j] otherwise
return (x)
end

Where (*) is: Define a table v[1 . . . n, 0 . . . W ],


2 value Initial condition: ∀j, v[0, j] = 0

2 decreasing weights To compute v[i, j] must look to v[i − 1, j] and to v[i − 1, j − wi ].

2 ratio value/weight v[n, W ] will indicate the profit.

Any of the proposed algorithms is optimal for integer knapsack?


Example. Question
Let I = {1, 2, 3, 4, 5} with v(1) = 1; v(2) = 6; v(3) = 18; v(4) = 22; v(5) = 28,
As you already know 0-1 Knapsack is NP-hard.
w(1) = 1; w(2) = 2; w(3) = 5; w(4) = 6; w(5) = 7 and W = 11.
But the previous algorithm has time complexity O(nW ). Therefore ¶=NP!
0 1 2 3 4 5 6 7 8 9 10 11 Is something wrong?
1 0 1 1 1 1 1 1 1 1 1 1 1
2 0 1 6 7 7 7 7 7 7 7 7 7
3 0 1 6 7 7 18 19 24 25 25 25 25
4 0 1 6 7 7 18 22 24 28 29 29 40
5 0 1 6 7 7 18 22 28 29 34 35 40

v[3, 5] = max{v[2, 5], v[2, 0] + v(3)} = max{7, 0 + 18} = 18

The time complexity is O(nW ). Exercise


Notice, that at each computation of v[i, j] we just need to store two rows of the Modify the 0-1 Knapsack algorithm so that in addition to computing the optimal
table, therefore the space complexity is 2W cost it computes an optimal solution.

You might also like