Slide 2 Suplemen Huffman Coding
Slide 2 Suplemen Huffman Coding
Huffman Coding-
Pre x Rule-
SPONSORED SEARCHES
Huffman Tree-
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 1/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Step-01:
Step-02:
Step-03:
Step-04:
Keep repeating Step-02 and Step-03 until all the nodes form a single tree.
The tree finally obtained is the desired Huffman Tree.
Time Complexity-
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 2/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Important Formulas-
The following 2 formulas are important to solve the problems based on Huffman Coding-
Formula-01:
Formula-02:
= Total number of characters in the message x Average code length per character
Problem-
A file contains the following characters with the frequencies as shown. If Huffman Coding is used for data
compression, determine-
Characters Frequencies
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 3/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
a 10
e 15
i 12
o 3
u 4
s 13
t 1
Solution-
Step-01:
Step-02:
Step-03:
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 4/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Step-04:
Step-05:
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 5/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Step-06:
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 6/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Step-07:
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 7/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 8/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Now,
Rule
If you assign weight ‘0’ to the left edges, then assign weight ‘1’ to the right edges.
If you assign weight ‘1’ to the left edges, then assign weight ‘0’ to the right edges.
Any of the above two conventions may be followed.
But follow the same convention at the time of decoding that is adopted at the time of
encoding.
After assigning weight to all the edges, the modified Huffman Tree is-
Now, let us answer each part of the given problem one by one-
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 9/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
To write Huffman Code for any character, traverse the Huffman Tree from root node to the leaf node of
that character.
Following this rule, the Huffman Code for each character is-
a = 111
e = 10
i = 00
o = 11001
u = 1101
s = 01
t = 11000
Characters occurring less frequently in the text are assigned the larger code.
Characters occurring more frequently in the text are assigned the smaller code.
= 2.52
= Total number of characters in the message x Average code length per character
= 58 x 2.52
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 10/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
= 146.16
≅ 147 bits
Get more notes and other study material of Design and Analysis of Algorithms.
Summary
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 11/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
Publisher Logo
Liked this article? Share it with your friends and classmates now-
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 12/13
3/2/2020 Huffman Coding | Huffman Coding Example | Time Complexity | Gate Vidyalay
https://www.gatevidyalay.com/huffman-coding-huffman-encoding/ 13/13