Question

A message is made up entirely of characters from the set X={P,Q,R,S,T}X = \{P, Q, R, S, T\}. The table of probabilities for each of the characters is shown below:

Character Probability
P 0.22
Q 0.34
R 0.17
S 0.19
T 0.08
Total 1.00

If a message of 100 characters over XX is encoded using Huffman coding, then the expected length of the encoded message in bits is

Official answer and solution
Official answer
225
From
Official GATE 2017 answer key

Solution

Construct the Huffman tree by repeatedly merging the two least probable nodes, determine codeword lengths, and calculate expected message length.

Given character probabilities:

  • T:0.08T: 0.08
  • R:0.17R: 0.17
  • S:0.19S: 0.19
  • P:0.22P: 0.22
  • Q:0.34Q: 0.34

Apply the greedy Huffman tree algorithm:

  1. Combine the two lowest probabilities T(0.08)T (0.08) and R(0.17)R (0.17):
    Node (TR)(TR) with probability 0.08+0.17=0.250.08 + 0.17 = 0.25.
    Remaining probabilities: S(0.19),P(0.22),(TR)(0.25),Q(0.34)S (0.19), P (0.22), (TR) (0.25), Q (0.34).
  2. Combine the two lowest probabilities S(0.19)S (0.19) and P(0.22)P (0.22):
    Node (SP)(SP) with probability 0.19+0.22=0.410.19 + 0.22 = 0.41.
    Remaining probabilities: (TR)(0.25),Q(0.34),(SP)(0.41)(TR) (0.25), Q (0.34), (SP) (0.41).
  3. Combine the two lowest probabilities (TR)(0.25)(TR) (0.25) and Q(0.34)Q (0.34):
    Node ((TR)Q)((TR)Q) with probability 0.25+0.34=0.590.25 + 0.34 = 0.59.
    Remaining probabilities: (SP)(0.41),((TR)Q)(0.59)(SP) (0.41), ((TR)Q) (0.59).
  4. Combine (SP)(0.41)(SP) (0.41) and ((TR)Q)(0.59)((TR)Q) (0.59) to form the root with probability 1.001.00.

Determine the length ℓi\ell_i of each character's codeword (its depth in the tree):

  • SS: depth 2 (in SPSP)   ⟹  ℓ(S)=2\implies \ell(S) = 2 bits
  • PP: depth 2 (in SPSP)   ⟹  ℓ(P)=2\implies \ell(P) = 2 bits
  • QQ: depth 2 (in (TR)Q(TR)Q)   ⟹  ℓ(Q)=2\implies \ell(Q) = 2 bits
  • RR: depth 3 (in TRTR inside (TR)Q(TR)Q)   ⟹  ℓ(R)=3\implies \ell(R) = 3 bits
  • TT: depth 3 (in TRTR inside (TR)Q(TR)Q)   ⟹  ℓ(T)=3\implies \ell(T) = 3 bits

Compute the expected length per character (LL):

L=∑piℓi=(0.19+0.22+0.34)×2+(0.17+0.08)×3L = \sum p_i \ell_i = (0.19 + 0.22 + 0.34) \times 2 + (0.17 + 0.08) \times 3
L=0.75×2+0.25×3=1.50+0.75=2.25 bits/characterL = 0.75 \times 2 + 0.25 \times 3 = 1.50 + 0.75 = 2.25 \text{ bits/character}

For a message of 100 characters:

Expected total length=100×2.25=225 bits\text{Expected total length} = 100 \times 2.25 = 225 \text{ bits}
Final answer

225

Common trap

Forgetting to multiply the per-character expected length (2.25 bits) by the message length of 100 characters.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 2
Question
50 of 65, printed as CS Q.50
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.