Huffman Algorithm/coding tree - Data Structures

C E N T R A L U N I V E R S I T Y O F K A S H M I R
Tulmulla Campus, Ganderbal
BTCS-303
DATA S T R U C T U R E S
Huffman Tree
Presented by: Sameem Makhdoomi
Enrollment no. 2124CUKmr09
Contents:
1) What is Haffman’s coding?
2) Why do we need it?
3) What is fixed sized codes.
4) What are variable sized codes.
5) How to encode and decode Huffman’s coding?
 Huffman Coding is a technique of compressing data to reduce
it’s size without losing any of the details. It was first developed
by David Huffman.
 Huffman Coding is generally useful to compress the data in
which there are frequently occuring characters.
What is Huffman Coding?
1)Suppose you want to store the data in a file. You can store
the data in compressed form to reduce the size of a file
2)If we want to send the data over a network, the data can
be compressed and sent to reduce the cost of transmission
Why do we need Huffman Coding?


To understand what will be the cost of a message or the size of that message, all that will be measured in terms of bits
Electronic devices or computers all use Binary codes for sending
message of characters or English alphabets
Message - BCCABBDDAECCBBAEDDCC
Length – 20 ASCII – 8bits
Suppose, I have a message here now this message I want to store it in a file or send it over a network
Characters HEXA BINARY
A 41 01000001
B 42 01000010
C 43 01000011
D 44 01000100
E 45 01000101
8 bits for each alphabet and for this message
containing 20 alphabets the size will be:
8 * 20 = 160 bits
Huffman Algorithm/coding tree - Data Structures
Q1: Do i need 8 bit coding for just 5 capital alphabets?
Ans. NO! Few bits can be sufficient.
[0/1] in _ 1bit (2 combinations)
[00,01,10,11] in _ _ 2bits (4 combinations)
That is why we will take _ _ _3bits because it will have [8 combinations]
Q2: Can i use my own codes with less amount of bits?
Ans. YES! That we will learn in next slides.
BCCABBDDAECCBBAEDDCC (20)
Coded message: 001010010000001001011011000100010010001001000100011011010010
Character Count /
Frequency
Code
A 3 000
B 5 001
C 6 010
D 4 011
E 2 100

3 bits for each alphabet and for this
message containing 20 alphabets the
size will be:

3 * 20 =60 bits

But that’s not enough!
Fixed Sized coding:
We must send the chart of codes along with the coded
message to know whats in the message / to decode it.

5 alphabets in original form with ascii codes:
5 * 8 =40bits +

3bits for each code: 3 * 5 = 15bits
Thus table is of size: 55bits
Thus total size of message along with the table will be:
60 +55 = 115bits
(around 35 to 40% reduction in size from 160 to 115)
A 000
B 001
C 010
D 011
E 100
Variable Sized coding (Huffman’s Coding):
I. Huffman says that we don't have to take fixed size codes for the
alphabets/elements.
II.Some characters or alphabets may be appearing less number of times,
some may be appearing more number of times so if you give small size
code for the more appearing characters then the size of the entire
message will be definitely reduced.
How to encode Huffman’s tree?
 Huffman code follows Optimal Marsh pattern (i.e merging together the
minimum sorted elements to get a single element).
 Huffman has given an approach for getting our own variable size code
Suppose the string below is to sent over a network:
B C A A D D D C C A C A C A C
Size of this message without coding: 15 * 8 = 120bits
Calculate the frequency of each character in the string
B C A A D D D C C A C A C A C
1 6 5 3
B C A D
Given string:
Sort the characters in increasing order of the frequency.
1 6 5 3 1 3 5 6
B C A D B D A C
1 3 5 6 4 5 6
B D A C * A C
Summing up w1 and w2, where w1 and w2
are the first 2 in the increasing order
queue
w1 w2
4 w1+w2
w1 1
B
w2 3
D
4 5 6
* A C
4
1 C 3
B D
1 3
B D
4 5
9
A
w1 w2
w1 w2
9 6
* C
w1+w2
6 9
C *
1 3
B D
5
A
6 9
15
0 1
1
C
0 1
w2
0
4
w1 w2
w1
w1+w2
1 3
B D
5
A
6
15
0 1
9
0
4
C
0
1
1
Character Frequency Code Code Size
A 5 11 5 * 2 =10
B 1 100 1 * 3 =3
C 6 0 6 * 1 =6
D 3 101 3 * 3 =9
4*8=32bits 15 9 bits 28 bits
B C A A D D D C C A C A C A C
Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0
1 3
B D
5
A
6 9
15
0 1
0
4
C
0
1
1
coded message to know whats in the message/
to decode it.

4 alphabets in original form with ascii codes:
4 * 8 =32bits +
Random bits for every code:
2+3+1+3=9bits

Thus tree/table is of size: 32+9 =41bits
Random bits for each alphabet and for this message
containing 15 alphabets the size will be:
5*2+3*1+6*1+3*3 =28bits
We must send the tree/table along with the
A 11
B 100
C 0
D 101
B C A A D D D C C A C A C A C
A 11
B 100
C 0
D 101
Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0
Thus total size of coded message along with the tree/table will be:
Coded message 28 +tree 41 = 69bits
Size of this message without coding: 15 * 8 = 120bits
Thus Huffman Coding reduced size of message from
120bits to just 69bits.
1 3
B D
5
A
6 9
15
0 1
0
4
C
0
1
1
How to decode Huffman’s tree?
Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0
Decoded message: BCAADDDCCACACAC
To decode the coded message we must have a tree/
table and a coded message.
I. We start traversing the tree using the coded
message and by the help of 0’s and 1’s on the
edges of the tree we can get our decoded
message.
Character Frequency
M 1
P 2
Y 4
S 4
3
7
11
4
Y
0
1
M
1
2
P
0 1
0
4
S
1
Example.
Coded message: 100101110011101011011
Element Code Code length
M 100 3
P 101 3
Y 11 2
S 0 1
Decoded message: MPYSSYPSYSY
3
7
11
0
1
M
1
2
P
0 1
4
Y
0
4
S
1
11
Example.
Coded message:
000000000100010001000100010000000
110001100110111000010001100000110
010010000000111100000100110000011
110001011100000000111011000011110
001010100011100010111001011100011
101010001100001000110001110001000
011010100011000111100000100001111
110001110001111000000010110000000
00110000011-000011100010101.
Character Code
I 00000
F 00001
Y 00010
N 10000
O 00100
U 01000
E 00011
D 00110
Character Code
M 01100
T 11000
A 00111
K 01110
S 11100
R 10101
H 10111
L 10001
W 01010
Time Complexity for Huffman’s tree:
O(n log n)
Want to learn more about Huffman Encoding and how
it’s done in real life:
 Research Gate Huffman Coding
1 de 25

Recomendados

Chapter 4 Lossless Compression Algorithims.pptx por
Chapter 4 Lossless Compression Algorithims.pptxChapter 4 Lossless Compression Algorithims.pptx
Chapter 4 Lossless Compression Algorithims.pptxMedinaBedru
5 visualizações29 slides
Huffman Coding Algorithm Presentation por
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationAkm Monir
5.6K visualizações14 slides
Huffman Coding.ppt por
Huffman Coding.pptHuffman Coding.ppt
Huffman Coding.pptMuktarHossain13
6 visualizações26 slides
HACKING AN AIR GAP COMPUTER USING ULTRASONIC SIGNALSPRESENT.docx por
HACKING AN AIR GAP COMPUTER  USING ULTRASONIC SIGNALSPRESENT.docxHACKING AN AIR GAP COMPUTER  USING ULTRASONIC SIGNALSPRESENT.docx
HACKING AN AIR GAP COMPUTER USING ULTRASONIC SIGNALSPRESENT.docxshericehewat
19 visualizações42 slides
Compression ii por
Compression iiCompression ii
Compression iiChandra Mohan Negi
409 visualizações51 slides
Compression Ii por
Compression IiCompression Ii
Compression Iianithabalaprabhu
487 visualizações51 slides

Mais conteúdo relacionado

Similar a Huffman Algorithm/coding tree - Data Structures

huffman ppt por
huffman ppthuffman ppt
huffman pptPatrickIasiahLBelga
5 visualizações25 slides
F-Bus Protocol - R.D.Sivakumar por
F-Bus Protocol - R.D.SivakumarF-Bus Protocol - R.D.Sivakumar
F-Bus Protocol - R.D.SivakumarSivakumar R D .
56 visualizações47 slides
Text compression por
Text compressionText compression
Text compressionSammer Qader
7K visualizações28 slides
Hufman coding basic por
Hufman coding basicHufman coding basic
Hufman coding basicradthees
365 visualizações17 slides
Farhana shaikh webinar_huffman coding por
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana Shaikh
34 visualizações53 slides
Lec7 8 9_10 coding techniques por
Lec7 8 9_10 coding techniquesLec7 8 9_10 coding techniques
Lec7 8 9_10 coding techniquesDom Mike
655 visualizações29 slides

Similar a Huffman Algorithm/coding tree - Data Structures (20)

F-Bus Protocol - R.D.Sivakumar por Sivakumar R D .
F-Bus Protocol - R.D.SivakumarF-Bus Protocol - R.D.Sivakumar
F-Bus Protocol - R.D.Sivakumar
Sivakumar R D .56 visualizações
Text compression por Sammer Qader
Text compressionText compression
Text compression
Sammer Qader7K visualizações
Hufman coding basic por radthees
Hufman coding basicHufman coding basic
Hufman coding basic
radthees365 visualizações
Farhana shaikh webinar_huffman coding por Farhana Shaikh
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman coding
Farhana Shaikh34 visualizações
Lec7 8 9_10 coding techniques por Dom Mike
Lec7 8 9_10 coding techniquesLec7 8 9_10 coding techniques
Lec7 8 9_10 coding techniques
Dom Mike655 visualizações
Lab01 por Fakhar Abbas
Lab01Lab01
Lab01
Fakhar Abbas244 visualizações
Lecft3data por ali Hussien
Lecft3dataLecft3data
Lecft3data
ali Hussien164 visualizações
Error Detection N Correction por Ankan Adhikari
Error Detection N CorrectionError Detection N Correction
Error Detection N Correction
Ankan Adhikari4.5K visualizações
BCDCONVERTER.pptx por MagedAldhaeebi
BCDCONVERTER.pptxBCDCONVERTER.pptx
BCDCONVERTER.pptx
MagedAldhaeebi3 visualizações
Digital image processing- Compression- Different Coding techniques por sudarmani rajagopal
Digital image processing- Compression- Different Coding techniques Digital image processing- Compression- Different Coding techniques
Digital image processing- Compression- Different Coding techniques
sudarmani rajagopal287 visualizações
. computer codes por asadsiddique12
. computer codes. computer codes
. computer codes
asadsiddique1299 visualizações
Topic 1 Data Representation por ekul
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representation
ekul16.3K visualizações
Topic 1 Data Representation por Kyle
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representation
Kyle3.8K visualizações
1.1.2 HEXADECIMAL por Buxoo Abdullah
1.1.2 HEXADECIMAL1.1.2 HEXADECIMAL
1.1.2 HEXADECIMAL
Buxoo Abdullah709 visualizações
Huffman coding por George Ang
Huffman codingHuffman coding
Huffman coding
George Ang1.3K visualizações
Komdat-Kompresi Data por mursalinfajri007
Komdat-Kompresi DataKomdat-Kompresi Data
Komdat-Kompresi Data
mursalinfajri007699 visualizações

Mais de Sameem Makhdoomi

Google's Employer Branding Strategy.pdf por
Google's Employer Branding Strategy.pdfGoogle's Employer Branding Strategy.pdf
Google's Employer Branding Strategy.pdfSameem Makhdoomi
1K visualizações10 slides
QUIZLET por
QUIZLETQUIZLET
QUIZLETSameem Makhdoomi
30 visualizações12 slides
Linear Differential Equation and Equations Reducible to Linear Form por
Linear Differential Equation and Equations Reducible to Linear FormLinear Differential Equation and Equations Reducible to Linear Form
Linear Differential Equation and Equations Reducible to Linear FormSameem Makhdoomi
271 visualizações21 slides
Coulomb's Law por
Coulomb's LawCoulomb's Law
Coulomb's LawSameem Makhdoomi
55 visualizações46 slides
Data Abstraction por
Data AbstractionData Abstraction
Data AbstractionSameem Makhdoomi
119 visualizações11 slides
Genetic Disorders por
Genetic DisordersGenetic Disorders
Genetic DisordersSameem Makhdoomi
53 visualizações13 slides

Mais de Sameem Makhdoomi(13)

Google's Employer Branding Strategy.pdf por Sameem Makhdoomi
Google's Employer Branding Strategy.pdfGoogle's Employer Branding Strategy.pdf
Google's Employer Branding Strategy.pdf
Sameem Makhdoomi1K visualizações
Linear Differential Equation and Equations Reducible to Linear Form por Sameem Makhdoomi
Linear Differential Equation and Equations Reducible to Linear FormLinear Differential Equation and Equations Reducible to Linear Form
Linear Differential Equation and Equations Reducible to Linear Form
Sameem Makhdoomi271 visualizações
Coulomb's Law por Sameem Makhdoomi
Coulomb's LawCoulomb's Law
Coulomb's Law
Sameem Makhdoomi55 visualizações
Data Abstraction por Sameem Makhdoomi
Data AbstractionData Abstraction
Data Abstraction
Sameem Makhdoomi119 visualizações
Genetic Disorders por Sameem Makhdoomi
Genetic DisordersGenetic Disorders
Genetic Disorders
Sameem Makhdoomi53 visualizações
Application of Network Theorem in AC Circuit. Resonance in series and paralle... por Sameem Makhdoomi
Application of Network Theorem in AC Circuit. Resonance in series and paralle...Application of Network Theorem in AC Circuit. Resonance in series and paralle...
Application of Network Theorem in AC Circuit. Resonance in series and paralle...
Sameem Makhdoomi80 visualizações
Body language and use of voice during the presentation por Sameem Makhdoomi
 Body language and use of voice during the presentation Body language and use of voice during the presentation
Body language and use of voice during the presentation
Sameem Makhdoomi117 visualizações
CRIMINOLOGY por Sameem Makhdoomi
CRIMINOLOGYCRIMINOLOGY
CRIMINOLOGY
Sameem Makhdoomi15 visualizações
Data mining por Sameem Makhdoomi
Data miningData mining
Data mining
Sameem Makhdoomi15 visualizações
Energy levels of the system and transitions por Sameem Makhdoomi
Energy levels of the system and transitionsEnergy levels of the system and transitions
Energy levels of the system and transitions
Sameem Makhdoomi22 visualizações
Mechanics - Bar Of Uniform Strength por Sameem Makhdoomi
Mechanics - Bar Of Uniform StrengthMechanics - Bar Of Uniform Strength
Mechanics - Bar Of Uniform Strength
Sameem Makhdoomi73 visualizações
CAUCHY’S MEAN VALUE THEOREM por Sameem Makhdoomi
CAUCHY’S MEAN VALUE THEOREMCAUCHY’S MEAN VALUE THEOREM
CAUCHY’S MEAN VALUE THEOREM
Sameem Makhdoomi514 visualizações

Último

DevOps-ITverse-2023-IIT-DU.pptx por
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptxAnowar Hossain
12 visualizações45 slides
Control Systems Feedback.pdf por
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdfLGGaming5
6 visualizações39 slides
Generative AI Models & Their Applications por
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their ApplicationsSN
8 visualizações1 slide
K8S Roadmap.pdf por
K8S Roadmap.pdfK8S Roadmap.pdf
K8S Roadmap.pdfMaryamTavakkoli2
8 visualizações1 slide
MK__Cert.pdf por
MK__Cert.pdfMK__Cert.pdf
MK__Cert.pdfHassan Khan
11 visualizações1 slide
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptxlwang78
83 visualizações19 slides

Último(20)

DevOps-ITverse-2023-IIT-DU.pptx por Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain12 visualizações
Control Systems Feedback.pdf por LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 visualizações
Generative AI Models & Their Applications por SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 visualizações
K8S Roadmap.pdf por MaryamTavakkoli2
K8S Roadmap.pdfK8S Roadmap.pdf
K8S Roadmap.pdf
MaryamTavakkoli28 visualizações
MK__Cert.pdf por Hassan Khan
MK__Cert.pdfMK__Cert.pdf
MK__Cert.pdf
Hassan Khan11 visualizações
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang7883 visualizações
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ... por AltinKaradagli
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
AltinKaradagli12 visualizações
GDSC Mikroskil Members Onboarding 2023.pdf por gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil53 visualizações
CHEMICAL KINETICS.pdf por AguedaGutirrez
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdf
AguedaGutirrez13 visualizações
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... por csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn5 visualizações
Codes and Conventions.pptx por IsabellaGraceAnkers
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptx
IsabellaGraceAnkers9 visualizações
Effect of deep chemical mixing columns on properties of surrounding soft clay... por AltinKaradagli
Effect of deep chemical mixing columns on properties of surrounding soft clay...Effect of deep chemical mixing columns on properties of surrounding soft clay...
Effect of deep chemical mixing columns on properties of surrounding soft clay...
AltinKaradagli9 visualizações
Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla637 visualizações
Instrumentation & Control Lab Manual.pdf por NTU Faisalabad
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdf
NTU Faisalabad 6 visualizações
START Newsletter 3 por Start Project
START Newsletter 3START Newsletter 3
START Newsletter 3
Start Project5 visualizações
DESIGN OF SPRINGS-UNIT4.pptx por gopinathcreddy
DESIGN OF SPRINGS-UNIT4.pptxDESIGN OF SPRINGS-UNIT4.pptx
DESIGN OF SPRINGS-UNIT4.pptx
gopinathcreddy19 visualizações
SUMIT SQL PROJECT SUPERSTORE 1.pptx por Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 15 visualizações
sam_software_eng_cv.pdf por sammyigbinovia
sam_software_eng_cv.pdfsam_software_eng_cv.pdf
sam_software_eng_cv.pdf
sammyigbinovia5 visualizações

Huffman Algorithm/coding tree - Data Structures

  • 1. C E N T R A L U N I V E R S I T Y O F K A S H M I R Tulmulla Campus, Ganderbal BTCS-303 DATA S T R U C T U R E S Huffman Tree Presented by: Sameem Makhdoomi Enrollment no. 2124CUKmr09
  • 2. Contents: 1) What is Haffman’s coding? 2) Why do we need it? 3) What is fixed sized codes. 4) What are variable sized codes. 5) How to encode and decode Huffman’s coding?
  • 3.  Huffman Coding is a technique of compressing data to reduce it’s size without losing any of the details. It was first developed by David Huffman.  Huffman Coding is generally useful to compress the data in which there are frequently occuring characters. What is Huffman Coding?
  • 4. 1)Suppose you want to store the data in a file. You can store the data in compressed form to reduce the size of a file 2)If we want to send the data over a network, the data can be compressed and sent to reduce the cost of transmission Why do we need Huffman Coding?
  • 5.   To understand what will be the cost of a message or the size of that message, all that will be measured in terms of bits Electronic devices or computers all use Binary codes for sending message of characters or English alphabets Message - BCCABBDDAECCBBAEDDCC Length – 20 ASCII – 8bits Suppose, I have a message here now this message I want to store it in a file or send it over a network Characters HEXA BINARY A 41 01000001 B 42 01000010 C 43 01000011 D 44 01000100 E 45 01000101 8 bits for each alphabet and for this message containing 20 alphabets the size will be: 8 * 20 = 160 bits
  • 7. Q1: Do i need 8 bit coding for just 5 capital alphabets? Ans. NO! Few bits can be sufficient. [0/1] in _ 1bit (2 combinations) [00,01,10,11] in _ _ 2bits (4 combinations) That is why we will take _ _ _3bits because it will have [8 combinations] Q2: Can i use my own codes with less amount of bits? Ans. YES! That we will learn in next slides.
  • 8. BCCABBDDAECCBBAEDDCC (20) Coded message: 001010010000001001011011000100010010001001000100011011010010 Character Count / Frequency Code A 3 000 B 5 001 C 6 010 D 4 011 E 2 100  3 bits for each alphabet and for this message containing 20 alphabets the size will be:  3 * 20 =60 bits  But that’s not enough! Fixed Sized coding:
  • 9. We must send the chart of codes along with the coded message to know whats in the message / to decode it.  5 alphabets in original form with ascii codes: 5 * 8 =40bits +  3bits for each code: 3 * 5 = 15bits Thus table is of size: 55bits Thus total size of message along with the table will be: 60 +55 = 115bits (around 35 to 40% reduction in size from 160 to 115) A 000 B 001 C 010 D 011 E 100
  • 10. Variable Sized coding (Huffman’s Coding): I. Huffman says that we don't have to take fixed size codes for the alphabets/elements. II.Some characters or alphabets may be appearing less number of times, some may be appearing more number of times so if you give small size code for the more appearing characters then the size of the entire message will be definitely reduced.
  • 11. How to encode Huffman’s tree?  Huffman code follows Optimal Marsh pattern (i.e merging together the minimum sorted elements to get a single element).  Huffman has given an approach for getting our own variable size code Suppose the string below is to sent over a network: B C A A D D D C C A C A C A C
  • 12. Size of this message without coding: 15 * 8 = 120bits Calculate the frequency of each character in the string B C A A D D D C C A C A C A C 1 6 5 3 B C A D Given string:
  • 13. Sort the characters in increasing order of the frequency. 1 6 5 3 1 3 5 6 B C A D B D A C
  • 14. 1 3 5 6 4 5 6 B D A C * A C Summing up w1 and w2, where w1 and w2 are the first 2 in the increasing order queue w1 w2 4 w1+w2 w1 1 B w2 3 D
  • 15. 4 5 6 * A C 4 1 C 3 B D 1 3 B D 4 5 9 A w1 w2 w1 w2 9 6 * C w1+w2
  • 16. 6 9 C * 1 3 B D 5 A 6 9 15 0 1 1 C 0 1 w2 0 4 w1 w2 w1 w1+w2
  • 17. 1 3 B D 5 A 6 15 0 1 9 0 4 C 0 1 1 Character Frequency Code Code Size A 5 11 5 * 2 =10 B 1 100 1 * 3 =3 C 6 0 6 * 1 =6 D 3 101 3 * 3 =9 4*8=32bits 15 9 bits 28 bits B C A A D D D C C A C A C A C Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0
  • 18. 1 3 B D 5 A 6 9 15 0 1 0 4 C 0 1 1 coded message to know whats in the message/ to decode it.  4 alphabets in original form with ascii codes: 4 * 8 =32bits + Random bits for every code: 2+3+1+3=9bits  Thus tree/table is of size: 32+9 =41bits Random bits for each alphabet and for this message containing 15 alphabets the size will be: 5*2+3*1+6*1+3*3 =28bits We must send the tree/table along with the A 11 B 100 C 0 D 101
  • 19. B C A A D D D C C A C A C A C A 11 B 100 C 0 D 101 Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0 Thus total size of coded message along with the tree/table will be: Coded message 28 +tree 41 = 69bits Size of this message without coding: 15 * 8 = 120bits Thus Huffman Coding reduced size of message from 120bits to just 69bits.
  • 20. 1 3 B D 5 A 6 9 15 0 1 0 4 C 0 1 1 How to decode Huffman’s tree? Coded message: 100 0 11 11 101 101 101 0 0 11 0 11 0 11 0 Decoded message: BCAADDDCCACACAC To decode the coded message we must have a tree/ table and a coded message. I. We start traversing the tree using the coded message and by the help of 0’s and 1’s on the edges of the tree we can get our decoded message.
  • 21. Character Frequency M 1 P 2 Y 4 S 4 3 7 11 4 Y 0 1 M 1 2 P 0 1 0 4 S 1 Example. Coded message: 100101110011101011011
  • 22. Element Code Code length M 100 3 P 101 3 Y 11 2 S 0 1 Decoded message: MPYSSYPSYSY 3 7 11 0 1 M 1 2 P 0 1 4 Y 0 4 S 1
  • 24. Time Complexity for Huffman’s tree: O(n log n)
  • 25. Want to learn more about Huffman Encoding and how it’s done in real life:  Research Gate Huffman Coding