LZW Compression
In this technique trick is that string-to-codeword mapping is created dynamically by the encoder also recreated dynamically by the decoder need not pass the code table between the two is a lossless compression algorithm degree of compression hard to predict depends on data, but gets better as codeword table contains more strings.
step 1.Initialize table with single character strings
step 2.STRING = first input character
step 3.WHILE not end of input stream
step 4.END WHILE
step 5.Output code for string
STRING TABLE |
ENCODER OUTPUT |
||
String |
codeword |
Representing |
output code |
BA |
256 |
B |
66 |
AB |
257 |
A |
65 |
BAA |
258 |
BA |
256 |
ABA |
259 |
AB |
257 |
AA |
260 |
A |
65 |
AA |
260 |
LZW De-Compression
step 1.Initialize table with single character strings
step 2.OLD = first input code
step 3.output translation of OLD
step 4.WHILE not end of input stream
S = translation of OLD
S = S + C
step 5.ELSE
S=translation of NEW
step 6.END WHILE
STRING TABLE |
ENCODER OUTPUT |
|
|||
|
String |
codeword |
string |
||
|
B |
||||
|
BA |
256 |
A |
||
|
AB |
257 |
BA |
||
|
BAA |
258 |
AB |
||
|
ABA |
259 |
A |
||
|
AA |
260 |
AA |
||
Doc | Algorithm Explanation File | ||
Source Code | Complete Code files |