Algorithms · Greedy Method and Fractional Knapsack
Fractional Knapsack Problem
Overview
The fractional knapsack problem asks how to fill a bag of limited capacity with objects of given weight and profit so that total profit is maximized.
It is a problem to which the greedy method applies, and it shows how the choice of greedy criterion (here, profit) determines the selection of objects.
Key concepts
- Knapsack problem: "Knapsack" means bag; the problem is to choose objects to place in the bag so that the maximum profit is obtained without exceeding the bag's capacity.
- Capacity: the bag can hold at most 20 units (20 kg) of any product.
- Objects: three objects are given, each with a weight (number of units) and a profit obtained by selling those units.
- Profit column meaning: selling 18 units of object 1 gives a profit of 25; selling 15 units of object 2 gives a profit of 24; selling 10 units of object 3 gives a profit of 15.
- Greedy strategy on profit: since the aim is to maximize profit, take the object having the maximum profit first.
- Reference: the treatment of the greedy method follows Design and Analysis of Algorithms by Sahani and Horowitz, which contains all these examples.
(The earlier sorting algorithms and divide-and-conquer methods were taken from Introduction to Algorithms by Cormen.)
How it works
- Try to put all the available objects into the bag.
- The bag capacity is only 20 units, while the objects weigh 18, 15 and 10 units.
- Any two objects together exceed 20, so not even more than one object can be placed in the bag.
- Therefore putting all the objects is not possible.
- The remaining question is which object to put first. Strategies differ; one strategy is to be greedy about profit — take the object with the maximum profit first.
Formulas
- Remaining capacity
- For the profit-greedy choice: units remaining.
Worked examples
Greedy strategy: be greedy about profit — pick the object with the maximum profit first.
| Step | Action | Weight used | Profit | Remaining capacity |
|---|---|---|---|---|
| 1 | Take object 1 (maximum profit 25); 18 units of object 1 must be taken to get profit 25 | 18 | 25 |
The object with maximum profit is object 1 (profit 25). To get the profit of 25 from object 1, 18 units of object 1 must be taken, so 18 units of the bag are filled, leaving 2 units of capacity.
Comparison
| Object | Weight (units) | Profit |
|---|---|---|
| 1 | 18 | 25 |
| 2 | 15 | 24 |
| 3 | 10 | 15 |
Revision summary
- Knapsack = bag; the goal is maximum profit subject to the bag's capacity.
- Capacity is 20 units; objects are (18, 25), (15, 24), (10, 15) as (weight, profit).
- Every object weighs at least half the capacity (18, 15 and 10 units against capacity 20), so at most one whole object fits — putting all objects whole is impossible.
- Greedy strategies differ; the first one considered is greedy on profit: take the maximum-profit object first.
- Picking object 1 (profit 25) uses 18 units and leaves only 2 units of capacity.
Previous-year exam insights
- Fractional knapsack is solved optimally by the greedy method: sort items by decreasing profit/weight ratio and take whole items while they fit, then a fraction of the next item to fill the capacity exactly; 0/1 knapsack has no greedy solution and needs dynamic programming.
- The greedy choice property and optimal substructure hold for fractional knapsack: after taking the highest-ratio item (or fraction), the remaining problem is the same problem on the residual capacity, so the greedy choice is safe.
- Time complexity of the ratio-greedy fractional knapsack is dominated by sorting: ; with an already sorted list it is .
- For 0/1 knapsack the DP recurrence is K[i][w] = max(K[i-1][w], value[i] + K[i-1][w-weight[i]]) with time and space, where W is capacity;
fractional knapsack's optimum is always at least the 0/1 optimum. - When items are taken by ratio, the last item may be split; the fraction taken is (remaining capacity)/(weight of that item), and its profit contribution is that fraction times the item's profit.
- Greedy on maximum profit alone or on minimum weight alone is not optimal for knapsack; only the profit/weight ratio criterion guarantees the optimal fractional solution.