Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Huffman Coding:

Huffman Coding is a technique of compressing data to reduce its size without


losing any of the details. It was first developed by David Huffman.

Huffman Coding is generally useful to compress the data in which there are
frequently occurring characters.

There are two ways we can follow to reduce message using Huffman coding
and those are:

 Fixed Length Code


 Variable Length Code

Fixed Length Code:

Suppose our message is: B B B C A A A D E E E A B B B A C C D A

Here we have total 20 characters. We know that, all the characters are
represented with their ASCII codes which are 8bit code.

So, 20*8=160 bits total size for given message

If we take a close look to our message then we can see we only have 5 unique
characters on that message. So, we don’t need 8 bit ASCII to represent those
characters. We can easily represent those characters by using only 3 bit code
(23 =8) which is 0-7. 8 bit character is used to represent all 256 characters.

Character Frequency Code

A 6 000

B 6 001

C 3 010

D 2 011

E 3 100
Now, our message size is 20*3=60 bits

Now, we need to find the size of our new table

Character Frequency Code

A 6 000

B 6 001

C 3 010

D 2 011

E 3 100

5*8=40 3*5=15

40+15=55

Now, our message size is 20*3=60 bits + table size is 55 bits =115 bits

So, we easily reduce the size of our message by the help of fixed length code.
Variable Length Code:

Huffman code technique tells us that, we can reduce message size more by
using variable length code.

Character Frequency Code

A 6

B 6

C 3

D 2

E 3

To do this we need to place the characters in ascending order according to


their frequency and then construct a tree from those characters by adding the
minimum two nodes at a same time where we need to ensure that left child of
that tree will be always smaller than the right child of that tree.
Character Frequency Code

A 6 11

B 6 10

C 3 00

D 2 010

E 3 011

Code is now easily calculated from decision tree. We can understand that
codes are variable length. Character which has higher frequency has lower
code in bits and character with lower frequency is higher in bits.

Character Frequency Code Frequency*Code

A 6 11 6*2=12

B 6 10 6*2=12

C 3 00 3*2=6

D 2 010 2*3=6

E 3 011 3*3=9

5*8=40 12 bits 45 bits


bits

This can be easily decoded from tree.

You might also like