Huffman Example
Huffman Example
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
𝑼𝒏𝒄𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒆𝒅 (# 𝒃𝒊𝒕𝒔) 𝟏𝟏 × 𝟖 𝟖𝟖
𝑪𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒊𝒐𝒏 𝑹𝒂𝒕𝒊𝒐 = = = = 𝟑. 𝟖𝟐𝟔
𝑪𝒐𝒎𝒑𝒓𝒆𝒔𝒔𝒆𝒅 (# 𝒃𝒊𝒕𝒔) 𝟐𝟑 𝟐𝟑
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