Skip to main content

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 {0.4,0.3,0.15,0.1,0.05}\{0.4, 0.3, 0.15, 0.1, 0.05\}:

Symbolpip_ilog⁡2(1/pi)\log_2(1/p_i)pilog⁡2(1/pi)p_i\log_2(1/p_i)
A00.41.32190.5288
A10.31.73700.5211
A20.152.73700.4105
A30.13.32190.3322
A40.054.32190.2161
Total12.0087
H=2.0087 bits/symbolH = 2.0087\ \text{bits/symbol}

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):

StageProbabilities (descending)Combined
10.4, 0.3, 0.15, 0.1, 0.050.1 + 0.05 = 0.15
20.4, 0.3, 0.15, 0.150.15 + 0.15 = 0.3
30.4, 0.3, 0.30.3 + 0.3 = 0.6
40.6, 0.40.6 + 0.4 = 1
Symbolpip_iHuffman codelil_i
A00.411
A10.3012
A20.150013
A30.100004
A40.0500014
LˉH=0.4(1)+0.3(2)+0.15(3)+0.1(4)+0.05(4)=0.4+0.6+0.45+0.4+0.2=2.05 bits/symbolηH=2.00872.05=97.99%\begin{aligned} \bar L_H &= 0.4(1) + 0.3(2) + 0.15(3) + 0.1(4) + 0.05(4) \\ &= 0.4 + 0.6 + 0.45 + 0.4 + 0.2 = 2.05\ \text{bits/symbol} \\ \eta_H &= \frac{2.0087}{2.05} = 97.99\% \end{aligned}

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}
Symbolpip_iStep 1Step 2Step 3Step 4Codelil_i
A00.4001
A10.310102
A20.151101103
A30.1111011104
A40.05111111114
LˉSF=0.4(1)+0.3(2)+0.15(3)+0.1(4)+0.05(4)=2.05 bits/symbolηSF=2.00872.05=97.99%\begin{aligned} \bar L_{SF} &= 0.4(1)+0.3(2)+0.15(3)+0.1(4)+0.05(4) = 2.05\ \text{bits/symbol} \\ \eta_{SF} &= \frac{2.0087}{2.05} = 97.99\% \end{aligned}

Comparison

ItemHuffmanShannon–Fano
Average length2.05 bits2.05 bits
Efficiency97.99 %97.99 %
Redundancy2.01 %2.01 %
MethodBottom-up (merging)Top-down (splitting)
OptimalityAlways optimalNot 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)

All Communication Systems chapter 6 questions →