Professional Documents
Culture Documents
Huffman Coding
Huffman Coding
CLRS- 16.3
Huffman coding.
"
!
Frequency in 000s
"
!
!
Freq in 000s
a xed-length
a variable-length
bits,
"!
and
Encoding
Given a code (corresponding to some alphabet ) and
a message it is easy to encode the message. Just
replace the characters by the codewords.
Example:
If the code is
Decoding
Prex-Codes
Fixed-length codes are always uniquely decipherable
(why).
We saw before that these do not always give the best
compression so we prefer to use variable length codes.
Prex Code: A code is called a prex (free) code if
no codeword is a prex of another one.
Example:
a prex code.
is
bits,
characters
frequency
xed-length code
prex code
saving.
n4/100
e/45
n3/55
n2/35
n1/20
a/20
b/15
c/5
d/15
!
10
n4/100
e/45
n3/55
n2/35
n1/20
a/20
b/15
c/5
d/15
!
Note that
, the depth of leaf in tree is equal
to the length,
of the codeword in code
associated with that leaf. So,
The sum
pathlength of tree
11
Huffman Coding
12
Let
be the
alphabet and its frequency distribution.
In the rst step Huffman coding merges and .
b/15
e/45
n1/20
c/5
d/15
Alphabet is now
a/20
13
Alphabet is now
Algorithm merges and
(could also have merged
and ).
n2/35
New alphabet is
c/5
b/15
d/15
a/20
e/45
n1/20
14
and
e/45
55
0
n2/35
n1/20
New alphabet is
c/5
b/15
d/15
a/20
Alphabet is
Algorithm merges
15
Current alphabet is
Algorithm merges and
and nishes.
n4/100
e/45
n3/55
n2/35
n1/20
a/20
b/15
c/5
d/15
16
e/45
n3/55
n2/35
n1/20
a/20
b/15
c/5
d/15
Huffman code is
,
,
,
,
.
This is the optimum (minimum-cost) prex code for
this distribution.
17
Huffman( )
;
;
the future leaves
for
to
Why
?
new node;
Extract-Min( );
Extract-Min( );
;
Insert(
);
&)$(# "
&
'%$# "
Running time is
operation takes time
18
19
Proof: Let
be an optimum prex code tree, and let and
be two siblings at the maximum depth of the tree (must exist
because is full). Assume without loss of generality that
and
(if this is not true, then rename these
characters). Since and have the two smallest frequencies it
follows that
(they may be equal) and
(may be equal). Because and are at the deepest level of the
tree we know that
and
.
#
) #
#
# # # # # # # # # # # # # )
#
# )
#
#
# # #
#
#
# #
#
#
Therefore,
switching
, that is,
with
is an optimum tree. By
20
Now switch the positions of and in the tree resulting in a different tree
and see how the cost changes. Since is optimum,
21
22