7 routes42 sections324 units

Unit 4.3 · about 4 h

115 / 324

Greedy choice, optimal substructure and exchange arguments

Greedy is the shortest path from idea to code and the easiest place to be confidently wrong. The proof is the algorithm, so write it before the loop.

  • derive

You can now

  • State the greedy-choice property and optimal substructure of a new problem in one paragraph each, before writing any loop.
  • Prove a greedy rule with an exchange argument that preserves feasibility and never worsens the objective.
  • Prove a greedy rule with a staying-ahead invariant that compares the greedy prefix against every optimal prefix.

Covers

  • Greedy-choice property and optimal substructure
  • Exchange, staying-ahead and cut arguments

Study, in this order

3 steps

  1. Greedy thinking: when local choices win, and when they do not
    DSA Handbook - recursion, backtracking and greedy

    When greedy is provably correct: The exchange argument; Greedy stays ahead; A working example: Partition Labels

    Depth · Use the Where greedy fails section as a preview of the next unit. Counterexample drills live there.

  2. Huffman encoding
    DSA Handbook - recursion, backtracking and greedy

    The algorithm; Watching it run; Why Huffman codes are optimal prefix codes: the sibling exchange and the optimal-substructure step

  3. Greedy Algorithms, Chapter 4
    Algorithms - recursion, backtracking and greedy

    4.1 Storing Files on Tape for the first exchange argument, 4.3 General Pattern for the exchange template, and 4.4 Huffman Codes (printed pp. 159-170)

    Depth · Erickson proves everything by exchange; take stays-ahead from the handbook. Skip 4.5 Stable Matching.

Focus on

  • Exchange has a fixed shape: take an optimal solution, swap its first differing choice for the greedy one, show validity kept and the objective not worse, then induct on the rest.
  • Staying-ahead has a different shape: the greedy frontier dominates every optimal prefix at each step, as with Jump Game max reach. Pick the template that matches the state you can compare.
  • Optimal substructure must hold after the greedy choice. In Huffman coding, merging the two least frequent symbols leaves a smaller instance whose optimum sits inside the full optimum through the weight recurrence.
  • Partition Labels is the stays-ahead drill: last occurrence table, extend the part end, cut at \(i = \text{end}\), in \(O(n)\) time and \(O(1)\) extra space.

Reproduce closed-book

From a blank file in Python 3.13.14, implement Partition Labels from its invariant and run it on ababcbacadefegdehijhklij to get [9, 7, 8]. Closed book, write the Huffman exchange step: swapping a least frequent symbol into a deepest sibling leaf changes total cost by at most zero.

Traps

  • Proving by example. Passing samples shows the code runs; only the exchange or staying-ahead step shows no optimal solution beats it.
  • An exchange that breaks feasibility. Swapping must keep the prefix-free sibling structure in Huffman coding and disjointness in scheduling, or the transformed optimum is not a valid candidate.
  • Staying ahead on a measure that does not imply the objective. Largest-coin-first on coins [1, 3, 4] with target 6 leads on amount covered after one coin (4 against 3), yet needs 3 coins (\(4 + 1 + 1\)) against the optimal 2 (\(3 + 3\)).

Practice

LeetCode 134: Gas Station

Attempt cold. Example gas [1, 2, 3, 4, 5] with cost [3, 4, 5, 1, 2] returns 3; gas [2, 3, 4] with cost [3, 4, 3] returns minus 1.

After submitting, read the DSA Handbook Jump games and gas station chapter (Claims 1 and 2) and the AlgoMaster Gas Station lesson (https://algomaster.io/learn/dsa/gas-station), and restate your loop invariant in their terms.

Self-test

  1. When do you reach for exchange, and when for staying-ahead?

  2. State the Huffman exchange computation.

  3. Why does \(\text{total gas} - \text{total cost}\) at least zero guarantee a valid start?

  4. What optimal-substructure claim does Huffman coding need after the greedy merge?

Completion criterion

Stop when

Gas Station is accepted from a cold attempt, and you can write the Huffman exchange step and the total-surplus argument without notes.

Mark passed, then open 4.4 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

Also in this section

  • AlgoMaster DSA - decomposition and greedy slice
    AlgoMasterrequiredpractice
    Attempt N-Queens and Gas Station cold, then implement reservoir sampling and test its output distribution over fixed-seed trials. Add another judged problem only for a diagnosed weakness.
  • MIT 6.042J - probability and randomized algorithms
    MIT OpenCourseWaresupporttheory
    Use the probability, expectation and concentration readings to derive sampling probabilities and distinguish expected from high-probability guarantees.
  • Algorithms lecture notes - randomized algorithms
    Jeff Ericksonsupporttheory
    Use note 1 Discrete Probability, note 2 Nuts and Bolts (randomized quicksort, with QuickSelect in its exercises), note 4 Tail Inequalities and note 5 Hashing to derive expected costs, separate expected from high-probability guarantees, and analyze hashing behavior.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels