An Example of LZW Compression Suppose that input strings are written over the input alphabet { a , b , c }. Consider the input string ababbaababac where under it are the codes that are sent when it is compressed using the LZW algorithm, and below it the dictionary that is constructed: a b a b b a a b a b a c | | \ / \ / \ / \ / | 0 1 3 4 3 7 2 0: a 1: b 2: c 3: ab 4: ba 5: abb 6: baa 7: aba 8: abac Before compression begins, the dictionary is initialized with all single character strings, which for this example are the characters a , b, c with indices 0, 1, 2 respectively.
(encoding continued) Next, compression proceeds in seven basic steps, where each step reads incoming characters up to a longest match to the dictionary, outputs the corresponding dictionary index, and then adds a new entry to the dictionary consisting of this longest match followed by the next incoming input character: Encoder Step 1. 0 is sent for the character a , and the string ab is added to the dictionary as entry 3 (the current match a followed by the next input character b ). Encoder Step 2. 1 is sent for the character b , and the string ba is added to the dictionary as entry 4 (the current match b followed by the next input character a ). Encoder Step 3. 3 is sent for the string ab , and the string abb is added to the dictionary as entry 5 (the current match ab followed by the next input character b ). Encoder Step 4. 4 is sent for the string ba , and the string baa is added to the dictionary as entry 6 (the current match ba followed by the next input character a ). Encoder Step 5. 3 is sent for the string ab , and the string aba is added to the dictionary as entry 7 (the current match ab followed by the next input character a ). Encoder Step 6. 7 is sent for the string aba , and the string abac is added to the dictionary as entry 8 (the current match aba followed by the next input character c ). Encoder Step 7. 2 is sent for the string c .
Now consider how LZW decompression operates: Before compression begins, the dictionary is initialized with all single character strings, which for this example are the characters a , b, c with indices 0, 1, 2 respectively. Next, the compressed data consisting of the sequence of 7 indices 0 1 3 4 3 7 2 is decompressed in 7 basic steps, where each step adds the same entries to the dictionary as were added during compression, but at one step later, because the decoder does not know the next input character to follow until the next index is received and its first character can be used: Decoder Step 1. 0 is received and a is output. Decoder Step 2. 1 is received, b is output, and the string ab is added to the dictionary as entry 3 (the previous match a followed by the first character b of the current match b ). Decoder Step 3. 3 is received, the string ab is output, and the string ba is added to the dictionary as entry 4 (the previous match b followed by the first character a of the current match ab ). Decoder Step 4. 4 is received, the string ba is output, and the string abb is added to the dictionary as entry 5 (the previous match ab followed by the first character b of the current match ba ). Decoder Step 5. 3 is received, the string ab is output, and the string baa is added to the dictionary as entry 6 (the previous match ba followed by the first character a of the current match ab ). Decoder Step 6. 7 is received, but dictionary entry 7 is not yet defined. The decoder can deduce that it must be that the encoder immediately used the dictionary entry it constructed at step 6 for its step 7. Hence, it must be that the encoder's dictionary entry 7 is the same as its dictionary entry 6 followed by the first character of dictionary entry 6, and so the decoder knows to output the string aba , and add the string aba to the dictionary as entry 7 (the previous match ab followed by the first character a of the current match aba ). Decoder Step 7. 2 is received, the string c is output, and the string abac is added to the dictionary as entry 8 (the previous match aba followed by the first character c of the current match c ).
Recommend
More recommend