Lec5 - LZW Compression
Lec5 - LZW Compression
Lecture 5
Lempel Ziv Welsch (LZW) Compression
and Decompression Method
Text Compression: Lempel Ziv Welsch (LZW) algorithm
The original Lempel Ziv approach to data compression was first
published in in 1977.
Terry Welch's refinements to the algorithm were published in
1984. The algorithm is surprisingly simple. In a nutshell, LZW
compression replaces strings of characters with single codes.
Encoder Decoder
As the input is processed, As the code is processed,
develop a dictionary and select reconstruct the dictionary to invert
string of symbols and encode the process of encoding
each string as a token using the
dictionary.
1- LZW coding (Dictionary-based Algorithm)
11/9/2022 3
Important features of LZW
11/9/2022 4
LZW: Compression algorithm:
Initialize Dictionary with alphabet:
Solution
Dictionary Message: a b a b a b a b a
0a Codes :
1b
Dictionary Message: a b a b a b a b a
0a Codes :
1b
Dictionary Message: a b a b a b a b a
0a Codes : 0
1b
2 ab IF STRING+CHAR=ab is in the
Dictionary: No
THEN
STRING = STRING+CHAR
ELSE
STRING=a output the code for STRING: 0
CHAR =b add STRING+CHAR=ab to
STRING+CHAR=ab Dictionary
STRING=b STRING=CHAR=b
ENDIF
LZW Compression : Example (1):
Dictionary Message: a b a b a b a b a
0a Codes : 0 1
1b
2 ab IF STRING+CHAR=ba is in the
Dictionary: No
3 ba THEN
STRING = STRING+CHAR
ELSE
STRING=b output the code for STRING: 1
CHAR =a add STRING+CHAR=ba to
STRING+CHAR=ba Dictionary
STRING=a STRING=CHAR=a
ENDIF
LZW Compression : Example (1):
Dictionary Message: a b ab a b a b a
0a Codes : 0 1
1b
2 ab * IF STRING+CHAR=ab is in the Dictionary:
Yes
3 ba THEN
STRING = STRING+CHAR=ab
ELSE
STRING=a output the code for STRING
CHAR =b add STRING+CHAR to Dictionary
STRING+CHAR=ab STRING=CHAR
STRING=ab ENDIF
LZW Compression : Example (1):
Dictionary Message: a b ab a b a b a
0a Codes : 0 1 2
1b
IF STRING+CHAR=aba is in the
2 ab Dictionary: No
THEN
3 ba STRING = STRING+CHAR
ELSE
4 aba output the code for STRING: 2
STRING=ab add STRING+CHAR=aba to
CHAR =a Dictionary
STRING+CHAR=aba STRING=CHAR=a
STRING=a ENDIF
LZW Compression : Example (1):
Dictionary Message: a b ab ab a b a
0a Codes : 0 1 2
1b
2 ab * IF STRING+CHAR=ab is in the
Dictionary: Yes
3 ba THEN
STRING = STRING+CHAR: ab
4 aba ELSE
output the code for STRING
STRING=a add STRING+CHAR to
CHAR =b Dictionary
STRING+CHAR=ab STRING=CHAR
STRING=ab ENDIF
LZW Compression : Example (1):
STRING=b
CHAR =a
Output code for the STRING: 3
STRING+CHAR=ba
LZW Compression : Example (2)
What would be the encoded version of the following message :
aab bba aab aab bba
If LZW compression algorithm starting with the dictionary containing a,
b, and space were used
a b space
1 2 3
Solution
1) word aab not exist in the dictionary add aab in the dictionary with code : 4
and output codes of aab and space as 1123
a b space aab
1 2 3 4
1) word bba not exist in the dictionary add bba in the dictionary with code : 5
and output codes of bba and space as 2213
a b space aab bba
1 2 3 4 5
1) word aab exist in the dictionary output codes of aab and space as 43
2) word aab exist in the dictionary output codes of aab and space as 43
3) word bba exist in the dictionary output codes of bba and space as 53
Then the compressed text codes are:
1123221343435
LZW Compression : Example (3)
What would be the encoded version of the following message :
aba bba baaab aba baaab baaab
If LZW compression algorithm starting with the dictionary containing a, b, and
space were used
a b space
1 2 3
Solution
word aba not exist in the dictionary add aba in the dictionary with code : 4 and output codes of aab
and space as 1213
a b space aba
1 2 3 4
word bba not exist in the dictionary add bba in the dictionary with code : 5 and output codes of bba
and space as 2213
a b space aba bba
1 2 3 4 5
word baaab not exist in the dictionary add baaab in the dictionary with code : 6 and output codes of
baaab and space as 211123
a b space aba bba baaab
1 2 3 4 5 6
word aba exist in the dictionary output codes of aba and space as 43
word baaab exist in the dictionary output codes of baaab and space as 63
word baaab exist in the dictionary output codes of baaab as 6
Then the compressed text codes are:
12132213 211123 43636
LZW Compression : Example (4)
• AB not exist in the dictionary add AB in the dictionary with code 128 and output code of A : 65
• BA not exist in the dictionary add BA in the dictionary with code 129 and output code of B : 66
• AA not exist in the dictionary add AA in the dictionary with code 130 and output code of A : 65
• ABA not exist in the dictionary add ABA in the dictionary with code 131 and output code of AB : 128
• ABB not exist in the dictionary add ABB in the dictionary with code 132 and output code of AB : 128
• BAA not exist in the dictionary add BAA in the dictionary with code 133 and output code of AB : 129
• ABAA not exist in the dictionary add ABAA in the dictionary with code 134 and output code of ABA :
131
• ABAAA not exist in the dictionary add ABAAA in the dictionary with code 135 and output code of
ABAA : 134
• AAB not exist in the dictionary add AAB in the dictionary with code 136 and output code of AA : 130
• BAB not exist in the dictionary add BAB in the dictionary with code 137 and output code of BA : 1329
• BB not exist in the dictionary add BB in the dictionary with code 138 and output code of B : 66
• BBB not exist in the dictionary add BBB in the dictionary with code 139 and output code of BB : 138
• BBBB Bnot exist in the dictionary add BBBB in the dictionary with code 140 and output code of BBB
: 139
• BB exist in the dictionary output code of BB : 138
2- LZW Decompression Algorithm
Initialize Dictionary
Input code c
Decode code c (index) to w
Output decoded string w
Put w? in Dictionary
REPEAT
a) Input code c
Decode the 1st symbol s1 of the code c
Complete the previous Dictionary entry with s1
b) Finish decoding the remainder of the code c
Output decoded string w
Put put w? in Dictionary
UNTIL no more codes
LZW Decompression : Example (1)
Consider the below code and its predefined dictionary.
0 2 13 1 1
Code 0 1
Key P Q
It is noted that the first code will always defined in the dictionary. Thus,
we will get
P 2 13 1 1 - Since this is the first code that we decompress. We will just
left it without adding any new code to dictionary.
Code 0 1
Key P Q
LZW Decompression : Example (1)
P 2 13 1 1 - Since 2 is not found in dictionary. We will replace 2 with text(0)fc(0) which is
equivalent to PP. New code added into dictionary based on key text(0)fc(0) which is
equivalent to PP.
PPP 1 3 1 1
Code 0 1 2
Key P Q PP
Code 0 1 2 3
Key P Q PP PPQ
LZW Decompression : Example (1)
PPPQ 3 1 1 - Since 3 is found in dictionary. We will replace 3 with text(3)which is
equivalent to PPQ. New code added into dictionary based on key text(1)fc(3) which
is equivalent to QP.
PPPQPPQ 1 1
Code 0 1 2 3 4
Key P Q PP PPQ QP
Code 0 1 2 3 4 5
Code 0 1 2 3 4 5 6
012233588 a 0 1 a
a b
012233588 b 0 1 2 ab
a b ab
012233588 ab 0 1 2 3 abab
a b ab ba
012233588 ab 0 1 2 3 4 ababab
a b ab ba aba
012233588 ba 0 1 2 3 4 5 abababba
a b ab ba aba abb
LZW Decompression : Example (2)