Huffman Coding
Huffman Coding
Huffman Coding
Agenda
Encoding
Compression
Huffman Coding
Encoding
UT CS
85 84 32 67 83
01010101 01010100 00100000 01000011
01010011
what is a file? how do some OS use file extensions?
open a bitmap in a text editor
open a pdf in word
ASCII - UNICODE
Text File
Text File???
Bitmap File
Bitmap File????
JPEG File
JPEG VS BITMAP
JPEG File
10
Encoding Schemes
11
Agenda
Encoding
Compression
Huffman Coding
12
Compression
Compression: Storing the same information
but in a form that takes less memory
lossless and lossy compression
Recall:
13
Lossy Artifacts
14
Why Bother?
Is compression really necessary?
2 Terabytes
500 HD, 2 hour movies or 500,000 songs
15
CPU Capability
Mobile Devices?
Cellular Network
iPhone CPU
17 download, 7 upload
17
Data In
From Network
18
Agenda
Encoding
Compression
Huffman Coding
20
21
22
Building a Tree
Scan the original text
Consider the following short text
Eerie eyes seen near lake.
Count up the occurrences of all characters in
the text
25
Building a Tree
Scan the original text
E e r i space
y s n a r l k .
26
Building a Tree
Scan the original text
Char Freq.
27
Building a Tree
Prioritize characters
Create binary tree nodes with character
and frequency of each character
Place nodes in a priority queue
The lower the occurrence, the higher the
priority in the queue
28
Building a Tree
The queue after inserting all nodes
sp
Building a Tree
While priority queue contains two or more
nodes
Create new node
Dequeue node and make it left subtree
Dequeue next node and make it right subtree
Frequency of new node equals sum of frequency of
left and right children
Enqueue new node back into queue
30
Building a Tree
sp
31
Building a Tree
sp
2
E
1
32
Building a Tree
2
E
sp
33
Building a Tree
2
E
sp
2
k
1
34
Building a Tree
sp
35
Building a Tree
sp
2
y
1
36
Building a Tree
sp
37
Building a Tree
sp
4
a
2
38
Building a Tree
sp
39
Building a Tree
E
1
2
i
1
sp
8
a
4
r
2
40
Building a Tree
E
1
2
i
1
sp
8
a
41
Building a Tree
sp
8
a
4
2
1
42
Building a Tree
sp
43
Building a Tree
6
sp
2
y
44
Building a Tree
e
sp
Building a Tree
e
sp
4
8
4
4
a
2
46
Building a Tree
sp
e
8
4
a
47
Building a Tree
e
8
10
a
sp
1
48
Building a Tree
e
8
10
4
4
a
sp
49
Building a Tree
10
16
sp
8
4
4
a
2
50
Building a Tree
10
16
e
sp
8
4
4
a
51
Building a Tree
26
16
10
4
sp
4
a
52
Building a Tree
After
enqueueing
this node
there is
only one
node left in
priority
queue.
26
16
10
4
sp
4
a
2
53
Building a Tree
Dequeue the single node
left in the queue.
This tree contains the
new code words for each
character.
Frequency of root node
should equal number of
characters in text.
26
16
10
4
2
6
2
E i k l y .
sp
4
1 1 1 1 1 1
8
4
a n
r s
2 2
2 2
26
16
10
4
2
6
2
E i
k l y .
1 1
1 1 1 1
sp
55
8
4
a n
r s
2 2
2 2
Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
26
16
10
4
2
6
2
E i
k l y .
1 1
1 1 1 1
sp
56
8
4
a n
r s
2 2
2 2
000010111000011001110
010010111101111111010
110101111011011001110
011001111000010100101
Char
E
i
k
l
y
.
space
e
a
n
r
s
Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
57
000010111000011001110
010010111101111111010
110101111011011001110
011001111000010100101
Tree predetermined
based on statistical analysis of text files or file types
59
26
10
4
2
A.
B.
C.
D.
E.
16
e
6
2
E i k l y .
sp
4
1 1 1 1 1 1
60
8
4
a n
r s
2 2
2 2
Assignment Hints
61
Assignment Example
"Eerie eyes seen near lake." will result in different
codes than those shown in slides due to:
adding elements in order to PriorityQueue
required pseudo eof character (PEOF)
62
Assignment Example
Char Freq. Char Freq. Char Freq.
E
1
y
1
k 1
e
8
s
2
. 1
r
2
n
2
PEOF 1
i
1
a
2
space 4
l
1
63
Assignment Example
.
1
E
1
i
1
k
1
l
1
y
1
PEOF
1
a
2
n
2
r
2
s
2
64
SP
4
e
8
Assignment Example
i
1
k
1
l
1
y
1
PEOF
1
a
2
n
2
r
2
s
2
2
.
1
E
1
65
SP
4
e
8
Assignment Example
i
1
k
1
l
1
y
1
PEOF a
1
2
n
2
r
2
s
2
SP
4
2
.
1
E
1
66
e
8
Assignment Example
l
1
y
1
PEOF
1
a
2
n
2
r
2
s
2
2
.
1
E
1
i
1
k
1
67
SP
4
e
8
Assignment Example
PEOF
1
a
2
n
2
r
2
s
2
2
.
1
E
1
i
1
SP
4
2
k
1
l
1
y
1
68
e
8
Assignment Example
n
2
r
2
s
2
2
.
1
E
1
i
1
2
k
1
l
1
SP
4
3
y
1
PEOF
1
a
2
69
e
8
Assignment Example
s
2
2
.
1
E
1
i
1
2
k
1
l
1
SP
4
3
y
1
PEOF
1
e
8
a
2
n
2
70
r
2
Assignment Example
2
i
1
2
k
1
l
1
SP
4
3
y
1
PEOF
1
a
2
n
2
e
8
4
s
2
r
2
2
.
1
71
E
1
4
n
2
r
2
s
2
2
.
1
2
E
1
i
1
e
8
SP
4
3
k
1
l
1
y
1
PEOF
1
a
2
72
4
2
2
i
1
k
1
l
1
PEOF
1
SP
4
3
y
1
e
8
a
2
n
2
4
r
2
s
2
2
.
1
73
E
1
11
e
8
4
n
2
r
2
4
s
2
.
1
SP
4
3
E
1
i
1
k
1
l
1
y
1
PEOF
1
74
a
2
11
16
e
8
4
2
SP
4
3
i
1
k
1
l
1
y
1
PEOF
1
a
2
8
4
4
n
2
s
2
r
2
2
.
1
75
E
1
27
11
16
e
8
4
2
SP
4
3
i
1
k
1
l
1
y
1
PEOF
1
a
2
8
4
4
n
2
s
2
r
2
2
.
1
76
E
1
Codes
value:
value:
value:
value:
value:
value:
value:
value:
value:
value:
value:
value:
value: