0% found this document useful (0 votes)
6 views9 pages

Huffman Coding

The document presents Huffman coding as a lossless data compression algorithm that uses variable-length prefix codes based on character frequencies. It details the algorithm's implementation, including the steps to build a Huffman tree and provides a Python implementation example. The conclusion emphasizes the efficiency and practicality of Huffman coding in data compression.

Uploaded by

girlsmiley144
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)
6 views9 pages

Huffman Coding

The document presents Huffman coding as a lossless data compression algorithm that uses variable-length prefix codes based on character frequencies. It details the algorithm's implementation, including the steps to build a Huffman tree and provides a Python implementation example. The conclusion emphasizes the efficiency and practicality of Huffman coding in data compression.

Uploaded by

girlsmiley144
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
You are on page 1/ 9

SREE SARASWATHI THYAGARAJA COLLEGE OF ARTS AND

SCIENCE
POLLACHI

PG-Department of Mathematics

Huffman Coding: A Greedy Algorithm for Data Compression

Guided By Presenter

Dr.O.V.Shanmuga Sundaran Seran Easter Rani.A


Head Of The Department-PG Mathematics
Huffman Coding: A
Greedy Algorithm for
Data Compression
This presentation explores Huffman coding's theoretical foundation. It
covers algorithmic implementations and practical applications in text file
compression. The aim is to showcase greedy algorithms in optimizing data
structures and improving computational efficiency.
Understanding Huffman Coding
Huffman coding is a lossless data compression algorithm. It assigns variable-length codes to input characters. Code lengths are based
on character frequencies. These codes are prefix codes, ensuring no ambiguity during decoding.

Lossless Variable-Length Codes Prefix Codes


No data is lost during compression. Characters get shorter or longer Ensures unambiguous decoding.
codes.
Huffman Algorithm
The Huffman coding algorithm builds a tree in a bottom-up manner. The tree, denoted as T,
requires |c| - 1 merge operations. Q represents the priority queue used during binary heap
construction.

Algorithm Huffman (c)


{
n= |c|
Q=c
for i<-1 to n-1
do
{
temp <- get node ()
left (temp] Get_min (Q) right [temp] Get Min (Q)
a = left [templ b = right [temp]
F [temp]<- f[a] + [b]
insert (Q, temp)
}
return Get_min (0)
}
Steps to Build Huffman Tree
The input is an array of unique characters with their frequencies. The output is a Huffman Tree. A min heap of leaf nodes is built, then two nodes
with minimum frequency are extracted.

1 Create Leaf Nodes 2 Extract Minimum Nodes


For each unique character. From the min heap.

3 Create Internal Node 4 Repeat


Sum of the two nodes frequencies. Until the heap contains only one node.
Example: "MATHEMATICIANS"
Consider the word "MATHEMATICIANS". The characters are the letters in the word. The
frequency is the number of times each character appears.

CHARACTERS FREQUENCY

M 2

A 3

T 2

H 1

E 1

I 2

C 1

N 1

S 1
Huffman Codes for
"MATHEMATICIANS"
Based on the Huffman Tree, the codes for each character are generated. Each character is assigned
a unique binary code.

N = 000

T=001

A = 01

M = 100

H = 1010

C = 1011

I = 110

S = 1110

E = 1111
Python Implementation
Below is the implementation of the Huffman coding approach in Python. The code defines a node class and uses a priority queue to build the Huffman tree.

# A Huffman Tree Node


import heapq
class node:
def __init__(self, freq, symbol, left=None, right=None):
# frequency of symbol
self.freq = freq
# symbol name (character)
self.symbol = symbol
# node left of current node
self.left = left
# node right of current node
self.right = right
# tree direction (0/1)
self.huff = ''
def __lt__(self, nxt):
return self.freq < nxt.freq
# utility function to print huffman
# codes for all symbols in the newly
# created Huffman tree
def printNodes(node, val=''):
# huffman code for current node
newVal = val + str(node.huff)
# if node is not an edge node
# then traverse inside it
if(node.left):
printNodes(node.left, newVal)
if(node.right):
printNodes(node.right, newVal)
# if node is edge node then
# display its huffman code
if(not node.left and not node.right):
print(f"{node.symbol} -> {newVal}")
# characters for huffman tree
chars = ['m','a','t','h','e','i','c','n','s']
freq = [2,3,2,1,1,2,1,1,1]
# list containing unused nodes
nodes = []
Conclusion
Huffman coding is a greedy algorithm that efficiently compresses data. It
assigns variable-length codes based on frequency. The Python
implementation demonstrates the practical application of this algorithm.

Efficient

Practical

Lossless

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy