3.7.3 · D4Algorithm Paradigms

Exercises — Greedy — exchange argument proof technique

2,903 words13 min readBack to topic

Before any exercise, we earn every symbol used below:


Level 1 — Recognition

L1.1 — Which proof technique?

You must show: "greedy is never behind the optimal solution after each of its first picks." Is this an exchange/swap argument or a greedy-stays-ahead argument?

Recall Solution

Greedy-stays-ahead. It tracks a measure (how far along you are after picks) and shows greedy's measure dominates optimal's at every . An exchange argument instead physically edits the optimal solution one swap at a time. See Greedy-stays-ahead proof technique for the contrast.

L1.2 — Spot the inversion

A schedule runs jobs in the order (length in brackets): . Under the SPT rule (shortest first), list every adjacent inversion.

Recall Solution

Adjacent pairs and whether the earlier one is longer (an inversion under the SPT rule):

  • : inversion
  • : → fine
  • : inversion

Two adjacent inversions: and .


Level 2 — Application

L2.1 — One SPT swap, by the numbers

Two jobs start as a block at time : job has , job has , and runs before . Compute in , then in the swapped schedule (with first), and give the improvement.

Recall Solution

In ( then ): ; . Sum . In ( then ): ; . Sum . Improvement . Matches the formula .

The picture (Figure s01) makes the "paid twice" idea visible. Read it as follows: each row is a schedule, each block's width is its processing time , and the red arrow points at the finish time that gets counted inside both -values (the first job's length is added again to the second job's completion). In the top row () that doubled length is the long red bar's ; in the bottom row () it is the short green bar's . So the figure shows geometrically why the total drops by exactly : you shrink the length that is charged twice. Without the figure the " vs " doubling is just algebra; with it you see which bar is doubled.

Figure — Greedy — exchange argument proof technique

L2.2 — One Smith's-rule swap

Weighted completion time . Adjacent jobs then : , . Should you swap? Compute the cost change and interpret.

Recall Solution

Where the formula comes from. Let the pair start at time (the start of whichever job runs first). In order -then-: and , so the pair contributes . In order -then-: the contribution is . Subtracting, every term containing cancels (both jobs still start no earlier than ), leaving That is why the messy expression collapses to this clean cross-difference: it isolates exactly the mutual-waiting penalty.

Plug in. . Positive means the current order ( then ) costs more — swapping helps by . Equivalently, and ; since , job has the worse ratio and should go later. This is Smith's rule — weighted completion time.

Tie case: if then — either order is equally good, so break ties arbitrarily.


Level 3 — Analysis

L3.1 — Why strict vs equal?

In the SPT proof the swap gives a strict improvement, but in the activity-selection proof it gives equal cost. Explain what feature of each problem forces strict vs equal, and why both still prove optimality.

Recall Solution
  • SPT minimises a sum of real quantities (). A genuine inversion () makes the swap strictly cheaper by . Strictness lets us argue by contradiction: an optimal schedule cannot contain an inversion, else we'd beat it. (At a tie the change is — not an inversion, nothing to fix.)
  • Activity selection counts activities. Replacing by the earlier-finishing keeps the count identical (one-for-one), so the swap is weakly no-worse. It preserves optimality rather than improving it, then we induct on the remainder — see Activity Selection Problem.

Both work because the exchange contract only requires "no worse." Strict improvement is a bonus (and enables the contradiction phrasing); equality is enough to carry optimality along the swap chain .

L3.2 — Huffman: both terms needed

Optimal tree cost is (frequency times depth). We swap and . Given and , compute each swap's and the total. Confirm .

Recall Solution

Where each comes from. Cost is (here is a symbol's frequency and its depth in the tree). Swapping symbols and means now sits at depth and at depth ; every other symbol keeps its depth, so all those terms cancel in the difference. Only the two moved symbols survive: That factored form is why the swap is easy to sign: it is a product of a frequency gap and a depth gap. The same algebra with gives .

Plug in. First swap: . Second swap: . Total . Cost strictly decreases here. Each factor pattern: times gives a non-positive product. Tie case: if a frequency gap is (equal frequencies) or a depth gap is , that term is exactly — still no increase. Summing both is mandatory — see Huffman Coding.


Level 4 — Synthesis

L4.1 — Full SPT exchange proof

Write the complete exchange argument that SPT (shortest processing time first) minimises . Use the 5-step skeleton.

Recall Solution

1. Setup. Let be any optimal schedule, the SPT schedule. 2. First difference. If , done. Otherwise differs from sorted order, so it contains at least one adjacent inversion: jobs then with . (If no adjacent pair were inverted, the whole order would be sorted = .) 3–4. Swap. Exchange the adjacent pair to get . Every job before and after keeps its exact start time, so only change — feasibility holds (still one job at a time). 5. No worse. Let be the start time of job in (equivalently, the start time of the adjacent block, since runs immediately after ). In : , , so their sum is . In : , , sum . Hence So is strictly cheaper — impossible if was optimal and had an inversion. Therefore optimal has no inversion, i.e. it is sorted by SPT . Hence is optimal. Tie case: if the difference is — the two orders cost the same, so equal-length jobs may sit in either order and SPT (breaking ties arbitrarily) is still optimal. (The adjacency-only argument suffices because any inversion implies an adjacent one — see L1.2.)

L4.2 — Full activity-selection first-step lemma

Prove: some optimal solution includes , the globally earliest-finishing activity.

Recall Solution

Notation for this proof. Each activity is an interval , where is its start time (when it begins) and is its finish time (when it ends). (This is a finish-time — unrelated to the frequency used in the Huffman exercise L3.2.) Setup. Let be optimal, its activities sorted by finish time. Let be the activity with the smallest finish time of all activities, so . Swap. Form by replacing with , keeping . Feasibility. In , activity starts at (they don't overlap). Since , activity still starts after finishes. vs : compatible. All later activities were already after , hence after . So is a valid (pairwise non-overlapping) selection. No worse. (swapped one for one), so is also optimal and it contains . Tie case: if two activities share the globally earliest finish time, either may serve as ; the same argument holds, so the choice is free. Induct on the subproblem of activities starting after : the same lemma applies, so greedy's every pick is extendable to an optimum.


Level 5 — Mastery

L5.1 — Invent the greedy key (deadline penalties)

unit-length jobs, all available at time , each run one per slot . Job has weight and, if it finishes in slot , pays a penalty . Using an adjacent-swap analysis, derive the correct sorting key and prove it.

Recall Solution

Notation for this proof. A "slot" is a unit time interval; a job placed in slot number finishes at . Consider two adjacent jobs where runs in slot and in slot .

Derive the swap change. Swapping puts in slot and in slot . Only these two jobs move slots, so all other penalties cancel in the difference: Expand: the -terms give (the slot index cancels entirely, since both jobs still occupy the same two slots), and the terms give . So

Read off the key. Swapping helps (lowers cost) when , i.e. when : a lighter job sitting before a heavier one should be swapped so the heavier one moves earlier. Therefore the greedy rule is sort by weight, descending — largest weight first.

Proof (exchange argument). Take any optimal schedule . If ever runs a lighter job immediately before a heavier one ( with before ), the adjacent swap above lowers cost by , contradicting optimality. Hence optimal contains no such inversion, so it is sorted by descending weight . Therefore is optimal.

Tie case: if then — equal-weight jobs may occupy either slot; break ties arbitrarily. (This is Smith's rule with all : the ratio ascending weight descending.)

L5.2 — When does exchange fail? (bridge to DP)

For coin change with denominations making , greedy-by-largest gives how many coins, and the true optimum how many? Explain, in one sentence, why no adjacent-swap argument can rescue greedy here — connecting to why we escalate to Dynamic Programming.

Recall Solution

Greedy-by-largest: coins. Optimum: coins. Greedy is not optimal. No exchange argument works because the problem lacks the exchange property: swapping a single greedy coin for another does not monotonically improve — the optimum shares no coin with greedy's first choice (), so there is no local swap chain that stays feasible and no-worse. When the greedy/exchange (or matroid) structure is absent, we need the overlapping-subproblem machinery of DP.


Recall Self-test checklist

Every exchange proof I write states the objective ::: yes — the number being optimised is named before any swap I swap at the first disagreement and induct on the matching prefix ::: yes I check feasibility of , not only its cost ::: yes If the construction does two swaps I sum both terms ::: yes I know strict-improvement (contradiction) vs equal-cost (preserve) are both valid ::: yes At a tie in the greedy key the swap change is exactly , so ties break arbitrarily ::: yes