LAB_6
LAB_6
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string>
return minHeap.top();
}
if (root->data != '$') {
codes[root->data] = code;
}
return decodedMessage;
}
int main() {
string message = "netal net ten";
// Encode message
string encodedMessage = encodeMessage(message, codes);
// Decode message
string decodedMessage = decodeMessage(encodedMessage, root);
Output:
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
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 .
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).