Lecture 6
Lecture 6
Technique-2
Another Greedy Design Problem: Fractional Knapsack
• Suppose that you are going for a long hike. You want to take some food
with you but the bag that you carry, only has a limited capacity.
• You aim is to fill your bag with as much expensive food item as possible.
• The problem is called fractional because you can cut out a portion of your
food item to put in your bag(knapsack).
• This optimization problem can be solved using greedy approach because
we are allowed to take a portion of our food items rather than taking it as a
whole.
• Each food item has a price associated with it.
The Fractional Knapsack Problem
Formalization
• Input: weights 𝑤1 … 𝑤𝑛 and values 𝑣1 … 𝑣𝑛 of 𝑛 items. Capacity 𝑊.
• Output: The maximum total value of fractions of items that fits into
the 𝑊.
Safe Move
• There exists an optimal solution that uses as much as possible of an
item with the maximal value per unit of weight.
The Greedy Algorithm
• While knapsack is not full.
𝑣𝑖
• Choose item 𝑖 with maximum .
𝑤𝑖
• If the item fits into the knapsack, take all of it.
• Otherwise take so much as to fill the knapsack.
• Return total value and amounts taken.
An Algorithm
1. Knapsack(𝑊, 𝑤, 𝑣)
2. let 𝐴[1 … 𝑤. 𝑙𝑒𝑛𝑔𝑡ℎ] be an array initialized to 0
3. 𝑉 = 0
𝑣[𝑖]
4. create an array of:
𝑤[𝑖]
5. for 𝑖 = 1 to 𝑤. 𝑙𝑒𝑛𝑔𝑡ℎ
6. if 𝑊 == 0
7. return (𝑉, 𝐴)
8. 𝑎 = min 𝑤[𝑖], 𝑊
𝑣[𝑖]
9. 𝑉 =𝑉+𝑎
𝑤[𝑖]
10. 𝑤[𝑖] = 𝑤[𝑖] − 𝑎
11. 𝐴[𝑖] = 𝑎
12. 𝑊 =𝑊−𝑎
13. return (𝑉, 𝐴)
Better one:
1. Knapsack(𝑊, 𝑤, 𝑣)
2. let 𝐴[1 … 𝑤. 𝑙𝑒𝑛𝑔𝑡ℎ] be an array initialized to 0
3. 𝑉 = 0
𝑣[𝑖]
4. //sort items by decreasing order of
𝑤[𝑖]
5. for 𝑖 = 1 to 𝑤. 𝑙𝑒𝑛𝑔𝑡ℎ
6. if 𝑊 == 0
7. return (𝑉, 𝐴)
8. 𝑎 = min 𝑤[𝑖], 𝑊
𝑣[𝑖]
9. 𝑉 =𝑉+𝑎
𝑤[𝑖]
10. 𝑤[𝑖] = 𝑤[𝑖] − 𝑎
11. 𝐴[𝑖] = 𝑎
12. 𝑊 =𝑊−𝑎
13. return (𝑉, 𝐴)
Some Observations
𝑣𝑖
• If we sort the input with respect to their for all 𝑖 then we will start
𝑤𝑖
with the item that has this ratio, the largest.
𝑣𝑖
• Otherwise we will have to repeatedly chose the item whose is
𝑤𝑖
higher than what has been seen before previously, costing us 𝑂 𝑛2
time.
The Running Time
• Note the runtime 𝑇 𝑛 = 𝑂 𝑛 + 𝑂(𝑛 lg 𝑛).
• The first 𝑂 𝑛 is the number of iterations and the second 𝑂(𝑛 lg 𝑛) is
the time taken for sorting.
• Since 𝑛 = 𝑂(𝑛 lg 𝑛) we can conclude that 𝑇 𝑛 = 𝑂(𝑛 lg 𝑛).
Review of The Greedy Strategy: Reduction to
Subproblem
• Make a first move
• Then solve a problem of the same kind
• Smaller: fewer weight capacity left, fewer fuel stations etc. This is
called a “subproblem”
Review of The Greedy Strategy: Safe Move
• A move is called safe if there is an optimal solution consistent with
this first move
• Not all first moves are safe