You are on page 1of 46

ADAPTIVE HUFFMAN CODE

The contents on this presentation have been


Compiled for academic use by Mukesh T
Israni
Contents of these slides developed from:
Khalid Sayood Introduction to Data
Compression (Third Edition)
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Huffman coding requires knowledge of the
probabilities of the source sequence.
If this knowledge is not available, Huffman
coding becomes a two-pass procedure.
The statistics are collected in the first pass,
and the source is encoded in the second
pass.
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


In order to convert this algorithm into a
one-pass procedure, Faller and Gallagher
independently developed adaptive
algorithms to construct the Huffman code
based on the statistics of the symbols
already encountered. These were later
improved by Knuth and Vitter.
Slides are private property of MI, Do not reproduce the contents without permission

Coding Techniques
If a1,a2,am are alphabets of size m
then choose e and r such that m=2 e + r
where 0 r 2ealso the Node will start
at 2m-1
If 1 k 2r then the symbol a k is
encoded as (e + 1) bit of k-1 else a k is
encoded as e bit binary representation of
k-r -1 (where k is current alphabet)
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Example:
For English Alphabets A to Z
m = 26
Then
e = 4 and
r =10
So alphabets A to T will be represented as 5
bit of k-1 and U to Z will be represented as 4
bit of k-r-1
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


ENCODING FLOWCHART

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


DECODING FLOWCHART

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Example:
Encode and Decode the symbols: aardvark
Step 1:
Start at the NYT (no. of node will be 51)
51

0 - NYT

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 2: First letter is A(1) which is a new
symbol and is between 1 and 2r so its code
will be e+1 (5) bit of k-1(0) so it will be
00000 which is transmitted
Transmission: 00000
Step 3:The Tree structure is updated by
splitting the NYT as shown below:
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49 0 - NYT
Step 3: Next Symbol is A

1
A- 1

50

It is not a new symbol and its code from the


tree will be (1) and 1 is transmitted and tree is
updated
Slides are private property of MI, Do not reproduce the contents without permission

Coding Techniques
51
0
49

0 - NYT

1
A- 2

50

Transmission: 000001
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 4: Next symbol is R, its a new symbol
so first code for NYT is transmitted (0) and
Since R(18) is between 1 and 2r its code will
be (18 1= 17) in binary 10001 and tree
will be updated
Transmission: 000001010001
Slides are private property of MI, Do not reproduce the contents without permission

Coding Techniques
51
0
49
0
47

0 - NYT

A- 2

50

1
1-R

48

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 5: Next symbol is D, its a new symbol
so first code for NYT is transmitted (00) and
Since D(4) is between 1 and 2r its code will
be (4 1= 3) in binary 00011 and tree will be
updated
Transmission: 0000010100010000011
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49
0
47
0
45

0NYT

2-A

50

1-

R
1-

48

46

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 6: Next symbol is V, its a new symbol
so first code for NYT is transmitted (000) and
Since V(22) is greater than2r its code will
be in 4 bits and represented as (22-10-1= 11)
in binary 1011 and tree will be updated
Transmission: 00000101000100000110001011
Slides are private property of MI, Do not reproduce the contents without permission

Coding Techniques
51
0

51

49
0

50

47
0

43

146

1-

1
44

0NYT

48

45
0

2-A

1- V

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
It should be noted that weight of node 49 is
greater than weight of node 50 and weight
of node 47 is greater than node 48
So nodes 49 and 50 will be swapped and
nodes 47 and 48 will be swapped and the
tree is redrawn

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49

2-A

50
0
47

1-R

48

0
45
0
43

0- NYT

1-D

1-V

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 7: Next symbol is A, and is not a new
symbol code for A will be (0) and tree
will be updated
Transmission:
000001010001000001100010110

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49

3-A

50
0
47

1-R

48

0
45
0
43

0- NYT

1-D

1-V

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 8: Next symbol is R, and is not a new
symbol code for R will be (10) and tree will
be updated
Transmission:
00000101000100000110001011010

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49

3-A

50
0
47

2- R

48

0
45
0
43

0- NYT

1
1

1-V

1-D

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
Step 9: Next symbol is K(11), its a new
symbol so first NYT(1100) is transmitted and
code for will be (01010) and tree will be
updated
Transmission:
00000101000100000110001011010110001
010
Slides are private property of MI, Do not reproduce the contents without permission

Coding Techniques
51
0
49

3-A

50

2-R

47

48

0
45
0
43
0
41

0- NYT

1-D

1-V

46

44

1-K

42

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
51
0
49

3-A

50
0
47

2-R

48

0
45

1-D
43
0
41

0- NYT

1-K

46
1

1-V

44

42

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Coding Technique
This completes the encoding procedure and
the final transmitted sequence is listed below
we will now use this and decode
Transmission:
00000101000100000110001011010110001
010
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Received bits:
00000101000100000110001011010110001
010
Step 1:
Start at the NYT (no. of node will be 51)
51

0 - NYT

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 2:Read 4 bits (0000), decimal value is 0
which is less than 10(r) so read 5 bits 00000,
add 1 to it 00000+1 = 00001 = 1=A, so first
symbol is A and update tree

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0

A- 1
49 0 - NYT
50
Step 3: Read next bit(1) which traces to A so
next symbol is A and update tree

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

0 - NYT

1
A- 2

50

Step 4: Read next bit(0) traces path to NYT so next


symbol is new. Now read 4 bits (1000) which is less
than (10)r so read 5 bits (10001) and add 1 to get 18
which is R which is the next symbol & update tree.
Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49
0
47

0 - NYT

A- 2

50

1
1-R

48

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 5: Read next 2 bits (00) traces path to
NYT so next symbol is new. Now read 4 bits
(0001) which is less than (10)r so read 5 bits
(00011) and add 1 to get 4 which is D which
is the next symbol & update tree.

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49
0
47
0
45

0NYT

2-A

50

1-

R
1-

48

46

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 6: Read next 3 bits (000) traces path to
NYT so next symbol is new. Now read 4 bits
(1011) which is greater than (10)r so
(11+ 10+ 1 =22) which is V which
is the next symbol & update tree.

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0

51

49
0

50

47
0

43

146

1-

1
44

0NYT

48

45
0

2-A

1- V

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

2-A

50
0
47

1-R

48

0
45
0
43

0- NYT

1-D

1-V

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 7: Read next bit (0) traces path to
A so next symbol is A & update tree.

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

3-A

50
0
47

1-R

48

0
45
0
43

0- NYT

1-D

1-V

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 8: Read next bit (10) traces path to
R so next symbol is R & update tree.

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

3-A

50
0
47

2- R

48

0
45
0
43

0- NYT

1-D

1-V

46

44

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
Step 9: Read next 4 bits (1100) traces path to
NYT so next symbol is new. Now read 4 bits
(0101) which is less than (10)r so read 5 bits
(01010) And add 1 to get 11 which is K so
next symbol is K & update tree.

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

3-A

50

2-R

47

48

0
45
0
43
0
41

0- NYT

1-D

1-V

46

44

1-K

42

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
51
0
49

3-A

50
0
47

2-R

48

0
45

1-D
43
0
41

0- NYT

1-K

46
1

1-V

44

42

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE


Decoding Technique
This was the last bit so the decoding is:
AARDVARK

Slides are private property of MI, Do not reproduce the contents without permission

ADAPTIVE HUFFMAN CODE

Slides are private property of MI, Do not reproduce the contents without permission

You might also like