4.6 Huffman Coding, Optimal Merge Pattern
4.6 Huffman Coding, Optimal Merge Pattern
UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Design and Analysis of Algorithms(CSH-311/IT
H-311)
Outcome:
• Student will understand
Huffman coding
Optimal merge pattern
Huffman Coding
Example
PROBLEM:
A file contains the following characters with the frequencies
as shown. If Huffman Coding is used for data compression,
determine-
1) Huffman Code for each character
2) Average code length
3) Length of Huffman encoded
message (in bits)
Solution
Now,
•We assign weight to all the edges of the constructed
Huffman Tree.
•Let us assign weight ‘0’ to the left edges and weight ‘1’ to
the right edges.
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.
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
Algorithm
Example
REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
Websites:
•https://www.geeksforgeeks.org/huffman-coding-greedy-algo-3/
•https://www.programiz.com/dsa/huffman-coding
•https://www.tutorialspoint.com/design_and_analysis_of_algorithms/
design_and_analysis_of_algorithms_optimal_merge_pattern.htm#:~:text=Merge
%20a%20set%20of%20sorted,into%20a%20single%20sorted%20file.
Summary
• Huffman Coding
• Optimal Merge Pattern