0% found this document useful (0 votes)
26 views

Huffman Coding

Uploaded by

sabbirsafi08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Huffman Coding

Uploaded by

sabbirsafi08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Huffman coding

Huffman coding is a lossless data compression algorithm. In this algorithm, a variable-length


code is assigned to input different characters. The code length is related to how frequently
characters are used. Most frequent characters have the smallest codes and longer codes for least
frequent characters.

There are mainly two parts. First one to create a Huffman tree, and another one to traverse the
tree to find codes.

There are mainly two major parts in Huffman Coding


1) Build a Huffman Tree from input characters.
2) Traverse the Huffman Tree and assign codes to characters.

For an example, consider some strings “YYYZXXYYX”, the frequency of character Y is larger
than X and the character Z has the least frequency. So the length of the code for Y is smaller than
X, and code for X will be smaller than Z.

Charecteristics of Huffman Coding

 Huffman Coding is a famous Greedy Algorithm.


 It is used for the lossless compression of data.
 It uses variable length encoding.
 It assigns variable length code to all the characters.
 The code length of a character depends on how frequently it occurs in the given text.
 The character which occurs most frequently gets the smallest code.
 The character which occurs least frequently gets the largest code.

Prefix Rule - no code is prefix to another code

 Huffman Coding implements a rule known as a prefix rule.


 This is to prevent the ambiguities while decoding.
 It ensures that the code assigned to any character is not a prefix of the code assigned to
any other character.

Application of Huffman coding:

1.Huffman coding is use in data compression.

2.A rather different use of Huffman encoding is in conjunction with cryptography.

Example:ABBCDBCCDAABBEEEBEAB
Problem

A file contains the following characters with the frequencies as shown. If Huffman Coding is
used for data compression. find the Huffman Code for each character .

Characters Frequencies

a 10

e 15

i 12

o 3

u 4

s 13

t 1

Step-01:

Step-02:
Step-03:

Step-04:
Step-05:

Step-06:
Step-07:

1. Huffman Code For Characters-

Following this rule, the Huffman Code for each character is-

 a = 111
 e = 10
 i = 00
 o = 11001
 u = 1101
 s = 01
 t = 11000

You might also like