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

Project Report Huffman Algorithm: Jinnah University For Women

The document describes the Huffman coding algorithm in two steps: 1) Building a Huffman tree from character frequencies where the least frequent characters have highest priority. This forms a binary tree with characters as leaves and internal nodes representing character combinations. 2) Traversing the tree from root to leaves and assigning variable length binary codes to each character, where left branches are 0 and right are 1. The codes are stored in an auxiliary array. Common characters have shorter codes for improved compression.

Uploaded by

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

Project Report Huffman Algorithm: Jinnah University For Women

The document describes the Huffman coding algorithm in two steps: 1) Building a Huffman tree from character frequencies where the least frequent characters have highest priority. This forms a binary tree with characters as leaves and internal nodes representing character combinations. 2) Traversing the tree from root to leaves and assigning variable length binary codes to each character, where left branches are 0 and right are 1. The codes are stored in an auxiliary array. Common characters have shorter codes for improved compression.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING

JINNAH UNIVERSITY FOR WOMEN

PROJECT REPORT
HUFFMAN ALGORITHM
Data Structure and Algorithm
BS (SE) FALL 2018

Group Member(s)

Aqsa Noor 2018/COMP/BS (SE)/1702

Roshanay Khan 2018/COMP/BS (SE)/1704

Supervisor Name: Ms. Tehreem Hussain

1
ACKNOWLEDGEMENT:

I take this occasion to thank Allah almighty for blessing us with his grace. I extend my sincere and heartfelt
thanks to our esteemed guide my teachers for providing me with the right guidance and advice at the
crucial junctures and for showing me the right way. Last but not the least, I would like to thank my friends
and family for the support and encouragement they have given me during the course of our work. This
project work in itself is an acknowledgement to the desperation, sincerity, passion and technical
assistance contributed to it by all our group members and our talented teachers. With immense gratitude,
we acknowledge our indebtedness to all the group members whose support and guidance have helped us
in carrying out this project work dynamically and up to the standards of our prestigious institution. Finally,
thank to our beloved friend that always stick together and work hard to produce a good project with all
afford and responsibility. Hope that all the effort will give many benefits to us and to our group project.

2
Table Of Contents
ACKNOWLEDGEMENT: ............................................................................................................................ 2
INTRODUCTION: ...................................................................................................................................... 4
REAL-LIFE APPLICATONS: ......................................................................................................................... 4
Step-1 HUFFMAN TREE: ....................................................................................................................... 4
ALGORITHM-1 ......................................................................................................................................... 5
STEP-2 ASSIGNING CODES: .................................................................................................................. 7
ALGORITHM-2 ......................................................................................................................................... 8
CODING: .................................................................................................................................................. 9
OUTPUT............................................................................................................................................... 9
COMPLEXITY.......................................................................................................................................... 10
CONCLUSION:........................................................................................................................................ 11

3
INTRODUCTION:
HUFFMAN CODING
Huffman Coding also called, as Huffman Encoding is a famous greedy algorithm that is used for the
lossless compression of data. 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 major steps in Huffman Coding


1) Build a Huffman Tree from input characters.
2) Traverse the Huffman Tree and assign codes to characters.
Although having two steps we will have two algorithms.

REAL-LIFE APPLICATONS:
 Huffman is widely used in all the mainstream compression formats that you might encounter -
from GZIP, PKZIP (WinZip etc.) and BZIP2, to image formats such as JPEG and PNG.
 ZIP is perhaps the most widely used compression tool that uses Huffman Encoding as its basis.

Step-1 HUFFMAN TREE:


This technique works by creating a binary tree of nodes.

1- Building a Huffman tree from the input characters.

5 13
4
ALGORITHM-1
 Algorithm for creating a Huffman tree

STEP1 Create a leaf node for each unique character and build a min heap of all
leaf nodes (Min Heap is used as a priority queue. The value of frequency field
is used to compare two nodes in min heap. Initially, the least frequent character
is at root).
STEP2. Extract two nodes with the minimum frequency from the min heap.
STEP3. Create a new internal node with a frequency equal to the sum of the
two nodes frequencies. Make the first extracted node as its left child and the
other extracted node as its right child. Add this node to the min heap.
STEP4. Repeat steps#2 and #3 until the heap contains only one node. The
remaining node is the root node and the tree is complete.
STEP4. Exit

 Example of a Huffman Tree

Characters Frequencies
a 5

b 9

c 12

d 13

e 16

f 45

1-
2-

5
3- 4-

5-

Hence a Huffman tree is formed,

6
STEP-2 ASSIGNING CODES:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to the left
child, write 0 to the array. While moving to the right child, write 1 to the array. Print the array when a leaf
node is encountered.

Characters Frequencies Code


a 5 1100
b 9 1101

c 12 100

d 13 101

e 16 111

f 45 0

7
ALGORITHM-2
Start
1- Define a node with character, frequency, left and right child of the node for Huffman tree.
2- Create a list ‘freq’ to store frequency of each character, initially, all are 0.
3- For each character c in the string do increase the frequency for character Ch. in freq list.

4- for all type of character (Ch.) process


- If the frequency of Ch. is non zero then
- Add Ch. and its frequency as a node of priority queue Q.

5-while Q is not empty do


6- Remove item from Q and assign it to left child of node
-Remove item from Q and assign to the right child of node
7- Traverse the node to find the assigned code

End

8
CODING:

OUTPUT

9
COMPLEXITY
The time complexity of the Huffman algorithm is O(nlogn) . Using a heap to store the weight of each
tree, each iteration requires O(logn) time to determine the cheapest weight and insert the new weight.
There are O(n) iterations, one for each item.

10
CONCLUSION:
Huffman in very beneficial can also be used in image compression and it has benefits of they are space
efficient given some corpus. They are prefix codes.

11

You might also like