Intermediate · Module 16 · Lesson 6
Huffman coding
Merge the two least frequent nodes repeatedly.
15–25 min readLesson 94 of 123
Lesson 94 of 123
1d streakTheory lesson · Step-by-step
Understand Huffman coding
Merge the two least frequent nodes repeatedly.
Worked approach
Trace the reasoning step by step
- 1State the candidate ordering and local choice made by Huffman coding.
- 2Identify the feasibility condition maintained by the partial solution.
- 3Use an exchange or stays-ahead argument to justify the choice.
- 4Commit the choice and remove candidates it makes impossible.
- 5Search for a counterexample before trusting the greedy rule.
Concrete example
Build codes for frequencies 5, 9, 12, 13.
- 1Merge 5 and 9 → 14.
- 2Merge 12 and 13 → 25.
- 3Merge 14 and 25.
Frequent symbols remain shallower than rare symbols.
What must remain true (Loop Invariant)
After every choice in Huffman coding, the partial solution can still be extended to an optimum under the proved exchange or stays-ahead argument.
Reference pseudocode
1huffmanCoding(candidates):
2 order candidates by the proved greedy key
3 solution = empty
4 for candidate in order:
5 if candidate preserves feasibility:
6 commit candidate to solution
7 return solution
Verify your trace
- • Write the state after every step.
- • Confirm the invariant still holds.
- • Count visited states to derive complexity.
- • Repeat with an edge-case input.
Cost Model
Complexity & Trade-offs
OperationTimeSpaceWhy
Repeated two-minimum merge
O(n log n)
O(n)
The bound follows from the number of states visited and the work performed per transition.When to use it
- Scheduling, reachability, coding, matching resources, and minimum-cost growth.
- Problems with an exchange proof or matroid-like safe choices.
- Online decisions that maintain a best frontier.
Common mistakes
- Choosing a plausible local optimum without a proof.
- Using the wrong sorting key.
- Applying greedy reasoning when choices interact through future state.
Practice prompts
- Reproduce the worked Huffman coding example without looking at the trace.
- Change one input condition and explain whether the Huffman coding invariant still holds.
- Implement the approach, derive its cost, and compare it with a straightforward baseline.
Mastery check
Can you derive it instead of memorizing it?
State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.