You are on page 1of 6

Data Structures (CS301) Total Marks: 20

Assignment # 02 Due Date: 11/08/2022


Semester Spring 2022

Student Name:- Abdul Ahad


Student VU ID:- BC200410608

Instructions:

Please read the following instructions carefully before solving & submitting assignment:
It should be clear that your assignment will not get any credit (marks) if:
 The submitted assignment file is not in “.doc” or “.docx” format.
 The submitted assignment file does not open or corrupted.
 The assignment is copied (from another student or internet).

Uploading instructions:

 Do not wait for grace day. Grace day is given only if there is problem with LMS on due date. Submit
your solution within due date.

It is clearly mentioned to submit your solution only in “.doc/.docx” format. Assignment submitted in any other
format like PDF, CPP, Any Image, Screenshot etc. will get zero marks.

Objective:

The objective of this assignment is to get hands on practice of


 Construction of frequency table
 Construction of Huffman Encoding tree
 Checking efficiency of Huffman encoding(compression) technique

For any query about the assignment, contact us at cs301@vu.edu.pk

Problem Statement: Marks 20


A competition of best quotes is being held in an educational institute. Administration of the institute will
collect the quotes (messages) from students over the internet as a text file. A task assigned to you is to use the
Huffman encoding technique and build a binary tree that will be used to encode and decode the content of text
files. Consider the message given below that is saved in text files and received through the internet. You have to
build a frequency table and Huffman encoding binary tree. Furthermore, calculate the efficiency of this encoding
technique and calculate how much bandwidth is saved for compressing the files/messages of 10,000 students.
Text Message:
Education is one thing no one can take away from you

Solution Of The Given Statement By Me:

You need to perform following step by step tasks to compress the message, calculate the efficiency and find
bandwidth saved for the messages of 10,000 students:

1. Count all the letters including space from the given text message.

I count all the letters including space from the given text message and that are “52” characters.

2. Draw a table with column names (letter, frequency, original bits, encoded bits)

Here I draw a table with the given column names of letter, frequency, original bits, encoded bits.

Letter Frequency Original Bits Encoded Bits

3. Fill the table with letters, frequency, original bits (for original bits get ASCII decimal code of each letter,
convert the decimal ASCII into 8 bits binary code) and encoded bits (these can be found from Huffman
encoding tree as mentioned in point 5).

Letter Frequency Original Bits


Space 10 00100000
n 6 01101110
o 6 01101111
a 5 01100001
e 4 01100101
t 3 01110100
i 3 01101001
u 2 01110101
c 2 01100011
y 2 01111001
d 1 01100100
s 1 01110011
h 1 01101000
g 1 01100111
k 1 01101011
w 1 01110111
f 1 01100110
r 1 01110010
m 1 01101101

4. Draw final Huffman encoding tree with the help of frequency table. (Step by step construction of
Huffman encoding tree is not required, just show the final tree in the solution file).
Here I draw the final Huffman encoding tree with the help of frequency table.

5. Get the encoded bits from tree and fill code of each letter in last column of table constructed in step 2.

I get all the encoded bits from tree and fill code of each letter in last column of table I constructed in step 2 of
the given statements.
Letter Frequency Original Bits Encoded Bits
Space 10 00100000 00
n 6 01101110 100
o 6 01101111 011
a 5 01100001 1111
e 4 01100101 1110
t 3 01110100 0101
i 3 01101001 1010
u 2 01110101 11001
c 2 01100011 10110
y 2 01111001 10111
d 1 01100100 110000
s 1 01110011 110001
h 1 01101000 110110
g 1 01100111 110111
k 1 01101011 110100
w 1 01110111 110101
f 1 01100110 010010
r 1 01110010 010011
m 1 01101101 01000

6. Calculate the efficiency of Huffman encoding technique.


(For efficiency use total original bits, total compressed (encoded) bits and find what percentage of
memory is saved with the help of Huffman encoding technique).

Using the Huffman encoding method entire 93 bits are required . Whereas if I send the unique message
consist of total characters “52” then each bit requires 8 bits. So there the tangible message will require
52*8 = 416 bits.

Here I Explain With Formula :

No. of bits With ASCII method = numbers of characters * numbers of encoding bits (8)
= 52 * 8
= 416 bits

No. of bits With Huffman method = numbers of frequencies * number of encoded bits
= (10 x 2) + (6 x 3) + (6 x 3) + (5 x 4) + (4 x 4) + (3 x 4) + (3 x 4) + (2 x 5) + (2 x 5) + (2 x 5) + (1 x 6) +
(1 x 6) + (1 x 6) + (1 x 6 )+ (1 x 6) + (1 x 6) + (1 x 6) + (1 x 6) + (1 x 5)
= 20 + 18 + 18 + 20 + 16 + 12 + 12 + 10 + 10 + 10
+6+6+6+6+6+6+6+6+5
= 199bits

Saved bits = 416 – 199


= 217 bits

Average of saved bits = saved bits / total bits x 100


= 217 / 416 x 100
= 52.16 %

7. For the calculation of bandwidth saved for 10,000 messages, use the calculation performed in step 6.

After my conclusion of the previous steps the Calculation of bandwidth saved for 10,000 messages by
using the calculation performed in step 6 the final answer is given below:-

Bandwidth saved = 217 x 10000

= 2170000 bits is the final step’s Answer.

Note:
Consider all the characters/letters are in lower case and new line characters are not used in the message.
Code is not required.

Lectures Covered:
This assignment will cover lectures 16 to 28.

Deadline:
Your assignment must be uploaded/submitted on/before Thursday, August 11, 2022.

You might also like