Skip to main content

Shannon-Fano and Huffman codes for six messages

Short answer

For probabilities 0.3,0.08,0.1,0.15,0.25,0.12, Shannon-Fano and Huffman codes both give average length 2.45 bits/message and efficiency 98.87%.

Six messages are transmitted with probabilities 0.3, 0.08, 0.1, 0.15, 0.25 and 0.12 respectively. Obtain their respective Shannon-Fano and Huffman's codes and code efficiencies.

Messages m1…m6m_1 \dots m_6 have p=0.3,0.08,0.1,0.15,0.25,0.12p = 0.3, 0.08, 0.1, 0.15, 0.25, 0.12 (sum = 1). Arranged in decreasing order: m1m_1 0.3, m5m_5 0.25, m4m_4 0.15, m6m_6 0.12, m3m_3 0.1, m2m_2 0.08.

Entropy:

Symbolpip_ilog⁡2(1/pi)\log_2(1/p_i)pilog⁡2(1/pi)p_i\log_2(1/p_i)
m10.31.73700.5211
m20.083.64390.2915
m30.13.32190.3322
m40.152.73700.4105
m50.252.00000.5000
m60.123.05890.3671
Total12.4224
H=2.4224 bits/messageH = 2.4224\ \text{bits/message}

Shannon–Fano code

Split into equal-probability halves: {0.3, 0.25} = 0.55 vs {0.15, 0.12, 0.1, 0.08} = 0.45; then {0.15, 0.12} = 0.27 vs {0.1, 0.08} = 0.18, and so on.

Symbolpip_iStep 1Step 2Step 3Codelil_i
m10.300002
m50.2501012
m40.151001003
m60.121011013
m30.11101103
m20.081111113
LˉSF=0.3(2)+0.25(2)+0.15(3)+0.12(3)+0.1(3)+0.08(3)=0.6+0.5+0.45+0.36+0.3+0.24=2.45 bitsηSF=2.42242.45=98.87%\begin{aligned} \bar L_{SF} &= 0.3(2)+0.25(2)+0.15(3)+0.12(3)+0.1(3)+0.08(3) \\ &= 0.6+0.5+0.45+0.36+0.3+0.24 = 2.45\ \text{bits} \\ \eta_{SF} &= \frac{2.4224}{2.45} = 98.87\% \end{aligned}

Huffman code

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

StageProbabilities (descending)Combined
10.3, 0.25, 0.15, 0.12, 0.1, 0.080.1 + 0.08 = 0.18
20.3, 0.25, 0.18, 0.15, 0.120.15 + 0.12 = 0.27
30.3, 0.27, 0.25, 0.180.25 + 0.18 = 0.43
40.43, 0.3, 0.270.3 + 0.27 = 0.57
50.57, 0.430.57 + 0.43 = 1
Symbolpip_iHuffman codelil_i
m10.3002
m50.25102
m40.150103
m60.120113
m30.11103
m20.081113
LˉH=0.3(2)+0.25(2)+0.15(3)+0.12(3)+0.1(3)+0.08(3)=2.45 bitsηH=2.42242.45=98.87%\begin{aligned} \bar L_{H} &= 0.3(2)+0.25(2)+0.15(3)+0.12(3)+0.1(3)+0.08(3) = 2.45\ \text{bits} \\ \eta_{H} &= \frac{2.4224}{2.45} = 98.87\% \end{aligned}
MethodLˉ\bar LEfficiencyRedundancy
Shannon–Fano2.4598.87 %1.13 %
Huffman2.4598.87 %1.13 %

Answer: both codes give Lˉ=2.45\bar L = 2.45 bits/message and efficiency 98.87 %.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Communication Systems chapter 6 questions →