Huffman codes
R. Inkulu
http://www.iitg.ac.in/rinkulu/
(Huffman codes)
1 / 15
(Huffman codes)
2 / 15
Prefix code
(Huffman codes)
3 / 15
xS fx .|(x)|.
(Huffman codes)
4 / 15
ABL(1 ) = 2.25
with 2 (a) = 11, 2 (b) = 10, 2 (c) = 01, 2 (d) = 001, 2 (e) = 000,
ABL(2 ) = 2.23
(Huffman codes)
5 / 15
a
0
0
e
1
b
1
d
0
c
1
b
0
e
0
d
1
a
1
c
c
0
e
0
b
1
a
1
d
(a) = 1, (b) =
letter in S. For each symbol x S, the path from the root to the leaf
labeled x; each time the path goes from a node to its left (resp. right)
child, write down a 0 (resp. 1) to get the encoding of x.
(Huffman codes)
6 / 15
a
0
0
e
1
b
1
d
0
c
1
b
0
e
0
d
1
a
1
c
0
0
e
0
b
1
a
1
d
(a) = 1, (b) =
letter in S. For each symbol x S, the path from the root to the leaf
labeled x; each time the path goes from a node to its left (resp. right)
child, write down a 0 (resp. 1) to get the encoding of x.
The encoding of S constructed from T is a prefix code.
(Huffman codes)
6 / 15
a
0
0
e
1
d
0
c
1
b
0
e
0
d
1
a
1
c
1
b
0
e
0
b
1
a
1
d
(a) = 1, (b) =
(Huffman codes)
7 / 15
(Huffman codes)
8 / 15
(Huffman codes)
8 / 15
(Huffman codes)
8 / 15
(Huffman codes)
xS fx .|(x)|
xS fx . depthT (x).
8 / 15
xS fx .|(x)|
xS fx . depthT (x).
a
0
0
e
1
d
0
c
1
b
0
e
0
d
1
a
1
c
1
b
c
0
e
0
b
1
a
1
d
(a) = 1, (b) =
For fa = 0.32, fb = 0.25, fc = 0.20, fd = 0.18, fe = 0.15, the rightmost one gives an optimal prefix code.
(Huffman codes)
8 / 15
(Huffman codes)
9 / 15
(Huffman codes)
10 / 15
(Huffman codes)
10 / 15
(Huffman codes)
10 / 15
(Huffman codes)
xS fx .|(x)|
xS fx . depthT (x).
10 / 15
For any two leaves u and v with depth(u) < depth(v) in an optimal full
binary tree T , the symbol associated with u must be more frequent than
the symbol associated with v.
- proof using an exchange argument
(Huffman codes)
11 / 15
For any two leaves u and v with depth(u) < depth(v) in an optimal full
binary tree T , the symbol associated with u must be more frequent than
the symbol associated with v.
- proof using an exchange argument
With the above in place, choice of assignment of symbols among leaves
(Huffman codes)
11 / 15
take leaves of least depth and label them with the highest-frequency
(Huffman codes)
12 / 15
two lowest-frequency letters, say x and y, are assigned to leaves that are
sibilings in T .
(Huffman codes)
13 / 15
two lowest-frequency letters, say x and y, are assigned to leaves that are
sibilings in T .
Let x and y be the two lowest-frequency letters. Let T be a full binary
(Huffman codes)
13 / 15
Huffman algorithm
Recursively find two symbols y, z with lowest frequency and make them
0
6/21
1
0
3/21
0
0
12/21
1
21/21
1
0
d:6/21 e:4/21
9/21
1
f:5/21
c:3/21
(Huffman codes)
14 / 15
Huffman algorithm
Recursively find two symbols y, z with lowest frequency and make them
0
6/21
1
0
3/21
0
0
12/21
1
21/21
1
0
d:6/21 e:4/21
9/21
1
f:5/21
c:3/21
14 / 15
Correctness
(Huffman codes)
15 / 15
Correctness
(Huffman codes)
15 / 15