Assignment-3
Huffman Encoding: A Practical
Approach with Illustrative Examples
Submitted To Dr. Mohsin Ashraf
Submitted by: Rajeel Ali
Student Id: F2022466027
Subject: E-Commerce
Section: QC-B
School of Commerce and Accountancy
University of Management and Technology
Example 1 “confessed”
Here is an example of Huffman encoding for the word "Confessed" with a frequency
table and a tree diagram:
Frequency Table:
letter frequency
c 1 3x1 3
o 1 3x1 3
n 1 3x1 3
f 1 3x1 3
d 1 3x1 3
E 2 3x2 6
4
S 2 2x2
=25
Tree Diagram:
Compression ratio
The compression ratio of a Huffman encoded string can be calculated as follows:
Compression Ratio = (Original Size - Compressed Size) / Original Size
In the example of Huffman encoding for the word "Confessed", the original size would
be the number of bits required to represent the original string using a simple binary
representation, which would be 8 bits per character * 9 characters = 72 bits.
The compressed size would be the number of bits required to represent the encoded
string, which in this case is 25 bits.
Therefore, the compression ratio can be calculated as:
Compression Ratio = (72 - 25) / 72 = 65.27%
Example 2 “commerce”
Here is an example of Huffman encoding for the word "commerce" with a frequency
table and a tree diagram:
Frequency Table:
letter frequency
o 1 3x1 3
r 1 3x1 3
m 2 2x2 4
e 2 2x2 4
c 3 3x3 9
=23
Tree Diagram:
Compression ratio
The compression ratio of a Huffman encoded string can be calculated as follows:
Compression Ratio = (Original Size - Compressed Size) / Original Size
In the example of Huffman encoding for the word "Confessed", the original size would
be the number of bits required to represent the original string using a simple binary
representation, which would be 8 bits per character * 8 characters = 64 bits.
The compressed size would be the number of bits required to represent the encoded
string, which in this case is 23 bits.
Therefore, the compression ratio can be calculated as:
Compression Ratio = (64 - 23) / 64 = 64.06%