DSA Notes
Master the Fractional Knapsack problem — why greedy by value/weight ratio works, complete implementation with fractional items, proof using exchange argument, comparison with 0/1 knapsack, and real-world applications in resource allocation.
Problem Statement
Given items with weights and values, and a knapsack with capacity W, maximize the total value. You can take fractions of items (unlike 0/1 knapsack where each item is all-or-nothing).
| Items | (weight, value) |
| Item 1: (10 kg, $60) | value density = $6/kg |
| Item 2: (20 kg, $100) | value density = $5/kg |
| Item 3: (30 kg, $120) | value density = $4/kg |
| Knapsack capacity | 50 kg |
| Take all of Item 1 | 10 kg, $60 (remaining: 40 kg) |
| Take all of Item 2 | 20 kg, $100 (remaining: 20 kg) |
| Take 2/3 of Item 3 | 20 kg, $80 (remaining: 0 kg) |
| Total | $240 |
Implementation
Proof of Correctness
Theorem: Greedy by value density yields the optimal fractional knapsack solution.
Proof (exchange argument):
Sort items by value density: v₁/w₁ ≥ v₂/w₂ ≥ ... ≥ vₙ/wₙ.
Suppose OPT is any optimal solution. Let x₁, x₂, ..., xₙ be the fractions taken (xᵢ ∈ [0,1]).
Greedy takes fractions g₁, g₂, ..., gₙ where:
- gᵢ = 1 for items until capacity is reached
- One item is partially taken
- gᵢ = 0 for remaining items
Claim: Greedy value ≥ OPT value.
If OPT ≠ Greedy, there exist i < j such that OPT takes less of i than greedy and more of j.
- Since i has higher density: vᵢ/wᵢ ≥ vⱼ/wⱼ
- Shifting ε weight from j to i (in OPT): gain ε·(vᵢ/wᵢ) - lose ε·(vⱼ/wⱼ) ≥ 0
- Value doesn't decrease after the swap
We can keep swapping until OPT matches Greedy without decreasing value. So Greedy value ≥ OPT value, and since OPT is optimal, they're equal. ∎
Fractional vs 0/1 Knapsack
| Feature | Fractional | 0/1 |
|---|---|---|
| Items | Can be split | All-or-nothing |
| Algorithm | Greedy O(n log n) | DP O(n×W) |
| Optimal by greedy | ✅ Yes | ❌ No |
| Value density heuristic | ✅ Correct | ❌ Can fail |
| Practical example | Filling bag with sand | Packing indivisible boxes |
Why 0/1 knapsack cannot be solved greedily:
| Items | [(w=3,v=4),(w=4,v=5),(w=5,v=6)], capacity=7 |
| Densities | 4/3≈1.33, 5/4=1.25, 6/5=1.2 |
| Total | $9 |
| Actually | take only item 2: v=5, or item 3: v=6, or items 1+2: v=9 |
| Wait, items 1+2 | w=3+4=7≤7, v=4+5=9 ← same as greedy here |
| Better example | items=[(2,3),(3,4),(4,5)], cap=5 |
| Densities | 1.5, 1.33, 1.25 |
| Greedy | take item1(2kg,3$) then item2(3kg,4$) but 2+3=5≤5, total=7 |
| Optimal | take item1+item2: 7$ ← same as greedy in this case! |
| True failure | items=[(1,1),(2,6),(3,10),(5,16)], cap=7 |
| Densities | 1, 3, 3.33, 3.2 |
| Greedy | item3(3,10), item2(2,6), item1(1,1)... wait 3+2+1=6≤7, value=17 |
| Actually take item4(5,16) | remaining=2, take item2(2,6): value=22 |
| Greedy with correct density | item3 density=3.33 first: w=3, value=10 |
| remaining=4 | item4 density=3.2: w=5>4, take 4/5 fraction: value=16×(4/5)=12.8 |
| Fractional total | 22.8 |
| For 0/1 | item4+item2=5+2=7, value=22 (optimal for 0/1) |
| Greedy 0/1 | take item3(3,10), item2(2,6), item1(1,1): total=17 ← suboptimal! |
Continuous Knapsack Applications
Summary
Fractional Knapsack:
- Greedy choice: Sort by value/weight ratio (value density), take items greedily
- Time: O(n log n) for sorting + O(n) for selection
- Correct: Exchange argument proves optimality
- Key difference from 0/1: Fractions allowed → greedy works
The ability to take fractions is what makes greedy correct. Without fractions (0/1 knapsack), greedy fails — use dynamic programming instead.
*Next Lesson: Huffman Coding*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Fractional Knapsack — Greedy by Value Density with Proof.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, greedy, fractional, knapsack
Related Data Structures & Algorithms Topics