Codes That Need No Commas
To squeeze a message, give common symbols short codewords and rare ones long codewords. But a stream of 0s and 1s has no commas. How does the receiver know where one codeword ends and the next begins? One simple rule fixes everything.
The telegraph operators of the nineteenth century had the right instinct. In Morse code the most common English letter, E, is a single dot, and T is a single dash. Rare letters like Q (——·—) and Z (——··) get four symbols. A typical message comes out shorter than it would if every letter got the same length. That is compression in its oldest form: spend little on the predictable, more on the surprising.
But Morse code has a secret ingredient that is easy to overlook: silence. A short pause separates the dots and dashes inside a letter, a longer pause separates letters, and a longer one still separates words. Those pauses are really extra symbols. Take them away and watch what happens.
Morse without the pauses
The trouble is that some Morse codewords are the beginnings of others. E (·) is the start of I (··), which is the start of S (···), which is the start of H (····). When the receiver has heard one dot, it cannot know whether the letter is over. Morse solves this with pauses; a computer writing 0s and 1s onto a disk has no pause to spare.
The prefix-free rule
One way out is to make every codeword the same length. The 5-bit telegraph codes and 8-bit text files both work like this: read 8 digits, that is one character, read the next 8. It is simple, but it throws away the whole point. A common letter costs exactly as much as a rare one.
The better way is a single rule: no codeword may be the beginning (the prefix) of another codeword. With the code 0, 10, 110, 111, read a stream from the left. The moment the digits you have read so far spell a whole codeword, that codeword is finished: nothing else could start that way. No commas, no lookahead, no ambiguity. Such codes are called prefix-free (some books say "prefix codes", meaning the same thing).
If no codeword is the start of another, a stream of bits can be cut into codewords as it arrives, with no separators at all.
There is a lovely picture for this. Draw a binary tree: from the root, step left for 0 and right for 1. Every codeword is a place in the tree. A code is prefix-free exactly when no codeword sits on the path to another one; in other words, every codeword is a leaf. Build your own code below and watch the tree, the decoder and the average length respond.
With the frequencies ½, ¼, ⅛, ⅛, the prefix-free code 0, 10, 110, 111 averages exactly 1.75 bits per symbol, and so does the entropy of that source: each codeword's length equals its symbol's surprise (1, 2, 3 and 3 bits). That is no coincidence, and it is the bridge from Part I to Part II. The ideal length for a codeword is the surprise of its symbol.
The codeword budget
Why not make every codeword short? Because short codewords are expensive. Look at the tree again. A 1-bit codeword sits one step below the root and claims everything beneath it: half the tree. A 2-bit codeword claims a quarter, a 3-bit codeword an eighth. Prefix-free codewords cannot overlap, so their claims must fit inside the whole tree.
Try spending the budget yourself. Five symbols occur with the frequencies shown; choose a length for each codeword and see whether a prefix-free code with those lengths can exist.
The budget explains the whole game. Giving each symbol a codeword as long as its surprise, log₂(1/p), spends the budget exactly (because the probabilities add up to 1), and makes the average length equal to the entropy. When surprises are not whole numbers, lengths must be rounded, and some efficiency is lost. Finding the best rounding is Huffman's problem, and the next chapter solves it.
Because codeword lengths must respect the budget, no prefix-free code can average fewer bits per symbol than the entropy of the source. Codes that need lookahead do not escape this either: any code that can be decoded at all has a prefix-free rival with exactly the same codeword lengths.
Which of these codes is prefix-free?
You give the most common symbol the 1-bit codeword 0. How much of the codeword budget is left for all the other symbols?
Connections
- Computer Science, Chapter 1: Talking in the Dark starts from signalling with on and off, including Morse code.
- Computer Science, Chapter 3: Counting by Doubling explains why fixed-length codes need log₂ N binary digits for N symbols.
- Chapter 2 computed the entropy that this chapter's codes are trying to reach.
What you now see
- Variable-length codes save space by giving common symbols short codewords, but a raw bit stream has no separators.
- Morse code relies on pauses; without them one signal hides many readings.
- A prefix-free code (no codeword starts another) decodes instantly, left to right, with no separators: its codewords are the leaves of a binary tree.
- Codeword lengths share a budget: Σ 2−length ≤ 1. Short codewords are expensive.
- The ideal length of a codeword is its symbol's surprise, log₂(1/p); then the average length equals the entropy, and no code can average less.