Sub topic 3
Why the Greedy Choice Is Optimal
This chapter assumes you have already built a Huffman tree with a min-heap and compared its average length with entropy. You should also be comfortable with Python and bit shifts.
Why the Greedy Choice Is Optimal
In any optimal prefix code, the two least frequent symbols can be placed as sibling leaves at the deepest level. Swapping a rarer symbol deeper never increases the average length. Merging those two symbols into a single node gives a smaller version of the same problem, and an optimal tree for that smaller problem extends to an optimal tree for the original. Applying this argument by induction proves that Huffman's repeated merging gives the minimum average length among codes that assign a whole codeword to each symbol.
Canonical Huffman Codes
The code lengths are all that matter. A canonical code reassigns the bit patterns using a fixed rule: sort the symbols by length and then by symbol, count upward, and shift left whenever the length grows.
def canonical_codes(lengths):
ordered = sorted(lengths.items(), key=lambda item: (item[1], item[0]))
codes, code, prev_len = {}, 0, ordered[0][1]
for sym, length in ordered:
code <<= (length - prev_len)
codes[sym] = format(code, "0{}b".format(length))
code += 1
prev_len = length
return codes
print(canonical_codes({"a": 1, "b": 2, "c": 3, "d": 3}))
# {'a': '0', 'b': '10', 'c': '110', 'd': '111'}Shipping the Code Table with the Compressed Data
The decoder needs the code before it can read the data. Canonical coding means the file only has to store one length per symbol, not the tree or the bit patterns. The decoder runs the same procedure on those lengths and gets identical codes.
Limitations
| Issue | Effect |
|---|---|
| Whole-bit lengths | A symbol with p = 0.99 still costs 1 bit, while the entropy of that source is about 0.08 bits per symbol |
| Single symbol | Needs a special case, such as assigning it the code "0" |
| Table overhead | On short inputs, the stored code table can cancel out the savings |
| Static model | The code cannot follow statistics that change partway through the data |
When probabilities are very skewed, arithmetic coding or ANS do better because they can spend fractional bits per symbol.
Adaptive Huffman Coding
Adaptive schemes (FGK, Vitter) update the tree after every symbol, and the decoder repeats the same updates. Nothing has to be transmitted up front, but each symbol costs more CPU time.
Length-Limited Huffman Codes
Real decoders cap code lengths so they can use lookup tables. DEFLATE caps them at 15 bits and JPEG at 16. The package-merge algorithm finds the optimal code under such a cap.
Huffman Coding in DEFLATE and JPEG
DEFLATE, used in gzip, zip and PNG, Huffman-codes the literals and the LZ77 lengths and distances. It stores canonical code lengths, and those lengths are themselves Huffman-coded. Baseline JPEG Huffman-codes its quantized DCT coefficients, and each file's DHT segment holds the code-length counts.
Exercise
Give the canonical codes for lengths {a: 2, b: 2, c: 2, d: 3, e: 3}.
Show answer
a=00, b=01, c=10, d=110, e=111. After c the counter is 3 (binary 11). Shifting it left for length 3 gives 110.