previous main page content next

LZW - compression

Main features


LZW (Lempel-Ziv-Welch) is a universal lossless data compression algorithm created by Abraham Lempel, Jacob Ziv, and Terry Welch. It was published by Welch in 1984 as an improved implementation of the LZ78 algorithm published by Lempel and Ziv in 1978. The algorithm is designed to be fast to implement, but not necessarily optimal since it does not perform any analysis on the data. It would typically compress large English texts to about half of their original sizes.


The method became widely used in the program compress, which became a more or less standard utility in Unix systems circa 1986. (It has since disappeared from many for both legal and technical reasons.) Several other popular compression utilities also used the method, or closely related ones. It became very widely used after it became part of the GIF image format in 1987. It may also (optionally) be used in TIFF files. LZW compression provided a better compression ratio, in most applications, than any well-known method available up to that time. It became the first widely used universal data compression method on computers.


The compressor algorithm builds a string translation table from the text being compressed. The string translation table maps fixed-length codes (usually 12-bit) to strings. The string table is initialized with all single-character strings (256 entries in the case of 8-bit characters). As the compressor character-serially examines the text, it stores every unique two-character string into the table as a code/character concatenation, with the code mapping to the corresponding first character. As each two-character string is stored, the first character is outputted. Whenever a previously-encountered string is read from the input, the longest such previously-encountered string is determined, and then the code for this string concatenated with the extension character (the next character in the input) is stored in the table. The code for this longest previously-encountered string is outputted and the extension character is used as the beginning of the next string.

The decompressor algorithm only requires the compressed text as an input, since it can build an identical string table from the compressed text as it is recreating the original text. However, an abnormal case shows up whenever the sequence character/string/character/string/character (with the same character for each character and string for each string) is encountered in the input and character/string is already stored in the string table. When the decompressor reads the code for character/string/character in the input, it cannot resolve it because it has not yet stored this code in its table. This special case can be dealt with because the decompressor knows that the extension character is the previously-encountered character.


 1:  begin
 2:     create_empty_dictionary(D)
 3:     for each (a in ABECEDA) do
 4:      begin 
 5:        add_to_dictionary(a,D) 
 6:      end 
 7:     S := ""  
 8:     while (!EOF) do 
 9:      begin  
10:        readSymbol(z)
11:        if (find_in_dictionary(Sz,D)) then
12:           S := Sz
13:        else 
14:         begin
15:           output(pointer(S,D))
16:           add_to_dictionary(Sz,D)
17:           S := z
18:         end
19:      end
20:  end


  1. LZW. Wikipedia, the free encyclopedia.
  2. Mark Nelson: LZW Data Compression.
  3. Interactive LZW compression
  4. Martin Kopka: Dictionary compression (LZW).
  5. Terry Welch: A Technique for High-Performance Data Compression. Computer, June 1984.
  6. Michael Dipperstein: Lempel-Ziv-Welch (LZW) Encoding Discussion and Implementation.