Appearance
Exchange Arguments: Proving Greedy Works
Quick Revisit
- USE WHEN: proving (or disproving) a greedy ordering — "sort by what criterion?"
- INVARIANT: swapping any two adjacent out-of-order elements never improves the solution
- TIME: O(n log n) for sort-based greedy | SPACE: O(n)
- CLASSIC BUG: wrong pairwise comparison — e.g., job scheduling needs wᵢ/tᵢ ratio, not wᵢ − tᵢ
- PRACTICE: 08-ladder-mixed
Greedy is the most dangerous technique in competitive programming. It looks right on small examples, it passes pretests, and then it fails on test 47 because your greedy ordering was subtly wrong. Exchange arguments are how you prove -- or disprove -- that a greedy strategy is correct.
Difficulty: [Advanced]CF Rating Range: 1700-2200 Prerequisites: Greedy
Contents
- The Exchange Argument Framework
- Proof 1: Activity Selection
- Proof 2: Minimizing Total Completion Time
- Proof 3: Huffman Coding
- Proof 4: Job Scheduling with Deadlines and Penalties
- When Exchange Arguments Fail
- The Proof Habit
- Practice Problems
The Exchange Argument Framework
Every exchange argument follows the same skeleton:
- Assume an optimal solution OPT that differs from the greedy solution G.
- Find two adjacent elements in OPT that violate the greedy ordering criterion.
- Swap those two elements and show the resulting solution is no worse.
- Repeat until OPT matches G (by induction / bubble sort argument).
- Conclude G is optimal.
The heart of step 3 is always an inequality. You write down the cost before and after the swap and show
Proof 1: Activity Selection
Problem: Given
Greedy: Sort by finish time
Exchange argument:
Let OPT be an optimal set that differs from greedy set G. Let
Swap
Now OPT' agrees with G on the first choice. Repeat inductively for the remaining intervals. Eventually OPT is transformed into G without losing any intervals.
Proof 2: Minimizing Total Completion Time
Problem:
Greedy (SPT rule): Schedule jobs in order of increasing processing time.
Exchange argument:
Suppose OPT has two adjacent jobs
- Before swap: contribution of
and is . - After swap: contribution is
.
Difference:
So the swap strictly decreases the total. This contradicts OPT being optimal unless no such pair exists -- i.e., OPT is already sorted by
Proof 3: Huffman Coding
Problem: Given character frequencies
Greedy: Repeatedly merge the two lowest-frequency nodes.
Exchange argument (sketch):
In any optimal tree, the two lowest-frequency characters must be siblings at the deepest level. Suppose not: say character
Swap their positions. Cost changes by
After establishing the two lowest-frequency characters are siblings at the bottom, replace them with a single node of combined frequency and recurse. This is exactly what Huffman's algorithm does.
Proof 4: Job Scheduling with Deadlines and Penalties
Problem:
Greedy: Sort jobs by penalty (largest first). Schedule each job at the latest available slot on or before its deadline. If no slot is available, the job is late.
Exchange argument:
Consider OPT where an on-time job
Now consider the case where a late job
When exchange arguments fail
Failed exchange #1: a tempting swap that breaks feasibility
Problem.
Tempting greedy. "Sort by deadline ascending; ties broken by processing time descending." A reasonable instinct: do the jobs with tight deadlines first.
Attempted exchange argument. Take two adjacent jobs
Where it fails. Consider
But add a third job
Try
Now try
The lesson: the swap inequality "moving the tighter deadline earlier never worsens the count of late jobs" is not an identity. Whether it helps depends on the cumulative time before the pair, which is a global quantity. A correct algorithm here is Moore–Hodgson (sort by deadline, then drop the largest job from the prefix whenever capacity is exceeded) — and that algorithm's correctness needs more than a one-step adjacent swap.
Failed exchange #2: 0/1 knapsack
Problem. Items with (weight, value):
Greedy by value/weight ratio picks item 1 (
The exchange fails because swapping isn't local — adding one item may force removing another due to the global capacity constraint. Exchange arguments need swaps that touch only the two elements involved.
Rule of thumb
If swapping two elements can change which other elements are feasible (global capacity, sequencing-dependent feasibility, packing constraints), the adjacent-swap argument breaks. The fix is usually DP on the resource or item subset.
The Proof Habit
In contests, you don't need to write a formal proof. But you should mentally:
- Identify the greedy ordering -- what comparator sorts elements?
- Imagine two adjacent out-of-order elements -- what happens when you swap?
- Check the inequality -- does the swap never worsen the answer?
If you can't convince yourself in 30 seconds, the greedy is probably wrong. Switch to DP. This 30-second mental check saves hours of debugging wrong greedy solutions.
Practice Problems
| Problem | Key Insight | CF Rating |
|---|---|---|
| CF 1203F1 - Complete the Projects (easy) | Sort by gain/loss, exchange argument on ordering | 1800 |
| CF 1491C - Pekora and Trampoline | Greedy simulation with exchange justification | 1700 |
| CF 1665C - Tree Infection | Binary search + greedy check, exchange on priority | 1600 |
| CF 1374E2 - Reading Books (hard) | Sort by contribution, exchange argument | 1900 |
| CF 580B - Kefa and Company | Sort + two pointers, greedy ordering proof | 1500 |
| CF 1443C - The Delivery Dilemma | Binary search the answer + greedy feasibility | 1400 |
Related Techniques
- Greedy -- exchange arguments are the primary proof technique for greedy correctness
- Sorting and Searching -- most exchange arguments reduce to "sort by this comparator"
- DP vs Greedy Guide -- when exchange arguments fail, the problem likely needs DP instead