Skip to main content

Huffman codes with examples

Short answer

Huffman coding is an optimal prefix code: repeatedly combine the two lowest probabilities and assign bits from the final merge. For symbols with probabilities 0.4,0.2,0.2,0.1,0.1, codes are A=00, B=10, C=11, D=010, E=011; average length 2.2 bits/symbol yields 96.45% efficiency vs 70.7% for a 3-bit fixed code.

Explain Huffman codes with examples.

A Huffman code is an optimal variable-length prefix code: for a given set of symbol probabilities, no other symbol-by-symbol code has a smaller average length Lˉ\bar L. Frequent symbols get short code words.

Procedure:

  1. List the symbols in decreasing order of probability.
  2. Combine the two lowest probabilities into a new probability equal to their sum.
  3. Put the sum in the reordered list, as high as possible among equal values.
  4. Repeat until only two (then one) probabilities remain.
  5. Going back from the last stage, assign 0 to one branch and 1 to the other at each merge; a symbol's code is the sequence of bits on its path.

Example: five symbols A–E with P={0.4,0.2,0.2,0.1,0.1}P = \{0.4, 0.2, 0.2, 0.1, 0.1\}.

Reduction (sum of two lowest moved up as high as possible):

StageProbabilities (descending)Combined
10.4, 0.2, 0.2, 0.1, 0.10.1 + 0.1 = 0.2
20.4, 0.2, 0.2, 0.20.2 + 0.2 = 0.4
30.4, 0.4, 0.20.4 + 0.2 = 0.6
40.6, 0.40.6 + 0.4 = 1
Symbolpip_iHuffman codelil_i
A0.4002
B0.2102
C0.2112
D0.10103
E0.10113
Lˉ=0.4(2)+0.2(2)+0.2(2)+0.1(3)+0.1(3)=2.2 bits/symbolH=2.1219 bits/symbol,η=2.12192.2=96.45%\begin{aligned} \bar L &= 0.4(2)+0.2(2)+0.2(2)+0.1(3)+0.1(3) = 2.2\ \text{bits/symbol} \\ H &= 2.1219\ \text{bits/symbol}, \qquad \eta = \frac{2.1219}{2.2} = 96.45\% \end{aligned}

A fixed-length code would need 3 bits (efficiency 70.7 %). No code word is a prefix of another, so "0001110" decodes uniquely as 00 | 011 | 10 = A, E, B. Huffman coding is used in JPEG, MP3 and ZIP.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Communication Systems chapter 6 questions →