Kayavak / Information Theory / IT·05 Codes: Prefix Codes, Kraft's Inequality and Huffman Coding / Deeper
Deeper · Information Theory
Codes: Prefix Codes, Kraft's Inequality and Huffman Coding
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Short codes for common things are old wisdom: Morse code, shorthand, and the telegraph code books that turned a common sentence into one word. What was missing was a way to find the best code. Shannon (1948) proved the entropy bound and gave a code within a bit of it; Fano (1949) gave a top-down splitting method; Leon Kraft's 1949 master's thesis at MIT stated exactly which codeword lengths are possible. Then David Huffman, a student in Fano's course, found in 1951 that building the tree from the bottom up gives the optimum, a result his teachers had looked for and missed.
For two decades Huffman coding was the compression method. Then its limits showed. Codes for single symbols can't spend less than a bit, and real data's probabilities change and depend on context. Arithmetic coding (Rissanen and Pasco, 1976; practical versions in the 1980s) removed the whole-number restriction; Lempel–Ziv methods (1977, 1978) learned repeated strings instead of symbol probabilities; and asymmetric numeral systems (Jarek Duda, around 2009) gave arithmetic coding's efficiency at Huffman's speed. Huffman codes survive inside DEFLATE, JPEG and HTTP headers because they're simple, fast and good enough after a strong model has done the real work.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A prefix code is a binary tree with symbols at the leaves; a symbol's codeword is its path. Kraft's inequality says how many leaves fit at each depth; the entropy is the least average depth; Huffman's bottom-up merging finds the best tree for given probabilities.
| Question | Answer |
|---|---|
| What is it? | Codes with codewords of different lengths, readable without separators |
| Problem it solves | Spending fewer binary digits on likely symbols, so messages get shorter on average |
| Parts | Codewords, the prefix property, a code tree, codeword lengths, the Kraft sum |
| How it works | Likely symbols near the root; decoding walks down the tree one digit at a time |
| Assumptions | Known probabilities; symbols coded one at a time; each gets a whole number of digits |
| When they change | Blocks of symbols, adaptive codes, arithmetic coding, context models |
| Where it fails | Very predictable sources (at least 1 bit per symbol); changing statistics; channel errors that derail decoding |
| Related ideas | Question trees (ch02), entropy (ch04), arithmetic coding and the source coding theorem (ch06) |
What if?
What if codewords could use three symbols instead of two?
Reason it through, then open
Huffman's algorithm generalises: merge the three least likely trees at each step (after adding zero-probability dummy symbols so the count works out), and Kraft's inequality becomes . The average length is then at least the entropy measured in trits, . Morse code is close to this: dots, dashes and gaps make it effectively ternary, with timing as a fourth signal. Binary won for electronics, but the theory doesn't care about the alphabet's size.
What if symbols had costs instead of lengths?
Reason it through, then open
In Morse code a dash takes three times as long to send as a dot, so codewords should be judged by their time, not their number of signals. Finding the best code when the code symbols have unequal costs is a harder problem (studied by Richard Karp in 1961), with no simple bottom-up rule. The entropy bound changes too: the best rate depends on the root of an equation involving the costs. Huffman's algorithm is special to equal-cost symbols.
What if the decoder didn't know the probabilities?
Reason it through, then open
Then the code must either be sent (as DEFLATE sends its lengths) or learned as the data arrives. Adaptive Huffman coding keeps counts and updates the tree after each symbol, so encoder and decoder, seeing the same symbols, always agree. The price is the early symbols, coded with poor estimates, and the work of updating the tree. Universal codes like Elias gamma go further and assume almost nothing, at a cost of about a factor of two. Chapter 6's universal compressors make the cost of not knowing the probabilities vanish per symbol for long inputs.
Where the boundary is
| Aspect | Prefix (instantaneous) code | Uniquely decodable code that isn't prefix |
|---|---|---|
| Decoding | Each codeword recognised as soon as it ends | May need to look ahead, sometimes far |
| Kraft's inequality | Holds | Also holds (McMillan) |
| Best average length | Same | Same |
| Example | 0, 10, 110, 111 | 1, 10, 100 (readable from the right) |
| Used in practice | Almost always | Rarely: no gain, more complexity |
| Why the distinction matters | Proves you lose nothing by choosing prefix codes | Shows unique decodability is the real requirement |
Derive it
Why Huffman's average length is the sum of the merged weights.
- Each time Huffman merges two trees of weights and , every symbol in them moves one level deeper, so their codewords each gain one digit.
- The symbols in the merged trees have total probability , so the average codeword length grows by exactly at that merge.
- Starting from depth 0 for every symbol, the final average length is therefore the sum of the weights of all the merged nodes, the internal nodes of the tree, excluding none: .
- Check on : merges create weights , and 1, which add to , the average length found in the chapter.
- This gives a quick way to compute a Huffman code's cost without writing a single codeword, and it's why a priority queue of weights is all the algorithm needs.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Prefix codes exist exactly when | Theorem | Kraft (1949); the chapter's proof |
| The same holds for every uniquely decodable code | Theorem | McMillan (1956) |
| No uniquely decodable code averages below | Theorem | The chapter's proof |
| Huffman codes are optimal among symbol codes | Theorem | Huffman (1952); the exchange and merge argument |
| Huffman is within 0.04 bits of the entropy for Shakespeare's bytes | Measured | The chapter's lab |
| DEFLATE limits codewords to 15 bits | Convention | RFC 1951 |
| Huffman found his method while choosing between a term paper and an exam | Historical | Huffman's own account, Scientific American profile (1991) |
Still open
Huffman coding itself is settled. Active questions sit around it: the fastest ways to decode Huffman and related codes on modern processors (table-driven, vectorised and multi-stream decoders), optimal length-limited codes for very large alphabets, and when to prefer Huffman, arithmetic coding or asymmetric numeral systems in a given format, a trade between speed, compression and patent history (arithmetic coding was long avoided for patent reasons, now expired).
What would change your mind? The chapter says no code for single symbols beats Huffman. A code that did would have to break the exchange argument or the merge step; none can. But "single symbols" is the important restriction: codes over blocks, runs or whole messages beat Huffman's per-symbol average routinely, which is not a contradiction.
Recall
What is a prefix code?
A code in which no codeword is the beginning of another, so each codeword is recognised as soon as it ends.
State Kraft's inequality.
Lengths l₁ … l_K are possible for a prefix code iff Σ 2^(−lᵢ) ≤ 1.
How close to the entropy can a code for single symbols get?
Its average length is at least H and can be made less than H + 1.
What does Huffman's algorithm do?
Repeatedly merges the two least likely subtrees until one tree remains; the result is an optimal prefix code.
Why do canonical Huffman codes matter?
The codewords follow from the lengths alone, so only the lengths need to be sent.
What's Huffman's main weakness?
Every symbol costs at least one whole binary digit, so very predictable sources are coded far above their entropy.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.