Adaptive Huffman Coding PDF
Adaptive Huffman Coding PDF
• One pass
• During the pass calculate the frequencies
CSEP 590
• Update the Huffman tree accordingly
Data Compression – Coder – new Huffman tree computed after transmitting the
Autumn 2007 symbol
– Decoder – new Huffman tree computed after receiving the
symbol
Adaptive Huffman Coding • Symbol set and their initial codes must be known
ahead of time.
• Need NYT (not yet transmitted symbol) to indicate a
new leaf is needed in the tree.
11
5 6
a a
3 3
d d
1 2
c b c b
CSEP 590 - Lecture 2 - Autumn 2007 3 CSEP 590 - Lecture 2 - Autumn 2007 4
1
Initialization Initialization
• Symbols a1, a2, ... ,am have a basic prefix code, • The tree will encode up to m + 1 symbols
used when symbols are first encountered. including NYT.
• Example: a, b ,c, d, e, f, g, h, i, j • We reserve numbers 1 to 2m + 1 for node
numbering.
0 1
• The initial Huffman tree consists of a single
0 1 0 1 node weight
0 1 0 1 0 1 0 1
0 2m + 1
1 0 1 NYT node number
a b c d e f 0
g h i j
CSEP 590 - Lecture 2 - Autumn 2007 7 CSEP 590 - Lecture 2 - Autumn 2007 8
CSEP 590 - Lecture 2 - Autumn 2007 9 CSEP 590 - Lecture 2 - Autumn 2007 10
2
Example Example
• aabcdad • aabcdad
1 21 1 21
0 1 0 1
0 19 1 20 0 19 1 20
NYT a NYT a
CSEP 590 - Lecture 2 - Autumn 2007 13 CSEP 590 - Lecture 2 - Autumn 2007 14
Example Example
• aabcdad • aabcdad
1 21 2 21
0 1 0 1
0 19 2 20 0 19 2 20
NYT a NYT a
NYT
fixed code for b
CSEP 590 - Lecture 2 - Autumn 2007 15 CSEP 590 - Lecture 2 - Autumn 2007 16
Example Example
• aabcdad • aabcdad
2 21 2 21
0 1 0 1
0 19 2 20 0 19 2 20
0 1 a 0 1 a
0 17 0
a 18 0 17 1
a 18
NYT b NYT b
CSEP 590 - Lecture 2 - Autumn 2007 17 CSEP 590 - Lecture 2 - Autumn 2007 18
3
Example Example
• aabcdad • aabcdad
2 21 3 21
0 1 0 1
1 19 2 20 1 19 2 20
0 1 a 0 1 a
0 17 1
a 18 0 17 1
a 18
NYT b NYT b
fixed code for c
NYT
output = 00010001 output = 0001000100010
CSEP 590 - Lecture 2 - Autumn 2007 19 CSEP 590 - Lecture 2 - Autumn 2007 20
Example Example
• aabcdad 3 21 • aabcdad 3 21
0 1 0 1
1 19 2 20 1 19 2 20
0 1 a 0 1 a
0 17 a
1 18 0 17 a
1 18
0 1 b 0 1 b
0 15 0a 16 0 15 1a 16
NYT c NYT c
CSEP 590 - Lecture 2 - Autumn 2007 21 CSEP 590 - Lecture 2 - Autumn 2007 22
Example Example
• aabcdad 3 21 • aabcdad 3 21
0 1 0 1
1 19 2 20 2 19 2 20
0 1 a 0 1 a
1 17 a
1 18 1 17 a
1 18
0 1 b 0 1 b
0 15 a1 16 0 15 a1 16
NYT c NYT c
CSEP 590 - Lecture 2 - Autumn 2007 23 CSEP 590 - Lecture 2 - Autumn 2007 24
4
Example Example
4 21
• aabcdad 4 21 • aabcdad 0 1
0 1
2 19 2 20
2 19 2 20 0 1 a
0 1 a
1 17 a
1 18
1 17 a
1 18 0 1 b
0 1 b
0 15 a1 16
0 15 a1 16 0 1 c
NYT c fixed code for d 0 13 0
a 14
NYT
NYT d
output = 0001000100010000011
output = 0001000100010000011
CSEP 590 - Lecture 2 - Autumn 2007 25 CSEP 590 - Lecture 2 - Autumn 2007 26
Example Example
4 21 4 21
• aabcdad 0 1 • aabcdad 0 1
2 19 2 20 2 19 2 20
0 1 a 0 1 a
1 17 a
1 18 1 17 a
1 18 exchange!
0 1 b 0 1 b
0 15 a1 16 1 15 a1 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT d NYT d
Example Example
4 21 4 21
• aabcdad 0 1 • aabcdad 0 1
2 19 2 20 2 19 2 20
0 1 a 0 1 a
1 17 1 18 1 17 2 18 exchange!
b 0 1 b 0 1
1 15 1a 16 1 15 1a 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT d NYT d
5
Example Example
4 21 4 21
• aabcdad 0 1 • aabcdad 0 1
2 19 2 20 2 19 3 20
a 0 1 a 0 1
1 17 2 18 1 17 2 18
b 0 1 b 0 1
1 15 1
a 16 1 15 a1 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT d NYT d
Example Example
5 21 Note: the first a is coded 5 21
• aabcdad 0 1 as 000, the second as 1, • aabcdad 0 1
and the third as 0
2 19 3 20 3 19 3 20
a 0 1 a 0 1
1 17 2 18 1 17 2 18
b 0 1 b 0 1
1 15 1
a 16 1 15 a1 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT d NYT d
Example Example
6 21 6 21
• aabcdad 0 1 • aabcdad 0 1
3 19 3 20 3 19 3 20
a 0 1
exchange! a 0 1
1 17 2 18 1 17 2 18
b 0 1 d 0 1
1 15 1a 16 1 15 1a 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT d NYT b
6
Example Example
6 21 6 21
• aabcdad 0 1 • aabcdad 0 1
3 19 3 20 3 19 4 20
a 0 1 a 0 1
2 17 2 18 2 17 2 18
d 0 1 d 0 1
1 15 1
a 16 1 15 a1 16
0 1 c 0 1 c
0 13 1
a 14 0 13 1
a 14
NYT b NYT b
CSEP 590 - Lecture 2 - Autumn 2007 41 CSEP 590 - Lecture 2 - Autumn 2007 42