Question: Objectives: To Determine the codes to be used for transmitting your name using Huffman coding. Deliverables: Huffman code of your name, along with answers to

Objectives:
To Determine the codes to be used for transmitting your name using Huffman coding.
Deliverables:
Huffman code of your name, along with answers to the given questions.
Background information : In the field of multimedia content development, understanding the factors that influence the quality of images, sound, and other elements is crucial. One such factor is the efficiency of data compression techniques. Huffman coding is a widely-used method for lossless data compression. The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters. The most frequent character gets the smallest code and the least frequent character gets the largest code. The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences) are assigned in such a way that the code assigned to one character is not the prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the generated bitstream. ( Reference : geeksforgeeks.com) Your task is to evaluate how Huffman coding can contribute to enhancing multimedia content quality, particularly focusing on the compression of textual data. Follow the steps below to complete this task:
Your report will be evaluated based on the following points.
1. Executed the given java program and got the output-
2. Huffman code of your name is written properly .
3. All the given tasks are completed successfully
CLO3 marks (5 marks)
Task 1
1. Copy the following program and save it as Huffman.py
2. Execute the program- give and check the output
3. Take snapshot of the output along with timestamp.
4. The output shows the codes used for each letter of your name.
University of Fujairah
BIT-Networking and Security Program
Task 2
Answer the following :
1. Determine the maximum length of the code used and for which letter it is used?
2. Determine the letter which gets shortest length code?
Task 3:
Draw a table as given in the example.
Eg:
MULTIMEDIA
Text: MULTIMEDIA
-- intervals --
'A' : 0.1
'D' : 0.1
'E' : 0.1
'I' : 0.2
'L' : 0.1
'M' : 0.2
'T' : 0.1
'U' : 0.1
--- Printing Codes ---
'A' : 101
'D' : 1100
'E' : 100
'I' : 00
'L' : 011
'M' : 111
'T' : 1101
University of Fujairah
BIT-Networking and Security Program
'U' : 010
-- Encoding/Decoding --
Encoded Text: 111010011110100111100110000101
Decoded Text: MULTIMEDIA
Arrange them as given in the following table
LETTER
M
U
L
T
I
M
E
D
I
A
FREQUNCY
2/10=0.2
1/10=
0.1
1/10=
0.1
1/10=
0.1
2/10=0.2
REPETITION
1/10=
0.1
1/10=
0.1
REPETITION
1/10=
0.1
CODE USED
111
010
011
1101
00
100
1100
101
Task 4:
Arranging the entries in order-
Write them without repetition, and in descending order of frequency
LETTER
I
M
E
U
L
A
T
D
FREQUNCY
2/10=0.2
2/10=0.2
1/10=
0.1
1/10=
0.1
1/10=
0.1
1/10=
0.1
1/10=
0.1
1/10=
0.1
CODE USED
00
111
100
010
011
101
1101
1100
Write your inference from the above table.
University of Fujairah
BIT-Networking and Security Program
Huffman.py
from heapq import heappush, heappop, heapify
from collections import defaultdict
class Node:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_huffman_tree(freq_map):
heap =[Node(char, freq) for char, freq in freq_map.items()]
heapify(heap)
while len(heap)>1:
left = heappop(heap)
right = heappop(heap)
merged = Node(None, left.freq + right.freq)
merged.left = left
merged.right = right
heappush(heap, merged)
University of Fujairah
BIT-Networking and Security Program
return heap[0]
def generate_codes(root, code='', codes={}):
if root is not None:
if root.char is not None:
codes[root.char]= code
generate_codes(root.left, code +'0', codes)
generate_codes(root.right, code +'1', codes)
def huffman_encoding(text):
freq_map = defaultdict(int)
for char in text:
freq_map[char]+=1
if len(freq_map)==1:
return {'0': text}
root = build_huffman_tree(freq_map)
codes ={}
generate_codes(root,'', codes)
encoded_text =''.join(codes[char] for char in text)
return codes, encoded_text
def huffman_decoding(codes, encoded_text):
decoded_text =''
reverse_codes ={v: k for k, v in codes.items()}
University of Fujairah
BIT-Networking and Security Program
code =''
for bit in encoded_text:
code += bit
if code in reverse_codes:
decoded_text += reverse_codes[code]
code =''
return decoded_text
def main():
text = input("Enter the text: ")
codes, encoded_text = huffman_encoding(text)
print("Encoded text:", encoded_text)
print("Frequency count and codes:")
for char, code in codes.items():
print(f"'{char}': {code}")
decoded_text = huffman_decoding(codes, encoded_text)
print("Decoded text:", deco

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!