SlideShare a Scribd company logo
Compression & Huffman Codes
Compression
Definition
Reduce size of data
(number of bits needed to represent data)
Benefits
Reduce storage needed
Reduce transmission cost / latency / bandwidth
Sources of Compressibility
Redundancy
Recognize repeating patterns
Exploit using
Dictionary
Variable length encoding
Human perception
Less sensitive to some information
Can discard less important data
Types of Compression
Lossless
Preserves all information
Exploits redundancy in data
Applied to general data
Lossy
May lose some information
Exploits redundancy & human perception
Applied to audio, image, video
Effectiveness of Compression
Metrics
Bits per byte (8 bits)
2 bits / byte  ¼ original size
8 bits / byte  no compression
Percentage
75% compression  ¼ original size
Effectiveness of Compression
Depends on data
Random data  hard
Example: 1001110100  ?
Organized data  easy
Example: 1111111111  110
Corollary
No universally best compression algorithm
Effectiveness of Compression
Lossless Compression is not always possible
If compression is always possible (alternative view)
Compress file (reduce size by 1 bit)
Recompress output
Repeat (until we can store data with 0 bits)
Lossless Compression Techniques
LZW (Lempel-Ziv-Welch) compression
Build pattern dictionary
Replace patterns with index into dictionary
Run length encoding
Find & compress repetitive sequences
Huffman codes
Use variable length codes based on frequency
Huffman Code
Approach
Variable length encoding of symbols
Exploit statistical frequency of symbols
Efficient when symbol probabilities vary widely
Principle
Use fewer bits to represent frequent symbols
Use more bits to represent infrequent symbols
A A B A
A A
A B
Huffman Code Example
Expected size
Original  1/82 + 1/42 + 1/22 + 1/82 = 2 bits / symbol
Huffman  1/83 + 1/42 + 1/21 + 1/83 = 1.75 bits / symbol
Symbol A B C D
Frequency 13% 25% 50% 12%
Original
Encoding
00 01 10 11
2 bits 2 bits 2 bits 2 bits
Huffman
Encoding
110 10 0 111
3 bits 2 bits 1 bit 3 bits
Huffman Code Data Structures
Binary (Huffman) tree
Represents Huffman code
Edge  code (0 or 1)
Leaf  symbol
Path to leaf  encoding
Example
A = “110”, B = “10”, C = “0”
Priority queue
To efficiently build binary tree 1
1 0
0
D
C
B
A
0
1
Huffman Code Algorithm Overview
Encoding
Calculate frequency of symbols in file
Create binary tree representing “best” encoding
Use binary tree to encode compressed file
For each symbol, output path from root to leaf
Size of encoding = length of path
Save binary tree
Huffman Code – Creating Tree
Algorithm
Place each symbol in leaf
Weight of leaf = symbol frequency
Select two trees L and R (initially leafs)
Such that L, R have lowest frequencies in tree
Create new (internal) node
Left child  L
Right child  R
New frequency  frequency( L ) + frequency( R )
Repeat until all nodes merged into one tree
Huffman Tree Construction 1
3 5 8 2 7
A C E H I
Huffman Tree Construction 2
3 5 8
2 7
5
A C E
H I
Huffman Tree Construction 3
3
5
8
2
7
5
10
A
C
E
H
I
Huffman Tree Construction 4
3
5
8
2
7
5
10
15
A
C
E
H
I
Huffman Tree Construction 5
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
E = 01
I = 00
C = 10
A = 111
H = 110
Huffman Coding Example
Huffman code
Input
ACE
Output
(111)(10)(01) = 1111001
E = 01
I = 00
C = 10
A = 111
H = 110
Huffman Code Algorithm Overview
Decoding
Read compressed file & binary tree
Use binary tree to decode file
Follow path from root to leaf
Huffman Decoding 1
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
Huffman Decoding 2
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
Huffman Decoding 3
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
A
Huffman Decoding 4
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
A
Huffman Decoding 5
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
AC
Huffman Decoding 6
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
AC
Huffman Decoding 7
3
5 8
2
7
5
10 15
25
1
1
1
1
0
0
0
0
A
C E
H
I
1111001
ACE
Huffman Code Properties
Prefix code
No code is a prefix of another code
Example
Huffman(“I”)  00
Huffman(“X”)  001 // not legal prefix code
Can stop as soon as complete code found
No need for end-of-code marker
Nondeterministic
Multiple Huffman coding possible for same input
If more than two trees with same minimal weight
Huffman Code Properties
Greedy algorithm
Chooses best local solution at each step
Combines 2 trees with lowest frequency
Still yields overall best solution
Optimal prefix code
Based on statistical frequency
Better compression possible (depends on data)
Using other approaches (e.g., pattern dictionary)

More Related Content

Similar to compression & huffman coder problem .ppt (20)

PPT
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
Helan4
 
PDF
Arithmetic Coding
anithabalaprabhu
 
PPT
Chapter%202%20 %20 Text%20compression(2)
nes
 
PPTX
Chapter 4 Lossless Compression Algorithims.pptx
MedinaBedru
 
PPT
Data Compression
Shubham Bammi
 
PPT
Hufman coding basic
radthees
 
PPTX
Farhana shaikh webinar_huffman coding
Farhana Shaikh
 
PPT
Speaker Segmentation (2006)
Luís Gustavo Martins
 
PPT
Data compression
Abhishek Grover
 
PDF
Sunzip user tool for data reduction using huffman algorithm
Dr Sandeep Kumar Poonia
 
PPTX
Image compression
Bassam Kanber
 
PDF
Introduction Data Compression/ Data compression, modelling and coding,Image C...
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
PPTX
Data representation
ChingTing
 
PPT
Noise infotheory1
vmspraneeth
 
PPT
Noise info theory and Entrophy
Izah Asmadi
 
PPT
lec4.ppt system calls explained in detail
frp60658
 
PPT
Computer Concepts
Phoebe Kim
 
PPTX
Data representation
Travis Leong Ping
 
PPTX
Data representation
Travis Leong Ping
 
PDF
Module-IV 094.pdf
SamrajECE
 
2.3 unit-ii-text-compression-a-outline-compression-techniques-run-length-codi...
Helan4
 
Arithmetic Coding
anithabalaprabhu
 
Chapter%202%20 %20 Text%20compression(2)
nes
 
Chapter 4 Lossless Compression Algorithims.pptx
MedinaBedru
 
Data Compression
Shubham Bammi
 
Hufman coding basic
radthees
 
Farhana shaikh webinar_huffman coding
Farhana Shaikh
 
Speaker Segmentation (2006)
Luís Gustavo Martins
 
Data compression
Abhishek Grover
 
Sunzip user tool for data reduction using huffman algorithm
Dr Sandeep Kumar Poonia
 
Image compression
Bassam Kanber
 
Introduction Data Compression/ Data compression, modelling and coding,Image C...
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Data representation
ChingTing
 
Noise infotheory1
vmspraneeth
 
Noise info theory and Entrophy
Izah Asmadi
 
lec4.ppt system calls explained in detail
frp60658
 
Computer Concepts
Phoebe Kim
 
Data representation
Travis Leong Ping
 
Data representation
Travis Leong Ping
 
Module-IV 094.pdf
SamrajECE
 

Recently uploaded (20)

PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Tally software_Introduction_Presentation
AditiBansal54083
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Ad

compression & huffman coder problem .ppt

  • 2. Compression Definition Reduce size of data (number of bits needed to represent data) Benefits Reduce storage needed Reduce transmission cost / latency / bandwidth
  • 3. Sources of Compressibility Redundancy Recognize repeating patterns Exploit using Dictionary Variable length encoding Human perception Less sensitive to some information Can discard less important data
  • 4. Types of Compression Lossless Preserves all information Exploits redundancy in data Applied to general data Lossy May lose some information Exploits redundancy & human perception Applied to audio, image, video
  • 5. Effectiveness of Compression Metrics Bits per byte (8 bits) 2 bits / byte  ¼ original size 8 bits / byte  no compression Percentage 75% compression  ¼ original size
  • 6. Effectiveness of Compression Depends on data Random data  hard Example: 1001110100  ? Organized data  easy Example: 1111111111  110 Corollary No universally best compression algorithm
  • 7. Effectiveness of Compression Lossless Compression is not always possible If compression is always possible (alternative view) Compress file (reduce size by 1 bit) Recompress output Repeat (until we can store data with 0 bits)
  • 8. Lossless Compression Techniques LZW (Lempel-Ziv-Welch) compression Build pattern dictionary Replace patterns with index into dictionary Run length encoding Find & compress repetitive sequences Huffman codes Use variable length codes based on frequency
  • 9. Huffman Code Approach Variable length encoding of symbols Exploit statistical frequency of symbols Efficient when symbol probabilities vary widely Principle Use fewer bits to represent frequent symbols Use more bits to represent infrequent symbols A A B A A A A B
  • 10. Huffman Code Example Expected size Original  1/82 + 1/42 + 1/22 + 1/82 = 2 bits / symbol Huffman  1/83 + 1/42 + 1/21 + 1/83 = 1.75 bits / symbol Symbol A B C D Frequency 13% 25% 50% 12% Original Encoding 00 01 10 11 2 bits 2 bits 2 bits 2 bits Huffman Encoding 110 10 0 111 3 bits 2 bits 1 bit 3 bits
  • 11. Huffman Code Data Structures Binary (Huffman) tree Represents Huffman code Edge  code (0 or 1) Leaf  symbol Path to leaf  encoding Example A = “110”, B = “10”, C = “0” Priority queue To efficiently build binary tree 1 1 0 0 D C B A 0 1
  • 12. Huffman Code Algorithm Overview Encoding Calculate frequency of symbols in file Create binary tree representing “best” encoding Use binary tree to encode compressed file For each symbol, output path from root to leaf Size of encoding = length of path Save binary tree
  • 13. Huffman Code – Creating Tree Algorithm Place each symbol in leaf Weight of leaf = symbol frequency Select two trees L and R (initially leafs) Such that L, R have lowest frequencies in tree Create new (internal) node Left child  L Right child  R New frequency  frequency( L ) + frequency( R ) Repeat until all nodes merged into one tree
  • 14. Huffman Tree Construction 1 3 5 8 2 7 A C E H I
  • 15. Huffman Tree Construction 2 3 5 8 2 7 5 A C E H I
  • 16. Huffman Tree Construction 3 3 5 8 2 7 5 10 A C E H I
  • 17. Huffman Tree Construction 4 3 5 8 2 7 5 10 15 A C E H I
  • 18. Huffman Tree Construction 5 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I E = 01 I = 00 C = 10 A = 111 H = 110
  • 19. Huffman Coding Example Huffman code Input ACE Output (111)(10)(01) = 1111001 E = 01 I = 00 C = 10 A = 111 H = 110
  • 20. Huffman Code Algorithm Overview Decoding Read compressed file & binary tree Use binary tree to decode file Follow path from root to leaf
  • 21. Huffman Decoding 1 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001
  • 22. Huffman Decoding 2 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001
  • 23. Huffman Decoding 3 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 A
  • 24. Huffman Decoding 4 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 A
  • 25. Huffman Decoding 5 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 AC
  • 26. Huffman Decoding 6 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 AC
  • 27. Huffman Decoding 7 3 5 8 2 7 5 10 15 25 1 1 1 1 0 0 0 0 A C E H I 1111001 ACE
  • 28. Huffman Code Properties Prefix code No code is a prefix of another code Example Huffman(“I”)  00 Huffman(“X”)  001 // not legal prefix code Can stop as soon as complete code found No need for end-of-code marker Nondeterministic Multiple Huffman coding possible for same input If more than two trees with same minimal weight
  • 29. Huffman Code Properties Greedy algorithm Chooses best local solution at each step Combines 2 trees with lowest frequency Still yields overall best solution Optimal prefix code Based on statistical frequency Better compression possible (depends on data) Using other approaches (e.g., pattern dictionary)