The standard way to prove a greedy algorithm optimal: assume an optimal solution, show it can be transformed step by step into the greedy one without getting worse, and conclude greedy is optimal too.

The two forms

Adjacent swap (for orderings). Take an optimal ordering. If two adjacent elements are out of greedy order, swap them and show the objective does not worsen. Since any permutation is reachable by adjacent swaps, greedy is optimal.

Exchange (for selections). Take an optimal solution differing from greedy at the first position. Replace the optimal’s choice with greedy’s choice and show the result is still feasible and no worse. Induct.

The recipe for ordering problems

To find the right sort key, consider just two jobs and and ask when should precede :

Rearrange into the form — that inequality is the comparator.

sort(jobs.begin(), jobs.end(), [](const Job& a, const Job& b) {
    return costIfAFirst(a, b) < costIfBFirst(a, b);      // derived pairwise
});

Check transitivity

A comparator derived this way must be a strict weak ordering. If the pairwise condition is not transitive, sorting is undefined behaviour and the greedy is wrong. Most standard keys (ratios, differences) are transitive; verify unusual ones.

The classic sort keys

ProblemObjectiveSort by
Interval schedulingmax non-overlappingearliest finish time
Minimum max latenessearliest deadline (EDD)
Minimum sum of completion timesshortest processing time (SPT)
Minimum weighted sumSmith’s rule: ascending
Huffmanmin weighted path lengthmerge the two smallest
Fractional knapsackmax valuevalue/weight ratio
Two-machine flow shopmin makespanJohnson’s rule
Minimise of style costspairwise comparison
Minimum number of platformssort events by time
Maximise pairingssort both, pair greedily

Deriving Smith’s rule

Two jobs, before : cost . Swapped: . Then

Three lines, and it gives the comparator directly. This is the whole technique.

Proving the selection form

For interval scheduling (earliest finish first):

  1. Let be greedy’s choices and an optimal solution, both sorted by finish time.
  2. Claim: for all . By induction: greedy picks the earliest-finishing compatible interval, and is compatible with by the hypothesis.
  3. If , then starts after , so greedy could have taken it — contradicting that greedy stopped.
  4. Hence . ∎

This “greedy stays ahead” structure is the template for most selection proofs.

When greedy fails

An exchange argument that does not close is evidence the greedy is wrong. Common failures:

ProblemGreedy fails because
0/1 knapsackitems are indivisible; ratio order is not optimal
Coin change, arbitrary denominationslargest-first can be beaten (, target 6)
Weighted interval schedulinga long high-value interval may beat several short ones
Set covergreedy is only -approximate
TSPnearest neighbour is -approximate
Graph colouringgreedy depends on the vertex order

Always test greedy against a brute force on small random inputs. A failing case is found in seconds and saves a wrong-answer verdict.

Matroids — when greedy is guaranteed

If the feasible sets form a matroid (closed downward, plus the exchange property), then greedy by weight is optimal for maximum-weight independent sets. This covers:

  • spanning forests → Kruskal,
  • linearly independent vectors → linear basis,
  • partition and uniform matroids,
  • scheduling unit jobs with deadlines (a transversal matroid).

Recognising a matroid is a proof that greedy works, with no exchange argument needed.

See also: Algorithmic Paradigms · Scheduling · Proof Techniques