Lecture2huffmancoding 151018181815 Lva1 App6892
Lecture2huffmancoding 151018181815 Lva1 App6892
com 1
Contents
Prefix Code
Representing prefix Codes Using Binary Tree
Binary Tree Terminology
Decoding a Prefix Code
Example
Huffman Coding
Cost of Huffman Tree
Optimality
If every word in the code has the same length, the code is called a
fixed-length code, or a block code.
Suppose we have two binary code words a and b, where a is k bits long, b
is n bits long, and k < n. If the first k bits of b are identical to a, then a is
called a prefix of b. The last n k bits of b are called the dangling suffix.
For example, if
A prefix code is most easily represented by a binary tree in which the external nodes
are labeled with single characters that are combined to form the message. The
encoding for a character is determined by following the path down from the root of
the tree to the external node that holds that character: a 0 bit identifies a left branch
in the path, and a 1 bit identifies a right branch.
In order for this encoding scheme to reduce the number of bits in a message, we use
short encodings for frequently used characters, and long encodings for infrequent ones.
0111110010110101001111100100
encodes the message "abracadabra!". The first 0 must encode 'a', then
the next three 1's must encode 'b', then 110 must encode r, and so on as
follows:
|0|111|110|0|1011|0|1010|0|111|110|0|100
a b r a c a d a b r a !
11000111100
3. Divide the list into two parts, with the total frequency counts of the left half being as
close to the total of the right as possible.
4. The left half of the list is assigned the binary digit 0, and the right half is assigned the
digit 1. This means that the codes for the symbols in the first half will all start with 0, and
the codes in the second half will all start with 1.
Huffman (1951)
Uses frequencies of symbols in a string to build a variable rate prefix
code.
Each symbol is mapped to a binary string.
More frequent symbols have shorter codes.
No code is a prefix of another.
Example:
a 0
b 100
c 101
d 11
Q. Given a text that uses 32 symbols (26 different letters, space, and some
punctuation characters), how can we encode this text in bits?
Q. Given a text that uses 32 symbols (26 different letters, space, and some
punctuation characters), how can we encode this text in bits?
Q. Some symbols (e, t, a, o, i, n) are used far more often than others.
How can we use this to reduce our encoding?
A. Encode these characters with fewer bits, and the others with more bits.
A. Use a separation symbol (like the pause in Morse), or make sure that there is no
ambiguity by ensuring that no code is a prefix of another one.
Let p1, p2, ... , pm be the probabilities for the symbols a1, a2, ... ,am,
respectively.
Define the cost of the Huffman tree T to be
Input: Probabilities p1, p2, ... , pm for symbols a1, a2, ... ,am, respectively.
Output: We would like to find a prefix code that is has the lowest possible
average bits per symbol.
Principle 1
In a Huffman tree a lowest probability symbol has maximum distance from
the root.
exchanging a lowest probability symbol with one at maximum distance will
lower the cost.
Principle 2
The second lowest probability is a sibling of the smallest in some
Huffman tree.
If we can move it there not raising the cost.
Principle 3
Assuming we have a Huffman tree T whose two lowest probability symbols
are siblings at maximum depth, they can be replaced by a new symbol whose
probability is the sum of their probabilities.
The resulting tree is optimal for the new symbol set.
1. If there is just one symbol, a tree with one node is optimal. Otherwise
2. Find the two lowest probability symbols with probabilities p and q
respectively.
3. Replace these with a new symbol with probability p + q.
4. Solve the problem recursively for new symbols.
5. Replace the leaf with the new symbol with an internal node with two
children with the old symbols.
Principle 3 (cont)
If T were not optimal then we could find a lower cost tree T. This will
lead to a lower cost tree T for the original alphabet.
A. simpel
A. simpel
Definition. A tree is full if every node that is not a leaf has two children.
Claim. The binary tree corresponding to the optimal prefix code is full.
Definition. A tree is full if every node that is not a leaf has two children.
Claim. The binary tree corresponding to the optimal prefix code is full.