Greedy choices: the exchange argument and when greedy fails
About this lesson
A greedy algorithm builds its answer one choice at a time, always taking what looks best right now and never going back. Fill a 50 kg bag from three items by best value per kilogram, prove with an exchange argument that no other load can beat it, then change one rule (items cannot be cut) and watch the same greedy rule lose $60. It uses the sorting from the Software lesson "Sorting" and the proof habits from the Maths lesson "Proofs and induction".
Fill a 50 kg bag
A bag holds at most 50 kg. Item A weighs 10 kg and is worth $60, B weighs 20 kg for $100, C weighs 30 kg for $120. All three weigh 60 kg, too much. Which load is worth the most?
capacity 50 kg A 10 kg $ 60 B 20 kg $100 C 30 kg $120 all three: 60 kg > 50 kg bag: empty → 0 kg, $0
Read the lesson step by step
Fill a 50 kg bag
A bag holds at most 50 kg. Item A weighs 10 kg and is worth $60, B weighs 20 kg for $100, C weighs 30 kg for $120. All three weigh 60 kg, too much. Which load is worth the most?
capacity 50 kg A 10 kg $ 60 B 20 kg $100 C 30 kg $120 all three: 60 kg > 50 kg bag: empty → 0 kg, $0Greedy: best value per kg
A greedy algorithm makes the choice that looks best right now and never takes it back. Here: the most dollars per kilogram first. A earns $6/kg, B $5/kg, C $4/kg, so the code sorts them A, B, C.
rate = value / weight A 60 / 10 = $6/kg B 100 / 20 = $5/kg C 120 / 30 = $4/kg items.sort(key=rate, reverse=True) → A, B, C bag: empty → 0 kg, $0Take all of A
A fits, so the loop takes all 10 kg of it: take = min(10, 50) = 10. The bag now holds 10 kg worth $60, with 40 kg of room left.
take = min(10, 50) = 10 got += 60 * 10 / 10 → 60 cap -= 10 → 40 bag: A 10 kg → 10 kg, $60Take all of B
B is next best. All 20 kg fit too. The bag holds 30 kg worth $160, and 20 kg of room is left.
take = min(20, 40) = 20 got += 100 * 20 / 20 → 160 cap -= 20 → 20 bag: A 10 kg + B 20 kg → 30 kg, $160Cut C to fit
C weighs 30 kg, but only 20 kg of room is left. In this version items can be cut, like flour, so take = min(30, 20) = 20: two thirds of C, worth $80. The bag is full and worth $240.
take = min(30, 20) = 20 got += 120 * 20 / 30 → 240 cap -= 20 → 0 greedy(...) returns 240.0 bag: A 10 kg + B 20 kg + C 20 kg → 50 kg, $240Is $240 the best?
Greedy never looked ahead, so we must prove it is right. Compare it with any other full bag, say all of B and all of C. It also weighs 50 kg, but it is worth $220, and A stays out.
another full bag: B + C 20 + 30 = 50 kg 100 + 120 = $220 < $240 A ($6/kg) is left outside bag: B 20 kg + C 30 kg → 50 kg, $220The exchange argument
Swap 10 kg of C ($4/kg) out for the 10 kg of A ($6/kg) left outside. The weight stays 50 kg and the value rises by $20, to $240. Trading toward a better $/kg never loses value.
swap 10 kg: C out, A in value 220 − 40 + 60 = 240 weight 50 − 10 + 10 = 50 gain = 10 × ($6 − $4) = $20 bag: A 10 kg + B 20 kg + C 20 kg → 50 kg, $240Why greedy wins
Any full bag that holds worse kilograms while better ones sit outside can make such a swap without losing value. Repeating the swaps turns it into the greedy bag. So no bag is worth more than $240.
exchange argument any full bag ≠ greedy bag holds some lower-rate kg while higher-rate kg are out swap them: value does not drop repeat until it IS the greedy bag so every bag ≤ $240 bag: A 10 kg + B 20 kg + C 20 kg → 50 kg, $240Now items cannot be cut
Change one rule: each item goes in whole or not at all. This is the 0/1 knapsack. Greedy still takes A, then B: 30 kg, $160. C needs 30 kg, only 20 are left, so C stays out.
0/1 knapsack: whole items only A fits: 10 kg, $60 B fits: 30 kg, $160 C: 30 kg > 20 kg left → skip greedy_whole(...) returns 160 bag: A 10 kg + B 20 kg → 30 kg, $160Greedy fails
Yet B and C, leaving A out, weigh exactly 50 kg and are worth $220, more than $160. The swap trick breaks: you cannot trade 10 kg of C without taking all 30. This version needs dynamic programming instead.
check all 2³ = 8 subsets of A, B, C: best: B + C = 50 kg, $220 greedy: $160 < $220 no 10 kg slices to swap bag: B 20 kg + C 30 kg → 50 kg, $220
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which statement describes a greedy algorithm?
Greedy commits to one locally best choice at each step. Trying everything is brute force, splitting in half is divide and conquer, and a table of smaller answers is dynamic programming.
02Items can be cut. The bag holds 50 kg. A is 10 kg for $60, B is 20 kg for $100, C is 30 kg for $120. What does greedy by value per kg get?
Rates are $6, $5 and $4 per kg. Take all of A ($60) and all of B ($100), then 20 of C's 30 kg, worth 120 × 20 / 30 = $80. Total $240.
03In the exchange argument, a full bag holds 10 kg of C ($4/kg) while 10 kg of A ($6/kg) is left out. What does swapping them do?
Same 10 kg out and in, so the weight is unchanged. The value changes by 10 × ($6 − $4) = +$20. Swapping toward a better rate never loses value, which is why no bag beats the greedy one.
04Now items must go in whole. Why does the same greedy rule fail?
Greedy takes A and B (30 kg, $160), and C needs 30 kg with only 20 left. B + C weighs 50 kg and is worth $220. The exchange argument breaks because you can no longer trade 10 kg slices of C.
05You pay 6 with coins worth 4, 3 and 1, always using the largest coin that fits. How many coins does greedy use, and what is the fewest possible?
Greedy picks 4, then 1, then 1: three coins. But 3 + 3 uses only two. Another case where the locally best choice is not globally best.
Glossary
- Greedy algorithm
- An algorithm that builds an answer step by step, each time taking the choice that looks best right now and never undoing it.
- Greedy-choice property
- A problem has it when some best answer starts with the greedy choice, so committing to that choice early is safe.
- Optimal substructure
- A best answer to the whole problem contains best answers to the smaller problems left after the first choice.
- Exchange argument
- A proof style: take any best answer that differs from the greedy one, swap one part toward the greedy choice, show it is no worse, and repeat until the two match.
- Fractional knapsack
- Fill a bag of limited weight with items you may cut, to get the most value. Greedy by value per weight solves it.
- 0/1 knapsack
- The same problem, but each item goes in whole or not at all. Greedy can fail; dynamic programming solves it.
- Dynamic programming
- A method that solves every smaller version of a problem once, stores each answer in a table, and builds the full answer from them.
- Counterexample
- One concrete input on which a claim is false. A single one is enough to show a greedy rule is not always optimal.
Notes and sources
- The three items and the 50 kg bag are the example CLRS (Introduction to Algorithms) uses for the fractional and 0/1 knapsack problems; the book measures them in pounds.
- The exchange argument in general: take any optimal answer that differs from the greedy one, change one part of it toward the greedy choice, and show the value does not get worse. Repeat until the two are the same. This proves the greedy-choice property; optimal substructure (what is left is a smaller problem of the same kind) finishes the proof.
- The 0/1 knapsack can be solved exactly by dynamic programming in O(n·W) time for n items and capacity W (MIT 6.006, Recitation 18). That is pseudopolynomial: it grows with the number W, not with the number of digits needed to write W.
- The code is real Python 3. greedy([(10, 60), (20, 100), (30, 120)], 50) returns 240.0; the whole-items version returns 160. list.sort with reverse=True keeps equal rates in their original order, because Python sorts are stable.
- Greedy fails in many places. Paying 6 with coins of 4, 3 and 1, largest coin first gives 4 + 1 + 1 (three coins), but 3 + 3 uses two.
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 15: Greedy Algorithms
- MIT 6.006 Introduction to Algorithms (Spring 2020), Recitation 18: 0-1 knapsack, where greedy fails
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Lecture 12: Greedy Algorithms
- Sedgewick and Wayne, Algorithms 4th ed., §4.3 Minimum Spanning Trees (greedy algorithms)
- Python documentation: list.sort (key, reverse, stability)
- Continuous knapsack problem (greedy by value-to-weight ratio, exchange argument)
Quiz Unlocks after the last step.