Information Theory / Coding
Compression and Huffman Codes
Short codewords for common symbols, the prefix property, and Shannon’s limit on compression.
Contents 3 sections
By the end you can
- Recognise prefix-free codes
- Build a Huffman code
- Compare average code length with entropy
Helps to know: Entropy
§1Prefix codes
Morse code gives E a single dot because E is common. A code is prefix-free when no codeword begins another, so a bit stream can be decoded without separators.
§2Huffman’s algorithm
- List the symbols with their probabilities.
- Merge the two least likely into a new node whose probability is their sum.
- Repeat until one node remains.
- Read codes from the tree: left is 0, right is 1.
Example 1Four symbols
Probabilities , , , .
- Merge C and D into , then with B into , then with A.
Check yourself
- Q1Is a prefix-free code?