Lempel-Ziv-Welch
Lempel-Ziv-Welch (LZW) is a universal lossless compression algorithm that builds a dictionary of symbol sequences during encoding, replacing repeated patterns with compact dictionary references. Unlike Huffman Coding, which requires prior knowledge of symbol frequencies, LZW is adaptive: it learns the statistics of the source as it compresses, making it suitable for streams where the distribution is unknown or changing. It is the algorithm behind the UNIX compress utility, the GIF image format, and the V.42bis modem standard.
The Adaptive Principle
LZW was developed by Terry Welch in 1984 as an improvement to the Lempel-Ziv dictionary schemes introduced by Abraham Lempel and Jacob Ziv in 1977 and 1978. The core insight is that the most efficient code for a source is the code designed for that source — but if the source statistics are unknown, the code must be built on the fly.
The algorithm begins with a dictionary containing all single symbols. As it reads the input, it searches for the longest match already in the dictionary, emits the dictionary index of that match, and adds the matched sequence plus the next input symbol as a new dictionary entry. The dictionary grows monotonically; no entry is ever removed. This means the encoder and decoder build identical dictionaries without the encoder ever transmitting the dictionary itself — the decoder can infer each new entry from the sequence of indices it receives.
The result is a compression scheme that approaches the entropy rate of the source asymptotically, provided the source is stationary and ergodic. For non-stationary sources, LZW adapts continuously, though with a lag: sudden changes in statistics are not immediately reflected in the dictionary.
LZW and the Limits of Adaptivity
LZW illustrates a fundamental tension in compression theory: adaptivity versus optimality. A Huffman code designed for the true source distribution achieves the entropy bound and no adaptive scheme can beat it in the long run. But if the true distribution is unknown — which is the case for almost all real data — an adaptive scheme like LZW may outperform any fixed code, because it converges to the true statistics while a fixed code remains tuned to an incorrect assumption.
The Source Coding Theorem guarantees that no lossless compression can beat the entropy rate on average. LZW approaches this bound from below, with the gap determined by how rapidly the dictionary learns the source structure. For sources with long-range correlations — where the optimal dictionary entries would need to capture dependencies across many symbols — LZW's greedy, left-to-right parsing is suboptimal. It captures local repetition well but misses global structure. This is why modern compression systems (like DEFLATE, used in PNG and ZIP) combine LZW-style dictionary coding with Huffman coding of the output: the dictionary captures repeated phrases, and Huffman coding squeezes the residual redundancy from the dictionary indices.
The Patent Controversy and Its Aftermath
LZW was patented by Unisys in 1985, and the company aggressively enforced the patent against developers of GIF and other formats throughout the 1990s and early 2000s. The controversy catalysed the development of patent-free alternatives — most notably the PNG format, which was designed explicitly to replace GIF and avoid LZW. The patent expired in 2003, but the episode left a lasting mark on software culture: it is frequently cited in debates about software patents as a case where a mathematically natural algorithm was privately appropriated, imposing costs on the entire internet.
The LZW patent story is not merely a historical footnote. It is a demonstration that the mathematical structure of compression — the optimal encoding of information — is not independent of the social and legal structures within which it is implemented. An algorithm that is theoretically universal can be practically restricted by patent law, and the restriction can reshape an entire ecosystem of image formats, modem standards, and software licences.
The persistent contrast between Huffman coding and LZW — one optimal but requiring prior knowledge, the other adaptive but asymptotically suboptimal — mirrors a deeper epistemological tension. Do we build models from assumed structure and pay the price of wrong assumptions? Or do we learn structure from data and pay the price of slow convergence? The answer, in compression as in science, is that the best approach depends on what you know, what you can afford to learn, and how wrong your prior assumptions are. The belief that one method is universally superior is not compression theory. It is marketing.
See also: Huffman Coding, Source Coding Theorem, Lempel-Ziv 77, DEFLATE, PNG, Information Theory