For simplicity, we take
WLOG
Huffman Code is defined inductively:
- If
assign codewords 0 and 1 - If
, find a Huffman coding in the case of probabilities
Append 0 (respectively 1) to the codewords for
Note that this construction is prefix free.
Theorem
Huffman codings are optimal.
Proof
Lemma
Suppose
Let
- If
then - There exist two codewords of maximal length which are equal up to the last digit.
Proof
- If not, swap
th and th codewords. This decreases the expected word length, contradicting optimality. - If not, then either there is only one code word of maximal length or any two codewords of maximal length differ before the last digit. In either case, delete the last digit of each codeword of maximal length. This maintains prefix-free condition, contradicting
optimal.
We show by, by induction on
Source
We construct a Huffman coding
Let
By the previous lemma, shuffling codewords we may assume that the last two codewords (i.e. associated with the smallest probabilities) of
We define a code
Then
By induction hypothesis,
Therefore,