- 2074 Chaitra (CS II) · 8 marks
Huffman and Shannon-Fano coding of A0–A4
Short answer
For symbols with probabilities 0.4,0.3,0.15,0.1,0.05, both Huffman and Shannon-Fano produce code lengths 1,2,3,4,4, average length 2.05 bits/symbol, efficiency 97.99%.
The source of information symbols {A0, A1, A2, A3 and A4} have corresponding probabilities {0.4, 0.3, 0.15, 0.1 and 0.05}. Encode the source symbols using Huffman encoder and Shannon-Fano encoder and compare their efficiency.
Entropy of the source :
| Symbol | |||
|---|---|---|---|
| A0 | 0.4 | 1.3219 | 0.5288 |
| A1 | 0.3 | 1.7370 | 0.5211 |
| A2 | 0.15 | 2.7370 | 0.4105 |
| A3 | 0.1 | 3.3219 | 0.3322 |
| A4 | 0.05 | 4.3219 | 0.2161 |
| Total | 1 | 2.0087 |
Huffman encoder
Combine the two lowest probabilities at each stage, place the sum as high as possible, then read the code from the final stage back to each symbol (0 = upper, 1 = lower).
Reduction (sum of two lowest moved up as high as possible):
| Stage | Probabilities (descending) | Combined |
|---|---|---|
| 1 | 0.4, 0.3, 0.15, 0.1, 0.05 | 0.1 + 0.05 = 0.15 |
| 2 | 0.4, 0.3, 0.15, 0.15 | 0.15 + 0.15 = 0.3 |
| 3 | 0.4, 0.3, 0.3 | 0.3 + 0.3 = 0.6 |
| 4 | 0.6, 0.4 | 0.6 + 0.4 = 1 |
| Symbol | Huffman code | ||
|---|---|---|---|
| A0 | 0.4 | 1 | 1 |
| A1 | 0.3 | 01 | 2 |
| A2 | 0.15 | 001 | 3 |
| A3 | 0.1 | 0000 | 4 |
| A4 | 0.05 | 0001 | 4 |
Shannon–Fano encoder
Divide the ordered list into two groups of nearly equal probability, 0 to the upper and 1 to the lower group, and repeat:
- {A0} = 0.4 | {A1, A2, A3, A4} = 0.6
- {A1} = 0.3 | {A2, A3, A4} = 0.3
- {A2} = 0.15 | {A3, A4} = 0.15
- {A3} | {A4}
| Symbol | Step 1 | Step 2 | Step 3 | Step 4 | Code | ||
|---|---|---|---|---|---|---|---|
| A0 | 0.4 | 0 | 0 | 1 | |||
| A1 | 0.3 | 1 | 0 | 10 | 2 | ||
| A2 | 0.15 | 1 | 1 | 0 | 110 | 3 | |
| A3 | 0.1 | 1 | 1 | 1 | 0 | 1110 | 4 |
| A4 | 0.05 | 1 | 1 | 1 | 1 | 1111 | 4 |
Comparison
| Item | Huffman | Shannon–Fano |
|---|---|---|
| Average length | 2.05 bits | 2.05 bits |
| Efficiency | 97.99 % | 97.99 % |
| Redundancy | 2.01 % | 2.01 % |
| Method | Bottom-up (merging) | Top-down (splitting) |
| Optimality | Always optimal | Not always optimal |
For this source both encoders give the same code lengths (1, 2, 3, 4, 4), so the efficiencies are equal at 97.99 %. In general Huffman's efficiency is equal to or better than Shannon–Fano's. A fixed 3-bit code would give only 66.96 %.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Coding efficiency: binary, Shannon-Fano and Huffman · Communication Systems, chapter 6
- Importance of source coding; fixed vs Shannon-Fano (five symbols) · Communication Systems, chapter 6
- Importance of source coding; Shannon-Fano vs BCD (four symbols) · Communication Systems, chapter 6
- Importance of source coding; Shannon-Fano vs fixed length (seven symbols) · Communication Systems, chapter 6
- Types of noise; Shannon-Fano and Huffman coding efficiency · Communication Systems, chapter 6
- Source coding; Huffman for five symbols (0.3, 0.25, 0.2, 0.15, 0.1) · Communication Systems, chapter 6
- Huffman code for symbols A0 to A4 · Data Communication, chapter 8
- Shannon-Fano code for eight symbols · Data Communication, chapter 8
- Shannon-Fano code for six symbols · Data Communication, chapter 8