Skip to content
Infinitesimal Dynamics home
Notebook: 0 XP

Information Theory / Coding

Compression and Huffman Codes

Short codewords for common symbols, the prefix property, and Shannon’s limit on compression.

3 min read · 2 sections · 1 checks

Contents 3 sections
  1. §1Prefix codes
  2. §2Huffman’s algorithm
  3. ?Check yourself

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

  1. List the symbols with their probabilities.
  2. Merge the two least likely into a new node whose probability is their sum.
  3. Repeat until one node remains.
  4. Read codes from the tree: left is 0, right is 1.

Example 1Four symbols

Probabilities A:0.5A: 0.5, B:0.25B: 0.25, C:0.125C: 0.125, D:0.125D: 0.125.

  1. Merge C and D into 0.250.25, then with B into 0.50.5, then with A.

Check yourself

  1. Q1
    Is {0,01,11}\{0, 01, 11\} a prefix-free code?