Project Report Huffman Algorithm: Jinnah University For Women
Project Report Huffman Algorithm: Jinnah University For Women
PROJECT REPORT
HUFFMAN ALGORITHM
Data Structure and Algorithm
BS (SE) FALL 2018
Group Member(s)
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.
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.
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
Characters Frequencies
a 5
b 9
c 12
d 13
e 16
f 45
1-
2-
5
3- 4-
5-
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.
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.
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