Sub topic 6
Proving Correctness: The Exchange Argument
This chapter assumes you know the greedy choice property and optimal substructure from earlier chapters, and that you are comfortable with proof by contradiction and induction.
The greedy-stays-ahead technique compares greedy and optimal step by step. The exchange argument takes a different route. You start from any optimal solution and change it, one small swap at a time, until it matches the greedy solution. If no swap makes the solution worse, then the greedy solution is at least as good as the optimal one, so it is optimal too.
The Recipe
Most exchange proofs follow the same four steps:
- Let G be the greedy solution and O be any optimal solution.
- If O differs from G, find a specific local difference. This is usually an adjacent pair that greedy would order the other way, called an inversion.
- Swap that pair and show the cost does not increase.
- Each swap moves O closer to G, for example by removing one inversion. After a finite number of swaps, O becomes G, and its cost never went up along the way.
A Worked Example: Shortest Job First
You have n jobs on one machine. Job i takes p_i time. The goal is to minimize the sum of completion times. Greedy rule: run jobs in increasing order of length.
from itertools import permutations
def total_completion(order):
t = total = 0
for p in order:
t += p
total += t
return total
def shortest_first(jobs):
return sorted(jobs)
jobs = [3, 1, 2, 5, 2]
greedy = shortest_first(jobs)
best = min(total_completion(list(p)) for p in permutations(jobs))
print(greedy, total_completion(greedy), best) # [1, 2, 2, 3, 5] 31 31The brute-force check agrees with greedy on this input. A proof shows that it agrees on every input.
The Proof
Suppose O is optimal and not sorted. Then somewhere in O a job i runs directly before a job j with pi > pj. If no adjacent pair were out of order, the sequence would be sorted. Let T be the time when job i starts.
| Order | Completion of first | Completion of second | Sum for the pair |
|---|---|---|---|
| i then j | T + p_i | T + p_i + p_j | 2T + 2p_i + p_j |
| j then i | T + p_j | T + p_j + p_i | 2T + 2p_j + p_i |
Every other job finishes at the same time either way, because the pair occupies the same block of time. The swap changes the total by (2pj + pi) − (2pi + pj) = pj − pi < 0. The total strictly decreases, which contradicts O being optimal. So every optimal schedule is sorted by length. When jobs tie, swapping them changes nothing, so the greedy order is one of the optimal schedules.
Common Errors
- Swapping non-adjacent elements. The jobs between them shift in time, and the cost comparison becomes messy or wrong.
- Forgetting termination. You must say why the swaps end. "Each swap removes one inversion, and there are at most n(n−1)/2" is enough.
- Assuming the optimum is unique. With ties, show the swap does not increase cost. Do not claim that it decreases.
- Proving the wrong direction. Show that O can be turned into G. Showing that G can be turned into something else proves nothing.
Exercise
Each job now also has a weight wi, and the goal is to minimize the sum of wi times the completion time of job i. Use an adjacent exchange to find the greedy order.
Show answer
For adjacent jobs i then j starting at time T, the weighted cost of the pair is wi(T+pi) + wj(T+pi+pj). Swapping them gives wj(T+pj) + wi(T+pj+pi). The swap changes the cost by wi pj − wj pi. That change is negative when pj/wj < pi/wi. So jobs should run in increasing order of pi/wi (Smith's rule), and the same inversion-removal argument proves this order optimal.
Exercise
Why does an unsorted sequence always contain an adjacent inversion?
Show answer
If every adjacent pair satisfied ak ≤ a{k+1}, then chaining these inequalities would make the whole sequence sorted. So an unsorted sequence must have at least one adjacent pair out of order.