Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q50 — Algorithm Design: Greedy Techniques

Explain with AI
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:CharacterProbability P0.22Q0.34R0.17S0.19T0.08Total1.00\begin{array}{|c|c|}\hline \textbf{Character} & \textbf{Probability } \\ \hline \text{$P$} & \text{$0.22$} \\ \text{$Q$} & \text{$0.34$} \\ \text{$R$} & \text{$0.17$} \\ \text{$S$} & \text{$0.19$} \\ \text{$T$} & \text{$0.08$} \\ \hline\text{Total} & \text{$1.00$} \\ \hline \end{array}If a message of 100100 characters over XX is encoded using Huffman coding, then the expected length of the encoded message in bits is ______.
View worked solution

Answer: 225

To find the expected length of the encoded message, we first construct the optimal prefix-free binary tree using Huffman Coding.

Step 1: List the characters and their probabilities

Arrange the characters in ascending order of their 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

Step 2: Construct the Huffman Tree

  1. Combine the two smallest probabilities: T(0.08)T (0.08) and R(0.17)R (0.17).
    • New internal node (TR)=0.08+0.17=0.25(TR) = 0.08 + 0.17 = 0.25.
    • Remaining set: 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 next two smallest: S(0.19)S (0.19) and P(0.22)P (0.22).
    • New internal node (SP)=0.19+0.22=0.41(SP) = 0.19 + 0.22 = 0.41.
    • Remaining set: (TR)(0.25),Q(0.34),(SP)(0.41)(TR)(0.25), Q(0.34), (SP)(0.41).
  3. Combine the next two smallest: (TR)(0.25)(TR) (0.25) and Q(0.34)Q (0.34).
    • New internal node (TRQ)=0.25+0.34=0.59(TRQ) = 0.25 + 0.34 = 0.59.
    • Remaining set: (SP)(0.41),(TRQ)(0.59)(SP)(0.41), (TRQ)(0.59).
  4. Combine the final two nodes: (SP)(0.41)(SP) (0.41) and (TRQ)(0.59)(TRQ) (0.59).
    • Root node =0.41+0.59=1.00= 0.41 + 0.59 = 1.00.

Step 3: Determine the Code Lengths

By tracing the paths from the root to each leaf node, we find the length (number of bits) for each character:

  • P (from root →\to SPSP →\to PP): 22 bits
  • Q (from root →\to TRQTRQ →\to QQ): 22 bits
  • R (from root →\to TRQTRQ →\to TRTR →\to RR): 33 bits
  • S (from root →\to SPSP →\to SS): 22 bits
  • T (from root →\to TRQTRQ →\to TRTR →\to TT): 33 bits

Step 4: Calculate the Expected Length

The expected length of a single character (LL) is given by:

L=∑(Probability of character×Code length of character)L = \sum (\text{Probability of character} \times \text{Code length of character}) L=(0.22×2)+(0.34×2)+(0.17×3)+(0.19×2)+(0.08×3)L = (0.22 \times 2) + (0.34 \times 2) + (0.17 \times 3) + (0.19 \times 2) + (0.08 \times 3) L=0.44+0.68+0.51+0.38+0.24=2.25 bits/characterL = 0.44 + 0.68 + 0.51 + 0.38 + 0.24 = 2.25 \text{ bits/character}

For a message containing 100100 characters, the expected length of the entire encoded message is:

Expected Length=100×2.25=225 bits\text{Expected Length} = 100 \times 2.25 = 225 \text{ bits}

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Algorithm Design: Greedy Techniques questions