- 2072 Chaitra (CS II) · 4 marks
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 . Frequent symbols get short code words.
Procedure:
- List the symbols in decreasing order of probability.
- Combine the two lowest probabilities into a new probability equal to their sum.
- Put the sum in the reordered list, as high as possible among equal values.
- Repeat until only two (then one) probabilities remain.
- 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 .
Reduction (sum of two lowest moved up as high as possible):
| Stage | Probabilities (descending) | Combined |
|---|---|---|
| 1 | 0.4, 0.2, 0.2, 0.1, 0.1 | 0.1 + 0.1 = 0.2 |
| 2 | 0.4, 0.2, 0.2, 0.2 | 0.2 + 0.2 = 0.4 |
| 3 | 0.4, 0.4, 0.2 | 0.4 + 0.2 = 0.6 |
| 4 | 0.6, 0.4 | 0.6 + 0.4 = 1 |
| Symbol | Huffman code | ||
|---|---|---|---|
| A | 0.4 | 00 | 2 |
| B | 0.2 | 10 | 2 |
| C | 0.2 | 11 | 2 |
| D | 0.1 | 010 | 3 |
| E | 0.1 | 011 | 3 |
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)
Related questions
- Shannon-Fano and Huffman codes for six messages · Communication Systems, chapter 6
- Source coding aim; Huffman "Kun Mandir Ma Janchhau Yatri" · Communication Systems, chapter 6
- Source coding; fixed vs variable length codes · Data Communication, chapter 8