Huffman Coding
Huffman Coding
3
Huffman Coding
• Let us understand prefix codes with a counter example. Let there be four
characters a, b, c and d, and their corresponding variable length codes be
00, 01, 0 and 1. This coding leads to ambiguity because code assigned to c
is the prefix of codes assigned to a and b. If the compressed bit stream is
0001, the de-compressed output may be “cccd” or “ccb” or “acd” or “ab”.
• On the other hand 00, 01, 10, 11 are the prefix code that can be assigned to
a, b, c and d respectively.
4
Major Steps in Huffman Coding
• Step 1. Build a min heap that contains 5 nodes where each node represents
root of a tree with single node.
6
Huffman Coding Example
• Extract two minimum frequency nodes from min heap. Add a new internal
node with frequency 3 + 5 = 8.
7
Huffman Coding Example
• Now min heap contains 4 nodes where 3 nodes are roots of trees with
single element each, and one heap node is root of tree with 3 elements
Character Frequency
c 6
b 7
Internal Node 8
e 9
8
Huffman Coding Example
• Extract two minimum frequency nodes from min heap. Add a new internal
node with frequency 6 + 7 = 13.
9
Huffman Coding Example
• Now min heap contains 3 nodes where 1 nodes are roots of trees with
single elements, and two heap node is root of tree with 3 elements
Character Frequency
Internal Node 8
e 9
Internal Node 13
10
Huffman Coding Example
• Extract two minimum frequency nodes from min heap. Add a new internal
node with frequency 8 + 9 = 17.
11
Huffman Coding Example
Character Frequency
Internal Node 13
Internal Node 17
12
Huffman Coding Example
• Extract two minimum frequency nodes from min heap. Add a new internal
node with frequency 13 + 17 = 30.
13
Huffman Coding Example
Internal Node 30
• Since only one node is remaining in min heap, so algorithm will stop here.
14
Steps to print codes from Huffman Tree
16
Steps to print codes from Huffman Tree
Character Frequency Code
e 9 11
b 7 01
c 6 00
d 5 101
a 3 100
• As you can see that the most frequent character gets the least number of
bits.
17
Algorithm of Huffman Coding
Algo:
• n = |c|
• Q=c
• for i = 1 to n-1
• Allocate a new node z
• z.left = x = Extract-min(Q)
• z.right = y Extract-min(Q)
• z.freq = x.freq + y.freq
• Insert(Q, z)
• return Extract-min(Q) //return the root node of the tree
19
Time Complexity
• Building Huffman Tree: The time complexity of building the Huffman tree depends on
method used to construct it. Using a priority queue to merge nodes has a time
complexity of the O(N log N) where N is the number of the unique characters in the
input.
• Encoding: Once the Huffman tree is constructed encoding a message involves traversing
the tree to find the code for each character. This traversal has a time complexity of O(N)
where n is the length of input message.
• Decoding: Decoding a Huffman-encoded message also requires traversing the Huffman
tree. This traversal has a time complexity of O(N) where N is the number of the bits in
the encoded message.
Sample Footer Text 20
Application of Huffman Coding
• Image compression: By compressing image data, Huffman coding can reduce
the amount of storage space required for digital images.
• Audio compression: Similar to image compression, Huffman coding can also be
used to compress audio data. This can save storage space and bandwidth when
streaming or downloading audio files.
• Text compression: Text files can also be compressed using Huffman coding.
This can be useful for reducing the size of text documents or email attachments.
• Data transmission: When data is transmitted over a network, it is often
compressed using Huffman coding to reduce the amount of bandwidth required.
This can help to speed up data transfer and reduce costs associated with
transmitting data. 21
Thank
you!
Sample Footer Text 22