Part II · Chapter 4

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

Interactive 4.1Morse Without Gaps
Type a word (letters only)
the dots and dashes actually sent other words hiding in the same signal
 
Without gaps, "sos" (··· ——— ···) can also be read as many other letter strings. The number of readings grows explosively with length: a longer word typically hides thousands or millions of alternatives in the same run of dots and dashes.

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).

The prefix-free rule

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.

Interactive 4.2The Code Builder
Edit the codewords (0s and 1s), or pick a code
codewords (leaves of the tree) and the bits sent a codeword that is the prefix of another, or a clash how often each symbol occurs entropy of the source
 
The sample message is ABACABAD. With the prefix-free code it costs 14 bits and decodes in one pass; with the fixed-length code it costs 16. "Not prefix-free" is shorter still, and useless: the same bits can be read in several ways. "Wasteful" decodes fine, but leaves a branch of the tree unused, so it spends bits it did not need to.

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.

Σ 2−length ≤ 1 "Each codeword of length L uses up 1/2L of the budget, and the total cannot exceed the whole." This is known as the Kraft inequality.

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.

Interactive 4.3The Codeword Budget
Slide each codeword's length
budget claimed by each codeword (1/2L) overspent: no prefix-free code possible symbol frequency entropy, the lower limit for the average
 
When the slices fit, the canonical codewords written in them form a working prefix-free code. Shrinking any codeword makes its slice grow, and something else must give. Rounding each surprise up always fits, though with these frequencies it leaves budget unspent; the best possible lengths (found by Huffman's method, next chapter) spend the whole budget and get the lowest average for these frequencies.

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.

Why entropy is a floor

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.

Intuition Check

Which of these codes is prefix-free?

Intuition Check

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

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.