3.7.3 · D3Algorithm Paradigms

Worked examples — Greedy — exchange argument proof technique

4,061 words18 min readBack to topic

You have seen the recipe in the parent note (3.7.3). This page is the drill hall: we march through every kind of situation an exchange argument can face, so that when a new problem lands on your desk you already recognise its shape.

Before any symbol is used, here is the notation the whole page uses, in plain words:

Recall Two facts we reuse everywhere (recapped so this page stands alone)
  • Completion time. If jobs run one at a time, a job's finish time = its start plus the total length of everything run up to and including it. The unweighted total completion time is .
  • The adjacent-swap identity for . Suppose two adjacent jobs (first) then start at time . Then and , so their combined contribution is . Swap them and it becomes . The whole schedule cost changes only in these two terms, so with the difference of the swap is . Longer-first before shorter costs more by exactly .

The scenario matrix

Every cell below is a distinct behaviour an exchange swap can show. The Cell column is a letter ID () we refer to as "cell A", "cell B", etc.; each worked example is tagged with the cell it covers.

Cell Scenario class What the swap does to cost Example
A Strict improvement (minimisation) : cost strictly drops → contradiction Ex 1
B Equal cost (counting / maximisation) : cost unchanged → still optimal Ex 2
C Ratio / weighted objective sign of set by a ratio rule Ex 3
D Two-swap construction must sum both terms Ex 4
E Degenerate input (ties, equal keys) , greedy still fine Ex 5
F Zero / empty / single element base case, no swap possible Ex 6
G Where greedy FAILS (no exchange lemma) swap can increase cost () Ex 7
H Real-world word problem translate to A or C Ex 8
I Exam twist (non-adjacent swap, prove chain) swap far-apart items Ex 9

Prerequisites you may want open: Proof by contradiction, Induction, Greedy Algorithms — general paradigm.


Example 1 — Strict improvement (cell A)


Example 2 — Equal cost (cell B)


Example 3 — Ratio / weighted objective (cell C)


Example 4 — Two-swap construction (cell D)


Example 5 — Degenerate input: ties (cell E)


Example 6 — Zero / single element (cell F)


Example 7 — Where greedy FAILS (cell G)


Example 8 — Real-world word problem (cell H)


Example 9 — Exam twist: non-adjacent swap (cell I)


Recall

Recall Match the cell to the behaviour

Which cell has (equal cost) because it counts objects? ::: Cell B (activity selection, maximisation). In cell C, what quantity decides who goes first? ::: The ratio (Smith's rule), not raw length. Why does cell G (coins , amount 6) admit no exchange proof? ::: A swap toward greedy's coin can increase the count (), so "no worse after swap" is false (). How does a non-adjacent swap (cell I) get justified? ::: As a chain of adjacent swaps, each with its own "no worse" inequality; gains sum. What does mean, versus ? ::: = greedy's -th choice in order; = the element in position of the optimal list .