LZW Dictionary Compression
Watch repeated phrases extend a shared dictionary and reconstruct the text from its emitted codes.
By Riano · Published · Updated · Report a correction
LZW Dictionary Compression
Lempel–Ziv–Welch (LZW) is a lossless dictionary compression algorithm that parses input text greedily into the longest recognized phrase w, emits its numeric code, and appends w + k (where k is the next lookahead character) as a new dictionary entry.
Greedy Longest-Prefix Parsing
Both encoder and decoder begin with the same initial alphabet table: A = 0, B = 1, and C = 2, with new phrase codes assigned sequentially starting at 3. Because the encoder only emits existing codes and extends the dictionary by one lookahead character at each boundary, the decoder can reconstruct the exact same dictionary on the fly without transmitting the table.
The KwKwK Decoder Special Case
During decoding, each received code usually indexes a phrase already present in the decoder dictionary. However, when the input contains an immediately overlapping pattern of the form K + w + K + w + K, the encoder emits the newly created code for K + w + K on the very next step before the decoder has finished inserting it. Whenever the incoming code equals the current decoder dictionary size, the phrase must begin and end with the first character of the previous phrase, so the decoder outputs previous + first(previous).
Worked Example
Trace the encoding and decoding of ABABABA with initial dictionary A = 0, B = 1, C = 2:
- Match A, lookahead B: Phrase
ABis absent. Emit 0 (for A), insertAB = 3, set w =B. - Match B, lookahead A: Phrase
BAis absent. Emit 1 (for B), insertBA = 4, set w =A. - Match AB, lookahead A: Phrase
ABis present (code 3), butABAis absent. Emit 3 (for AB), insertABA = 5, set w =A. - Match ABA, end of input: Remaining suffix
ABAmatches code 5. Emit final code 5. - Decoding [0, 1, 3, 5]: Code 0 decodes to A; code 1 decodes to B (adds AB = 3); code 3 decodes to AB (adds BA = 4). When code 5 arrives, dictionary size is 5, triggering previous + first(previous) =
AB+A=ABA.
Experiment
Compare regular dictionary growth with the KwKwK special branch in https://riano.app/module/lzw-compression:
- Use default input
ABABABAand press Step four times to watch codes 0, 1, 3, 5 emit and observe Decoder special case used switch totrueon the final step. - Enter
ABCABCABCAand press Finish to verify emitted codes0, 1, 2, 3, 5, 4, 0where every phrase is already in the decoder table (special casefalse). - Enter the 12-symbol constant run
AAAAAAAAAAAAand press Finish to see phrase lengths grow from 1 to 4 with codes0, 3, 4, 5, 3.
Limitations
This educational module restricts the alphabet to uppercase A, B, C (up to 12 characters) and emits raw integer codes. Production formats such as GIF or Unix compress pack codes into variable-width bit streams and reserve control codes for dictionary reset (CLEAR) and end-of-stream (EOS).
Common Pitfalls
Omitting the code == dict.length branch in an LZW decoder causes immediate decompression failure on repetitive runs like ABABABA or AAAAAAAAAAAA.
References
The LZW dictionary construction and KwKwK decoding rule follow standard compression specifications:
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive LZW Dictionary Compression module.
Try LZW Dictionary Compression on Riano →