0% found this document useful (0 votes)
75 views13 pages

L12 Fractional Knapsack Greedy

The greedy algorithm for the fractional knapsack problem involves sorting items by their value per unit weight in descending order. It then packs the knapsack by taking as much of the highest value per unit weight item as possible, then moving on to the next highest, until the knapsack is full. This approach provides an optimal solution by always selecting items that provide the greatest value for their weight.

Uploaded by

Shivansh Rag
Copyright
© © All Rights Reserved
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)
75 views13 pages

L12 Fractional Knapsack Greedy

The greedy algorithm for the fractional knapsack problem involves sorting items by their value per unit weight in descending order. It then packs the knapsack by taking as much of the highest value per unit weight item as possible, then moving on to the next highest, until the knapsack is full. This approach provides an optimal solution by always selecting items that provide the greatest value for their weight.

Uploaded by

Shivansh Rag
Copyright
© © All Rights Reserved
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/ 13

Greedy Algorithms

Fractional Knapsack
Greedy Algorithms
• Similar to dynamic programming, but simpler approach
– Also used for optimization problems

• Idea: When we have a choice to make, make the one that


looks best right now
– Make a locally optimal choice in hope of getting a globally optimal
solution

• Greedy algorithms don’t always yield an optimal solution


• Makes the choice that looks best at the moment in order to
get optimal solution.
Knapsack Problem
Knapsack Problem
• One wants to pack n items in a luggage
– The ith item is worth vi dollars and weighs wi pounds
– Maximize the value but cannot exceed W pounds
– vi , wi, W are integers
• 0-1 knapsack  each item is taken or not taken
• Fractional knapsack  fractions of items can be taken
• Both exhibit the optimal-substructure property
– 0-1: If item j is removed from an optimal packing, the remaining
packing is an optimal packing with weight at most W-wj
– Fractional: If w pounds of item j is removed from an optimal
packing, the remaining packing is an optimal packing with weight
at most W-w that can be taken from other n-1 items plus wj – w of
item j
Greedy Algorithm for Fractional
Knapsack problem
• Fractional knapsack can be solvable by the greedy
strategy
– Compute the value per pound vi/wi for each item
– Obeying a greedy strategy, take as much as possible of the
item with the greatest value per pound.
– If the supply of that item is exhausted and there is still more
room, take as much as possible of the item with the next value
per pound, and so forth until there is no more room
– O(n lg n) (we need to sort the items by value per pound)
– Greedy Algorithm?
– Correctness?
Fractional Knapsack Problem
• Knapsack capacity: W

• There are n items: the i-th item has value vi and weight wi

• Goal:

– find xi such that for all 0  xi  1, i = 1, 2, .., n

 wixi  W and

 xivi is maximum
Fractional Knapsack - Example
20
• E.g.: ---
$80
Item 3 30 +

Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60

$60 $100 $120 $240

$6/pound $5/pound $4/pound


Fractional Knapsack Problem
• Greedy strategy 1:
– Pick the item with the maximum value
• E.g.:
– W=1
– w1 = 100, v1 = 2
– w2 = 1, v2 = 1
– Taking from the item with the maximum value:
Total value taken = v1/w1 = 2/100
– Smaller than what the thief can take if choosing the
other item
Total value (choose item 2) = v2/w2 = 1
Fractional Knapsack Problem
Greedy strategy 2:

• Pick the item with the maximum value per pound vi/wi

• If the supply of that element is exhausted and the thief can carry
more: take as much as possible from the item with the next greatest
value per pound

• It is good to order items based on their value per pound

v1 v2 vn
  ... 
w1 w2 wn
Fractional Knapsack Problem (Algo)

• Running time: (n) if items already ordered; else (nlgn)


Fractional Knapsack Problem
Example
Let us consider that the capacity of the knapsack W = 60 and the
list of provided items are shown in the following table −

As the provided items are not sorted. After sorting, the items are
as shown in the following table.
Fractional Knapsack Problem
Example

Solution
First all of B is chosen as weight of B is less than the capacity of
the knapsack.
Next, item A is chosen, as the available capacity of the knapsack is
greater than the weight of A.
Now, C is chosen as the next item. However, the whole item
cannot be chosen as the remaining capacity of the knapsack is less
than the weight of C.
Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
Fractional Knapsack Problem
Example

Now, the capacity of the Knapsack is equal to the selected items.


Hence, no more item can be selected.
The total weight of the selected items is 10 + 40 + 20 * (10/20) =
60
And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 =
440
This is the optimal solution.
We cannot gain more profit selecting any different combination
of items.

You might also like