0% found this document useful (0 votes)
7 views

LAB_6

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

LAB_6

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment Number: 6

NAME: Md Muaz Sayyed ROLLNO: 60


CLASS: TY-IT-A BATCH: 3
___________________________________________________________________________
Problem Statement:
Write a program that uses either an actual message or frequency of
appearance of individual characters from the message to build Huffman
coding tree. Write a text msg and encode and decode it using
constructed Huffman coding tree.
Answer:
Pseudo Code:
Algorithm Huffman (c)
{
n= |c|
Q=c
for i<-1 to n-1
do
{
temp <- get node ()
left (temp] Get_min (Q) right [temp] Get Min (Q)
a = left [templ b = right [temp]
F [temp]<- f[a] + [b]
insert (Q, temp)
}
return Get_min (0)
}
Code:

#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string>

using namespace std;

// Huffman tree node


struct HuffmanNode {
char data;
int frequency;
HuffmanNode *left, *right;

HuffmanNode(char data, int frequency) {


this->data = data;
this->frequency = frequency;
left = right = nullptr;
}
};

// Comparator for priority queue


struct compare {
bool operator()(HuffmanNode* l, HuffmanNode* r) {
return (l->frequency > r->frequency);
}
};

// Function to build Huffman tree


HuffmanNode* buildHuffmanTree(const unordered_map<char, int>& freqMap)
{
priority_queue<HuffmanNode*, vector<HuffmanNode*>, compare>
minHeap;

// Create leaf nodes and add them to the priority queue


for (auto& pair : freqMap) {
HuffmanNode* node = new HuffmanNode(pair.first, pair.second);
minHeap.push(node);
}

// Build Huffman tree


while (minHeap.size() != 1) {
HuffmanNode *left = minHeap.top(); minHeap.pop();
HuffmanNode *right = minHeap.top(); minHeap.pop();

HuffmanNode* newNode = new HuffmanNode('$', left->frequency +


right->frequency);
newNode->left = left;
newNode->right = right;
minHeap.push(newNode);
}

return minHeap.top();
}

// Function to generate Huffman codes for each character


void generateHuffmanCodes(HuffmanNode* root, string code,
unordered_map<char, string>& codes) {
if (!root) return;

if (root->data != '$') {
codes[root->data] = code;
}

generateHuffmanCodes(root->left, code + "0", codes);


generateHuffmanCodes(root->right, code + "1", codes);
}

// Function to encode a message using Huffman codes


string encodeMessage(const string& message, const unordered_map<char,
string>& codes) {
string encodedMessage = "";
for (char c : message) {
encodedMessage += codes.at(c);
}
return encodedMessage;
}

// Function to decode a message using Huffman tree


string decodeMessage(const string& encodedMessage, HuffmanNode* root) {
string decodedMessage = "";
HuffmanNode* curr = root;

for (char bit : encodedMessage) {


if (bit == '0') {
curr = curr->left;
} else {
curr = curr->right;
}

if (curr->left == nullptr && curr->right == nullptr) {


decodedMessage += curr->data;
curr = root;
}
}

return decodedMessage;
}

// Function to print Huffman tree in a proper manner


void printHuffmanTree(HuffmanNode* root, int level = 0, char branch =
'-') {
if (root) {
printHuffmanTree(root->right, level + 1, '/');
cout << string(level * 6, ' ');
if (branch != '-') {
cout << branch << "----";
}
cout << root->frequency;
if (root->data != '$') {
cout << ":" << root->data;
}
cout << endl;
printHuffmanTree(root->left, level + 1, '\\');
}
}

int main() {
string message = "netal net ten";

// Calculate frequency of each character in the message


unordered_map<char, int> freqMap;
for (char c : message) {
freqMap[c]++;
}

// Build Huffman tree


HuffmanNode* root = buildHuffmanTree(freqMap);

// Generate Huffman codes


unordered_map<char, string> codes;
generateHuffmanCodes(root, "", codes);

// Encode message
string encodedMessage = encodeMessage(message, codes);

// Display Huffman codes


cout << "Huffman Codes:\n";
for (auto& pair : codes) {
cout << pair.first << " : " << pair.second << endl;
}

// Display Huffman tree


cout << "\nHuffman Tree (Preorder Traversal):\n";
printHuffmanTree(root);

// Decode message
string decodedMessage = decodeMessage(encodedMessage, root);

// Display original, encoded, and decoded message


cout << "\nOriginal Message: " << message << endl;
cout << "Encoded Message: " << encodedMessage << endl;
cout << "Decoded Message: " << decodedMessage << endl;
return 0;
}

Output:

Word : netal net ten

Symbol Probability Table:

Symbol Frequency Code Probability


n 3 10 3/13
e 3 00 3/13
t 3 01 3/13
a 1 1111 1/13
l 1 1110 1/13
Space 2 110 2/13

Entropy
Entropy (H) = − ∑ ( p i log2 ( pi ) ) where p i is the probability of symbol i
i

Entropy (H) =


( 3
13
log 2( )
3 3
+ log 2
13 15
3 3
+ log 2
13 13 ( )
3 1
+ log 2
13 13 ( )
1 1
+ log 2
13 13
1 2
( )
+ log 2
13 13
2
13 ( ) ( ))
H ≈ 1.067 bits/ symbol
Compression ratio

Original message size: 13 characters * 8 bits/character = 104 bits

Encoded message size: 32 bits

Compression Ratio = Original size / Encoded size = 104 / 32 ≈ 3.25

Average Length :
L=∑ ( p i l i )
i

3 3 3 1 1 1
L= ( 2 ) + ( 2 )+ ( 2 )+ ( 4 )+ ( 4 )+ (3 ) L=2.23 bits /symbol
13 13 13 13 13 13
Efficiency (η)
Efficiency is defined as the ratio of the entropy of the original message to the average codeword length in the
encoded message. Mathematically, it can be expressed as:

H original
Efficiency ( η )=
L
−H original is the entropy of the original message .

−L isthe average codeword length∈the encoded message .


Calculating Efficiency (η)
Now , we can substitute the values of H original ∧Linto the efficiency formula :

1.067
η= ≈ 0.478=47.8%
2.23
This indicates that the efficiency of the provided Huffman coding scheme for the message is approximately
0.478 meaning it is slightly more efficient than the original message's entropy alone.

Time Complexity : For a n- character message, we need to select two nodes with minimum
value (n-1) times and let log n be the height of the binary tree (hosting min heap), then the
total time is
(n-1)* log n, thus O(n log n).

You might also like