0% found this document useful (0 votes)
57 views

Dictionary Methods: Introduction To Lempel-Ziv Encoding

The document discusses dictionary coding techniques for data compression, specifically Lempel-Ziv encoding methods LZ77 and LZ78. It explains that dictionary coding builds a dictionary of frequently occurring symbols and strings, and encodes data by referencing the dictionary rather than using statistics like Huffman coding. Adaptive dictionary techniques like LZ77 and LZ78 dynamically build the dictionary during encoding and decoding.

Uploaded by

MOVIETADKA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Dictionary Methods: Introduction To Lempel-Ziv Encoding

The document discusses dictionary coding techniques for data compression, specifically Lempel-Ziv encoding methods LZ77 and LZ78. It explains that dictionary coding builds a dictionary of frequently occurring symbols and strings, and encodes data by referencing the dictionary rather than using statistics like Huffman coding. Adaptive dictionary techniques like LZ77 and LZ78 dynamically build the dictionary during encoding and decoding.

Uploaded by

MOVIETADKA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

20-02-2023

Dictionary Methods

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

Introduction to Lempel-Ziv Encoding

 Data compression up until the late 1970's mainly directed


towards creating better methodologies for Huffman coding.

 An innovative, radically different method was introduced


in1977 by Abraham Lempel and Jacob Ziv.

 This technique (called Lempel-Ziv) actually consists of two


considerably different algorithms, LZ77 and LZ78.

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

1
20-02-2023

Dictionary Coding

• Dictionary coding is different from Huffman coding and


arithmetic coding.
▫ Both Huffman and arithmetic coding techniques are
based on a statistical model (e.g., occurrence
probabilities).
▫ In dictionary-based data compression techniques, a
symbol or a string of symbols generated from a source
alphabet is represented by an index to a dictionary
constructed from the source alphabet.

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

Dictionary Coding

• Consider English text coding.

▫ Huffman or arithmetic coding treats each symbol based on


its occurrence probability. That is, the source is modeled as
a memoryless source.
▫ It is well known, however, that this is not true in many
applications.
▫ In text coding, structure or context plays a significant role.
 Very likely that the letter u appears after the letter q.
 Likewise, it is likely that the word “concerned” will
appear after “As far as the weather is.”
DCE//SEM VI//EXTC//Dr. Vishakha
Kelkar

2
20-02-2023

Dictionary Coding

• The strategy of the dictionary coding is to build a dictionary


that contains frequently occurring symbols and string of
symbols.
▫ When a symbol or a string is encountered and it is
contained in the dictionary, it is encoded with an index
to the dictionary.
▫ Otherwise, if not in the dictionary, the symbol or the
string of symbols is encoded in a less efficient manner.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Categorization of Dictionary-Based
Coding Techniques
• The heart of dictionary coding is the formulation of the
dictionary.
• A successfully built dictionary results in data compression;
the opposite case may lead to data expansion.
• According to the ways in which dictionaries are
constructed, dictionary coding techniques can be classified
as static or adaptive.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

3
20-02-2023

Static Dictionary Coding


• A fixed dictionary,
▫ Produced before the coding process
▫ Used at both the transmitting and receiving ends
▫ It is possible when the knowledge about the source
alphabet and the related strings of symbols, also known
as phrases, is sufficient.
• Merit of the static approach: its simplicity.
• Its drawbacks lie on
▫ Relatively lower coding efficiency
▫ Less flexibility compared with adaptive dictionary
techniques

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Static Dictionary Coding

• An example of static algorithms occurs is diagram coding.


▫ A simple and fast coding technique.
▫ The dictionary contains:
 all source symbols and
 some frequently used pairs of symbols.
▫ In encoding, two symbols are checked at once to see if
they are in the dictionary.
 If so, they are replaced by the index of the two symbols in the
dictionary, and the next pair of symbols is encoded in the next
step.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

4
20-02-2023

Adaptive Dictionary Coding


• A completely defined dictionary does not exist prior
to the encoding process and the dictionary is not
fixed.
▫ At the beginning of coding, only an initial dictionary exists.
▫ It adapts itself to the input during the coding process.

• All adaptive dictionary coding algorithms can be


traced back to two different original works by Ziv
and Lempel [ziv 1977, ziv 1978].
▫ The algorithms based on [ziv 1977] are referred to as the
LZ77 algorithms.
▫ Those based on [ziv 1978] the LZ78 algorithms.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZ77: Sliding Window Lempel-Ziv


Cursor

a a c a a c a b c a b a b a c
Dictionary Lookahead
(previously coded) Buffer

• Dictionary and buffer “windows” are fixed length and


slide with the cursor
• Repeat:
Output (p, l, c) where
p = position of the longest match that starts in
the dictionary (relative to the cursor)
l = length of longest match
c = next char in buffer beyond longest match
Advance window by l + 1

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

5
20-02-2023

LZ77: Example

a a c a a c a b c a b a a a c (_,0,a)

a a c a a c a b c a b a a a c (1,1,c)

a a c a a c a b c a b a a a c (3,4,b)

a a c a a c a b c a b a a a c (3,3,a)

a a c a a c a b c a b a a a c (1,2,c)

Dictionary (size = 6) Longest match

Buffer (size = 4) Next character

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZ77 Decoding
• Decoder keeps same dictionary window as encoder.
• For each message it looks it up in the dictionary and
inserts a copy at the end of the string
• What if l > p? (only part of the message is in the
dictionary.)
• E.g. dict = aac, codeword = (3,4,b)

• Simply copy from left to right


for (i = 0; i < length; i++)
out[cursor+i] = out[cursor-offset+i]
• Out = aacab

a a c a a c a b c a b a a a c (3,4,b)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

6
20-02-2023

LZ77 Optimizations used by gzip


• LZSS: Output one of the following two formats
(0, position, length) or (1,char)
• Uses the second format if length < 3.
a a c a a c a b c a b a a a c (1,a)

a a c a a c a b c a b a a a c (1,a)

a a c a a c a b c a b a a a c (1,c)

a a c a a c a b c a b a a a c (0,3,4)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZ77 example
• Encode and decode the following example using
LZ77
• ‘c a b r a c a d a b r a r r a r r a d’

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

7
20-02-2023

Comparison to Lempel-Ziv 78
• Both LZ77 and LZ78 and their variants keep a
“dictionary” of recent strings that have been
seen.
• The differences are:
▫ How the dictionary is stored (LZ78 is a table)
▫ How it is extended (LZ78 only extends an existing entry
by one character)
▫ How it is indexed (LZ78 indexes the entries in the table)
▫ How elements are removed

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZ78 Compression Algorithm


Start with empty dictionary

WHILE (there more characters in the char stream)


P=next char
Is P in the dictionary?
yes:uppend the next char to P
check the dictionary
send the index of the max match
If not in the dictionary
add entry in dictionary send the index of the prev seen
string from dictionary
If char not in dictionary send <0,code of the symbol>
END

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

8
20-02-2023

Example 1: LZ78 Compression 1


Example 1: Use the LZ78 algorithm to encode the message

ABBCBCABABCAABCAAB

Solution: The encoding process is presented below in which:


 Column Index indicates the index
 The column DICTIONARY shows what string has been
added to the dictionary. The index of the string is equal to
the step number.
 The column OUTPUT presents the output in the form
(W,C). W represents the index of prefix in the dictionary.
 The output of each step decodes to the string that has been
added to the dictionary.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 1: LZ78 Compression Step 1


ABBCBCABABCAABCAAB POS = 1
C = empty
P = empty
Index DICTION OUTPUT
ARY
1 A (0,A)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

9
20-02-2023

Example 1: LZ78 Compression Step 2


ABBCBCABABCAABCAAB POS = 2
C = empty
P = empty

Index DICTIO OUTPUT


NARY
1 A (0,A)
2 B (0,B)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 1: LZ78 Compression Step 3


ABBCBCABABCAABCAAB POS = 3
C = empty
P = empty

Index DICTIONAR OUTPUT


Y
1 A (0,A)
2 B (0,B)
3 BC (2,C)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

10
20-02-2023

Example 1: LZ78 Compression Step 4


ABBCBCABABCAABCAAB POS = 5
C = empty
P = empty
Index DICTION OUTPUT
ARY
1 A (0,A)
2 B (0,B)
3 BC (2,C)
4 BCA (3,A)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 1: LZ78 Compression Step 5


ABBCBCABABCAABCAAB POS = 8
C = empty
P = empty
Index DICTION OUTPUT
ARY
1 A (0,A)
2 B (0,B)
3 BC (2,C)
4 BCA (3,A)
5 BA (2,A)

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

11
20-02-2023

Example 1: LZ78 Compression Step 6


ABBCBCABABCAABCAAB POS = 10
C=A
P = BCA
Index DICTION OUTPUT
ARY
1 A (0,A)
2 B (0,B)
3 BC (2,C)
4 BCA (3,A)
5 BA (2,A)
6 BCAA (4,A)
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 1: LZ78 Compression Step 7


ABBCBCABABCAABCAAB POS = 14
C = empty
INDE DICTIONAR OUTPUT P = empty
X Y
1 A (0,A)
B (0,B)
2

3 BC (2,C)
4 BCA (3,A)
5 BA (2,A)
6 BCAA (4,A)
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

7 BCAAB (6,B)

12
20-02-2023

Example 1: Coded Information


 The coded information
<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZ78 Decompression Algorithm


At the start the dictionary is empty
While (more code words in the code stream)
W = code word
C = character following it
output the string of W + C
add the string of W + C to the dictionary
end

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

13
20-02-2023

Example 2: LZ78 Decompression

Example 2: Use the above algorithm to decompress the


compressed output sequence of Example 1, namely,

<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 2: LZ78 Decompression Step 1


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

14
20-02-2023

Example 2: LZ78 Decompression Step 2


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 2: LZ78 Decompression Step 3


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B
3 (2,C) BC BC

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

15
20-02-2023

Example 2: LZ78 Decompression Step 4


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B
3 (2,C) BC BC
4 (3,A) BCA BCA

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 2: LZ78 Decompression Step 5


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B
3 (2,C) BC BC
4 (3,A) BCA BCA
5 (2,A) BA BA

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

16
20-02-2023

Example 2: LZ78 Decompression Step 6


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B
3 (2,C) BC BC
4 (3,A) BCA BCA
5 (2,A) BA BA
6 (4,A) BCAA BCAA
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example 2: LZ78 Decompression Step 7


<(0,A)><(0,B)><(2,C)><(3,A)><(2,A)><(4,A)><(6,B)>

W=empty C=empty

STEP INPUT OUTPUT DICTIONARY


1 (0,A) A A
2 (0,B) B B
3 (2,C) BC BC
4 (3,A) BCA BCA
5 (2,A) BA BA
6 (4,A) BCAA BCAA
7 (6,B) BCAAB
DCE//SEM VI//EXTC//Dr. BCAAB
Vishakha Kelkar

17
20-02-2023

Exercise
Use LZ78 to trace encoding the string
SATATASACITASA.
Also decode it.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Exercise :LZ78

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

18
20-02-2023

Exercise :LZ78

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Exercise :LZ78

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

19
20-02-2023

Compress using LZ-78

39 39 126 126
39 39 126 126
39 39 126 126
39 39 126 126

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

LZ78: Concluding Remarks


 LZ77 is based on a sliding window. Where as the output of
LZ 78 is based on dynamic dictionary.

 The compression ratio of LZ77 is similar to that of LZ78.

 The biggest advantage of LZ78 over the LZ77 algorithm is


the reduced number of string comparisons in each
encoding step.

 More application areas of LZ77 and LZ78:


• LZ77: gzip,PKZIP,
• LZ78: compress, GIF, CCITT (modems), ARC,

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

20
20-02-2023

LZW: Main idea

• Thealgorithm does not use any search buffer, lookahead


buffer, sliding window
• Instead there is a Dictionary of previously encountered
strings
• This Dictionary starts (almost) empty
• The encoder add new entries to the Dictionary during
the message encoding

• The decoder decode codes using Dictionary and


add new entries to the Dictionary during decoding

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Encoding algorithm


Initialize Dictionary with alphabet
STRING = get input character
WHILE there are still input characters DO
CHAR = get input character
IF STRING+CHAR is in the Dictionary
THEN
STRING = STRING+CHAR
ELSE
output the code for STRING
add STRING+CHAR to Dictionary
STRING=CHAR
ENDIF
END of WHILE
Output code for the STRING
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

21
20-02-2023

LZW: Example (0)

Dictionary Message: a b a b a b a b a
0a Codes :
1b

Initialize Dictionary with alphabet

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (1)

Dictionary Message: a b a b a b a b a
0a Codes :
1b

STRING=a STRING = get input character

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

22
20-02-2023

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

LZW: Example (2)

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=a STRING = STRING+CHAR
CHAR =b ELSE
STRING+CHAR=ab output the code for STRING: 0
STRING=b add STRING+CHAR=ab to Dictionary
STRING=CHAR=b
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

23
20-02-2023

LZW: Example (3)

Dictionary Message: a b a b a b a b a
0a Codes : 0 1
1b
2 ab
3 ba

IF STRING+CHAR=ba is in the Dictionary: No


THEN
STRING=b STRING = STRING+CHAR
CHAR =a ELSE
STRING+CHAR=ba output the code for STRING: 1
STRING=a add STRING+CHAR=ba to Dictionary
STRING=CHAR=a
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (4)

Dictionary Message: a b ab a b a b a
0a Codes : 0 1
1b
2 ab *
3 ba

IF STRING+CHAR=ab is in the Dictionary: Yes


THEN
STRING=a STRING = STRING+CHAR=ab
CHAR =b ELSE
STRING+CHAR=ab output the code for STRING
STRING=ab add STRING+CHAR to Dictionary
STRING=CHAR
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

24
20-02-2023

LZW: Example (5)

Dictionary Message: a b ab a b a b a
0a Codes : 0 1 2
1b
2 ab
3 ba
4 aba

IF STRING+CHAR=aba is in the Dictionary: No


THEN
STRING=ab STRING = STRING+CHAR
CHAR =a ELSE
STRING+CHAR=aba output the code for STRING: 2
STRING=a add STRING+CHAR=aba to Dictionary
STRING=CHAR=a
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (6)

Dictionary Message: a b ab ab a b a
0a Codes :0 1 2
1b
2 ab *
3 ba
4 aba

IF STRING+CHAR=ab is in the Dictionary: Yes


THEN
STRING=a STRING = STRING+CHAR: ab
CHAR =b ELSE
STRING+CHAR=ab output the code for STRING
STRING=ab add STRING+CHAR to Dictionary
STRING=CHAR
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

25
20-02-2023

LZW: Example (7)

Dictionary Message: a b ab aba b a


0a Codes :0 1 2
1b
2 ab
3 ba
4 aba *

IF STRING+CHAR=aba is in the Dictionary: Yes


THEN
STRING=ab STRING = STRING+CHAR: aba
CHAR =a ELSE
STRING+CHAR=aba output the code for STRING
STRING=aba add STRING+CHAR to Dictionary
STRING=CHAR
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (8)

Dictionary Message: a b ab aba b a


0a Codes :0 1 2 4
1b
2 ab
3 ba
4 aba
5 abab

IF STRING+CHAR=abab is in the Dictionary: No


THEN
STRING=aba STRING = STRING+CHAR
CHAR =b ELSE
STRING+CHAR=abab output the code for STRING: 4
STRING=b add STRING+CHAR=abab to Dictionary
STRING=CHAR= b
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

26
20-02-2023

LZW: Example (9)

Dictionary Message: a b ab aba ba


0a Codes :0 1 2 4
1b
2 ab
3 ba
4 aba
5 abab

IF STRING+CHAR=ba is in the Dictionary: Yes


THEN
STRING=b STRING = STRING+CHAR: ba
CHAR =a ELSE
STRING+CHAR=ba output the code for STRING
STRING=ba add STRING+CHAR to Dictionary
STRING=CHAR
ENDIF
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (10)

Dictionary Message: a b ab aba ba


0a Codes :0 1 2 4 3
1b
2 ab
3 ba *
4 aba
5 abab

STRING=b
CHAR =a
STRING+CHAR=ba Output code for the STRING: 3

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

27
20-02-2023

LZW: Decoding

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
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (0)

Dictionary Codes: 012436


0a Message:
1b

Initialize Dictionary with alphabet

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

28
20-02-2023

LZW: Example (1)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a
1b
2 a?

Input code c=0


Decode code c to w=a
Output decoded string w=a
Put w?=a? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (2a)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b+
1b
2 ab

a) Input code c=1


Decode the 1st symbol s1=b of the code c=1
Complete the previous Dictionary entry with s1=b

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

29
20-02-2023

LZW: Example (2b)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b
1b
2 ab
3 b?

b) Finish decoding the remainder of the code c=1


Output decoded string w=b
Put put w?=b? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (3a)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b a+
1b
2 ab
3 ba

a) Input code c=2


Decode the 1st symbol s1=a of the code c=2
Complete the previous Dictionary entry with s1=a

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

30
20-02-2023

LZW: Example (3b)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ab
1b
2 ab
3 ba
4 ab?

a) Finish decoding the remainder of the code c=2


Output decoded string w=ab
Put put w?=ab? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (4a) step 1

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b aba+
1b
2 ab
3 ba
4 ab?

a) Input code c=4


 Decode the 1st symbol s1=a of the code c=4
Complete the previous Dictionary entry with s1=a

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

31
20-02-2023

LZW: Example (4a) step 2

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b aba+
1b
2 ab
3 ba
4 aba

a) Input code c=4


Decode the 1st symbol s1=a of the code c=4
 Complete the previous Dictionary entry with s1=a

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (4b)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa
1b
2 ab
3 ba
4 aba
5 aba?

b) Finish decoding the remainder of the code c=4


Output decoded string w=aba
Put put w?=aba? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

32
20-02-2023

LZW: Example (5a)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa b+
1b
2 ab
3 ba
4 aba
5 abab

a) Input code c=3


Decode the 1st symbol s1=b of the code c=3
Complete the previous Dictionary entry with s1=b

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (5b)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa ba
1b
2 ab
3 ba
4 aba
5 abab
6 ba?

b) Finish decoding the remainder of the code c=3


Output decoded string w=ba
Put put w?=ba? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

33
20-02-2023

LZW: Example (6a) step 1

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa ba b+
1b
2 ab
3 ba
4 aba
5 abab
6 ba?

a) Input code c=6


 Decode the 1st symbol s1=b of the code c=6
Complete the previous Dictionary entry with s1=b

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Example (6a) step 2

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa ba b+
1b
2 ab
3 ba
4 aba
5 abab
6 bab

a) Input code c=6


Decode the 1st symbol s1=b of the code c=6
 Complete the previous Dictionary entry with s1=b

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

34
20-02-2023

LZW: Example (6b)

Dictionary Codes : 0 1 2 4 3 6
0a Message: a b ababa ba bab
1b
2 ab
3 ba
4 aba
5 abab
6 bab
7 bab?

a) Finish decoding the remainder of the code c=6


Output decoded string w=bab
Put put w?=bab? in Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

DCE//SEM
VI//EXTC//Dr.
Vishakha Kelkar

Compress using LZW the following string and also decode


the message.
”ABABBABCABABBA"

35
20-02-2023

Exsercise:LZW

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Exsercise:LZW

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

36
20-02-2023

Exsercise:LZW

DCE//SEM VI//EXTC//Dr. Vishakha


Kelkar

Example
Consider the following 4 x 4 8 bit image
39 39 126 126
39 39 126 126 Dictionary Location Entry

39 39 126 126 0 0
1 1
39 39 126 126 . .
255 255
256 -

511 -

Initial Dictionary

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

37
20-02-2023

Example
- Is 39 in the dictionary……..Yes
39 39 126 126 - What about 39-39………….No
- Then add 39-39 in entry 256
39 39 126 126
39 39 126 126
39 39 126 126
Dictionary Location Entry

0 0
1 1
. .
255 255
256 - 39-39

511 -
DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

Example
39 39 126 126
39 39 126 126
39 39 126 126
39 39 126 126

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

38
20-02-2023

Decoding LZW
• The dictionary which was used for encoding need
not be sent with the image.

• A separate dictionary is built by the decoder, on


the “fly”, as it reads the received code words.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

LZW: Notes
• Extremely effective when there are repeated patterns
in the data that are widely spread
• Negatives: Create entries in the dictionary that may
never be used
• Applications: TIFF, V.42 bis modem standard

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

39
20-02-2023

DICTIONARY METHODS

• Adapts well to changes in the file (e.g. a Tar file with many
file types within it).
• Initial algorithms did not use probability coding and
performed poorly in terms of compression. More modern
versions (e.g. gzip) do use probability coding as “second
pass” and compress much better.
• The algorithms are becoming outdated, but ideas are used in
many of the newer algorithms.

DCE//SEM VI//EXTC//Dr. Vishakha Kelkar

40

You might also like