Arab Academy for Science and Technology and Maritime Transport
College of Computing and Information Technology
Course Data Compression (CS411)
Lecturer Dr. Radwa Fathalla
TA Mahmoud ElMorshedy
Huffman Example
1. Encode the following message using Huffman code and calculate the compression ratio:
abracadabra
Probability Model:
Character Frequency
a 5
b 2
c 1
d 1
r 2
Huffman Tree:
11
0 1
5 6
0 1
a
2 4
0 1
b
2 2
0 1
r
1 1
c d
1
Codewords Table:
Character Codeword
a 0
b 10
c 1110
d 1111
r 110
Encoded Text: 0 10 110 0 1110 0 1111 0 10 110 0
a b r a c a d a b r a
𝑼𝒏𝒄𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒆𝒅 (# 𝒃𝒊𝒕𝒔) 𝟏𝟏 × 𝟖 𝟖𝟖
𝑪𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒊𝒐𝒏 𝑹𝒂𝒕𝒊𝒐 = = = = 𝟑. 𝟖𝟐𝟔
𝑪𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒆𝒅 (# 𝒃𝒊𝒕𝒔) 𝟐𝟑 𝟐𝟑
2. Using the same codewords table from the previous example decode:
1100011100011111010010
For decoding, you may use the table to look for the corresponding character for the code,
or you may directly use the Huffman tree (if given).
1100011100011111010010
1 not in the table
1100011100011111010010
11 also not in the table
1100011100011111010010
110 -> r (from the table)
Output: r
1100011100011111010010
0 -> a
Output: ra
1100011100011111010010
0 -> a
Output: raa
1100011100011111010010
1 not in the table
1100011100011111010010
11 not in the table
1100011100011111010010
111 not in the table
2
1100011100011111010010
1110 -> c
Output: raac
1100011100011111010010
0 -> a
Output: raaca
1100011100011111010010
0 -> a
Output: raacaa
1100011100011111010010
1111 -> d
Output: raacaad
1100011100011111010010
Output: raacaadb
1100011100011111010010
Output: raacaadbb
1100011100011111010010
Output: raacaadbba
1100011100011111010010
Output: raacaadbbab
Decoded: raacaadbbab