Part II · Chapter 7

Look-Back Compression: What Zip Does

Huffman codes letters one at a time and never notices that a whole phrase has appeared before. A different idea fixes that: whenever text repeats, send a pointer back to the earlier copy instead. Pair that idea with Huffman and you have the heart of zip.

Old account books and school registers have a lovely shortcut: the ditto mark. Instead of writing "Mrs. Patel, 14 Harbour Road" five times down a column, the clerk writes it once and then puts 〃 underneath: "same as above". The ditto mark is tiny, and it can stand for any amount of text, as long as that text has already appeared.

In 1977 Abraham Lempel and Jacob Ziv published a compression method built on exactly this instinct, followed in 1978 by a second method that builds an explicit dictionary of phrases instead of pointing back into a window (its descendant LZW is inside GIF images). Their methods, known as LZ77 and LZ78, and the many descendants of them, became the workhorses of everyday lossless compression. Here is the 1977 idea in one sentence: walk through the text, and whenever the next few characters already appeared somewhere behind you, write "go back d characters and copy L of them" instead of the characters themselves.

The look-back machine

Step through a text below. At each step the machine looks back for the longest earlier match of what comes next. If it finds one of at least 3 characters, it emits a copy token (back d, length L), drawn as an arc from the earlier copy to the new spot. Otherwise it emits the next character as a plain literal.

Interactive 7.1The Look-Back Machine
Step or play; edit the text
text covered by a copy token, and the arc to its earlier source literal characters the token being emitted now
 
In the tongue twister, phrases like "sea shells" and "she sells" are copied rather than spelled out again. Try abab…: after two literals a single copy covers everything, because a copy may overlap the text it is producing ("go back 2, copy 58"). Random letters almost never repeat three characters in a row, so nearly everything stays a literal.

Notice what the machine never needed: a table of letter frequencies. It does not know English. It simply remembers what it has already sent, which the receiver also has, so the receiver can follow every pointer and rebuild the text exactly. Ziv and Lempel, and later researchers, proved that, for long enough inputs from a broad class of sources, schemes like these approach the best possible compression rate without being told the source's statistics in advance. That property is called being universal.

Look-back compression

Anything that has been said before can be sent as a short pointer to where it was said: repeats cost almost nothing.

Catching what Huffman misses

Remember the two blind spots from Chapter 5? "abababab…" cost Huffman 1 bit per character because Huffman looks at letters one at a time. Look-back compression sees the repeat and turns the whole thing into three tokens. Real files are full of repeats at every scale: common words, the same variable name all through a program, the same row structure in a spreadsheet, the same chorus in a song.

But the two methods are not rivals; they cover each other's weaknesses. After LZ has replaced repeats with pointers, the remaining stream of literals and pointer values is still lopsided: spaces and e's are still common literals, short copies are more common than long ones. So the next step is to Huffman-code that stream. That combination is the DEFLATE format, used inside zip files, gzip, and PNG images.

Below, a toy version of each approach compresses four kinds of text. The toy formats are deliberately simple and their costs are spelled out in the readout, so you can see where the bits go; real DEFLATE uses cleverer codes for lengths and distances and does somewhat better.

Interactive 7.2Compressor Showdown
Pick a text, or type your own
bits needed by each method entropy floor of the letter-frequency model (a limit for letter-by-letter codes only)
 
On this short prose sample, Huffman alone already does well and the toy look-back stage only about breaks even: its repeats are short, and each pointer costs 21 bits in this toy format. Longer documents have more and longer repeats, which is where look-back earns its keep. On the song, whole repeated lines collapse into pointers, and look-back plus Huffman wins easily. On abab… the look-back coders go far below the violet line, because that line only describes letter-by-letter coding. On random letters, Huffman gets each letter down to about log₂ 27 ≈ 4.75 bits, simply because there are only 27 symbols. Look-back finds almost nothing to point at, and fixed-size tokens end up worse than plain text: every literal pays a flag bit.

Spot the most important lesson in that last figure: on "abab…" the look-back coders beat the violet "entropy floor" by a mile. That does not break Shannon's theorem. The violet line is the entropy of the letter-frequency model, which assumes every character is drawn independently. The true source of "abab…" is nothing like that; its entropy per character is nearly zero, and that is the floor that actually binds. A compressor can only beat a model's floor by using a better model of the data.

Which floor?

Every entropy is the entropy of some model. Look-back compression, context models and language models all go below the letter-frequency floor by capturing structure that model ignores; none of them can beat the true entropy of the source on average.

What modern compressors add

Newer formats keep the same two ingredients, find repeats and then code what is left according to its statistics, and improve each of them: much larger look-back windows, smarter searches for good matches, and probability models that adapt to context instead of one fixed Huffman table. Some replace Huffman with arithmetic-style coding, which, as Chapter 5 hinted, lets a symbol cost a fraction of a bit.

All of them obey the pigeonhole limit of Chapter 6. They shrink the files they were designed to expect, text, code and structured data, and they leave random-looking data, including already-compressed photos and videos, essentially unchanged.

Intuition Check

Why does "abababab…" compress enormously with look-back compression but not at all with letter-by-letter Huffman coding?

Intuition Check

A text never repeats any run of three characters. What does the toy look-back coder with fixed-size tokens do to it?

Connections

What you now see

  • Look-back (LZ77-style) compression replaces repeated text with pointers: go back d, copy L.
  • It needs no letter statistics and catches order-based redundancy that Huffman cannot see; copies may even overlap themselves.
  • DEFLATE, inside zip, gzip and PNG, runs look-back first and Huffman-codes the result.
  • A compressor can beat the letter-frequency "floor" when it models the data better; it can never beat the true entropy of the source on average.
  • Text with no repeats gives look-back nothing to point at, and the token overhead makes it slightly larger.