ENع
←Greedy choices
Lesson 26 · Greedy and divide and conquer

Greedy choices: the exchange argument and when greedy fails

14 min10 steps5 questionsAdvanced
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".

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 26 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Greedy: 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, $0
  3. Take 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, $60
  4. Take 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, $160
  5. Cut 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, $240
  6. Is $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, $220
  7. The 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, $240
  8. Why 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, $240
  9. Now 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, $160
  10. Greedy 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

Quiz Unlocks after the last step.