Professional Documents
Culture Documents
Greedy Algorithms: Part - 4: Huffman Coding
Greedy Algorithms: Part - 4: Huffman Coding
2
Two Ways to Compress data
• Fixed-Length Codewords
• Variable-Length Codewords
3
Example
E.g.:
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
4
Fixed-Length Codes
E.g.: Data file containing 100,000 characters
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
• 3 bits needed
5
Huffman Codes
• Idea:
– Variable Length Codes
6
Constructing a Huffman Code
• Idea:
– Arranging the characters in Ascending order of their
frequencies
f: 5 e: 9 c: 12 b: 13 d: 16 a: 45
7
Example
f: 5 e: 9 c: 12 b: 13 d: 16 a: 45 c: 12 b: 13 14 d: 16 a: 45
0 1
f: 5 e: 9
14 d: 16 25 a: 45 25 30 a: 45
0 1 0 1 0 1 0 1
f: 5 e: 9 c: 12 b: 13 c: 12 b: 13 14 d: 16
0 1
f: 5 e: 9
a: 45 55 0 100 1
0 1
a: 45 55
25 30 0 1
0 1 0 1
25 30
c: 12 b: 13 14 d: 16 0 1 0 1
0 1 c: 12 b: 13 d: 16
14
f: 5 e: 9 0 1
f: 5 e: 9 8
Variable-Length Codes
E.g.: Data file containing 100,000 characters
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
Variable length
0 101 100 111 1101 1100
Codewords
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
Fixed Length
000 001 010 011 100 101
Codewords
Variable length
0 101 100 111 1101 1100
Codewords
300000 – 224000
X 100
300000
= 25.33%
Encode : abcde
Decode: 101100011111011100
12