0% found this document useful (0 votes)
27 views11 pages

Ds Lec46,47unit5

Uploaded by

time4edu95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Ds Lec46,47unit5

Uploaded by

time4edu95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Maharana Pratap Group of Institutions, Mandhana, Kanpur

(Approved By AICTE, New Delhi And Affiliated To AKTU, Luck now)

Digital Notes
[Department of Computer Science Engineering]

Course : [Link]
Branch : CS
Semester :3rd
Subject Name :DATA STRUCTURE
Subject Code :KCS301
Lecture No. /Topic :46,47/Huffman Algorithm
Prepared by : SNEHA KHORIA
1
© Copyright, Confidential, Maharana Pratap Group
Huffman Algorithm
• Huffman algorithm is a method for building an extended binary tree
with a minimum weighted path length from a set of given weights.

• This is a method for the construction of minimum redundancy codes.

• Applicable to many forms of data transmission

• multimedia codecs such as JPEG and MP3


Huffman Algorithm
• 1951, David Huffman found the “most efficient method of representing
numbers, letters, and other symbols using binary code”. Now standard
method used for data compression.
• In Huffman Algorithm, a set of nodes assigned with values if fed to the
algorithm. Initially 2 nodes are considered and their sum forms their
parent node.
• When a new element is considered, it can be added to the tree.
• Its value and the previously calculated sum of the tree are used to form the
new node which in turn becomes their parent.
Huffman Algorithm
• Let us take any four characters and their frequencies, and sort this list by
increasing frequency.
• Since to represent 4 characters the 2 bit is sufficient thus take initially
two bits for each character this is called fixed length character.
character frequencies sort Character frequencies code
E 10 A 3 00
T 7 O 5 01
O 5 T 7 10
A 3 E 10 11
• Here before using Huffman algorithm the total number of bits required is:
nb=3*2+5*2+7*2+10*2 =06+10+14+20 =50bits
Character frequencies code

A 3 110
O 5 111
T 7 10
E 10 0

Thus after using Huffman algorithm the total number of bits required
is
nb=3*3+5*3+7*2+10*1 =09+15+14+10 =48bits
i.e (50-48)/50*100%=4%
Since in this small example we save about 4% space by using Huffman algorithm. If we take large
Huffman Algorithm
• Lets say you have a set of numbers and their frequency of use and
want to create a huffman encoding for them
Value Frequencies
1 5
2 7
3 10
4 15
5 20
6 45
Huffman Algorithm
• Creating a Huffman tree is simple. Sort this list by frequency and make
the two-lowest elements into leaves, creating a parent node with a
frequency that is the sum of the two lower element's frequencies:
12:*
/ \
5:1 7:2
• The two elements are removed from the list and the new parent node,
with frequency 12, is inserted into the list by frequency. So now the
list, sorted by frequency, is:
10:3
12:*
15:4
20:5
45:6
Huffman Algorithm
• You then repeat the loop, combining the two lowest elements. This results in:
22:*
/ \
10:3 12:*
/ \
5:1 7:2
• The two elements are removed from the list and the new parent node, with
frequency 12, is inserted into the list by frequency. So now the list, sorted by
frequency, is:
15:4
20:5
22: *
Huffman Algorithm
Value C D E K L M U Z
Frequency 32 42 120 7 42 24 37 2

After
sorted

Value Z K M C L D U E
Frequency 2 7 24 32 42 42 37 120

We can represent it using 3 bit

You might also like