Sub topic 3
Why a greedy choice is safe: the exchange argument
This chapter assumes you know Big-O notation, can read short Python, and have seen the idea of a greedy algorithm: build a solution by repeatedly taking the choice that looks best right now. The question here is how to prove that such a choice never costs you the optimum.
What a greedy proof must show
A greedy algorithm is correct only if two properties hold.
- Greedy-choice property: some optimal solution contains the greedy first choice.
- Optimal substructure: after making that choice, what remains is a smaller instance of the same problem, and an optimal solution to it combines with the choice to give an optimal solution to the whole.
The exchange argument proves the first property. It does not prove that the algorithm finds every optimal solution. It proves that at least one optimal solution agrees with the greedy choice, and that is enough.
The exchange argument in four steps
- Fix any optimal solution
OPT. Call the greedy algorithm's first choiceg. - If
gis already inOPT, you are done. - Otherwise pick an element
xinOPTand swap it forg, producingOPT'. - Show that
OPT'is still feasible and no worse thanOPT. SoOPT'is optimal and containsg.
Apply the same step to the remaining subproblem, again and again, and you conclude that the whole greedy solution is optimal. This is induction on the number of choices.
Worked example: activity selection
The rule is: sort activities by finish time and always pick the one that finishes first among those compatible with what you have chosen.
Claim. Let g be the activity with the earliest finish time. Some optimal schedule contains g.
Proof. Let OPT be an optimal schedule with its activities sorted by finish time, and let a be its first activity.
- If
a = g, we are done. - Otherwise
finish(g) <= finish(a), becausegfinishes earliest of all activities. - Every other activity in
OPTstarts at or afterfinish(a). So it also starts at or afterfinish(g). - Replace
awithg. The new scheduleOPT'has no overlaps and the same number of activities.
So OPT' is optimal and contains g. The remaining problem is to schedule the activities that start at or after finish(g), and the same argument applies to it.
You can check the result on the standard instance. Sorted by finish time, the code picks (1, 4), (5, 7), (8, 11), (12, 16).
def max_activities(intervals):
count = 0
last_end = float('-inf')
for start, finish in sorted(intervals, key=lambda x: x[1]):
if start >= last_end:
count += 1
last_end = finish
return count
print(max_activities([(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)])) # 4Choosing the right greedy rule
The proof is also how you find out which rule is wrong. A rule fails when you cannot complete the swap.
| Rule for activity selection | Swap works? | Counterexample |
|---|---|---|
| Earliest finish time | Yes | None |
| Earliest start time | No | (0, 10) blocks (1, 2), (3, 4), (5, 6) |
| Shortest duration | No | (4, 6) overlaps (1, 5) and (5, 9), each of which would be compatible with the other |
| Fewest conflicts | No | Fails on some crafted instances |
For earliest start, the first activity in OPT may finish far later than g, so replacing it can collide with its successors. The proof breaks exactly where the algorithm breaks.
A second example: fractional knapsack
Items have weights and values, and you may take fractions of an item. The rule is to take items in decreasing order of value per unit weight.
Suppose OPT uses less of the best-ratio item g than is available, and still holds some weight d of a lower-ratio item x. Replace d units of x with d units of g. The weight is unchanged. The value rises by d * (ratio(g) - ratio(x)), which is not negative. So the swap never hurts.
The same swap fails for 0/1 knapsack. You cannot take d units of an item, only all of it or none, so the replacement may not fit. That is why 0/1 knapsack needs dynamic programming.
Common errors
- Swapping without checking feasibility. Showing the new solution is no worse is only half the proof. You must also show it obeys every constraint.
- Proving the wrong direction. The claim is that some optimal solution contains
g. Do not claim that every optimal solution contains it. Ties make that false. - Forgetting the subproblem. After the first choice, the rest of the problem must have the same form. If a choice changes the rules for the remaining items, optimal substructure may fail.
- Testing in place of proving. Passing a few random inputs does not show a greedy rule is correct. In GATE questions, a single counterexample is enough to reject a rule.
Exam strategy
When a GATE question asks whether a greedy rule gives the optimum, do the following.
- Try a tiny instance with two or three items that separates the candidate rule from the alternatives.
- If you find a counterexample, the rule is wrong and you are finished.
- If you cannot, sketch the swap: which element of
OPTdoesgreplace, and why does the result stay feasible?
Exercise
Consider the rule "pick the activity with the earliest start time" for activity selection. Give an instance on which it fails, and say which step of the exchange argument breaks.
Show answer
Take the activities (0, 10), (1, 2), (3, 4). Earliest start picks (0, 10) first. It overlaps both of the others, so the greedy schedule has 1 activity, while the optimum is 2: (1, 2) and (3, 4). The swap step breaks. The first activity in OPT, (1, 2), finishes earlier than the greedy choice (0, 10). Replacing it with (0, 10) overlaps (3, 4), so the new schedule is not feasible.
Exercise
Why does the exchange argument for fractional knapsack not carry over to 0/1 knapsack? Use capacity 50 and items (weight, value) = (10, 60), (20, 100), (30, 120) to show that greedy by ratio fails.
Show answer
In 0/1 knapsack, an item is taken whole or not at all, so you cannot swap an arbitrary amount d of one item for d of another. The ratios are 6, 5 and 4. Greedy by ratio takes (10, 60) and (20, 100), for weight 30 and value 160, and the 30-unit item no longer fits. The optimum takes (20, 100) and (30, 120), for weight 50 and value 220. The exchange fails because replacing the 10-unit item with a fraction of the larger item is not allowed.