Skip to content
Official PYQ · 2007

GATE CS 2007 Q77 — Algorithm Design: Greedy Techniques

Explain with AI

Suppose the letters a, b, c, d, e, fa, \,b, \,c, \,d, \,e, \,f have probabilities 12,14,18,116,132,132\dfrac{1}{2}, \dfrac{1}{4}, \dfrac{1}{8}, \dfrac{1}{16}, \dfrac{1}{32}, \dfrac{1}{32}, respectively. 

What is the average length of the Huffman code for the letters a, b, c, d, e, fa, \,b, \,c, \,d, \,e, \,f?

Choose one option
View worked solution

Answer: D

To find the average length of the Huffman code, we first construct the Huffman tree by repeatedly merging the two nodes with the smallest probabilities:

  1. Initial probabilities: a:1632a: \frac{16}{32}, b:832b: \frac{8}{32}, c:432c: \frac{4}{32}, d:232d: \frac{2}{32}, e:132e: \frac{1}{32}, f:132f: \frac{1}{32}
  2. Merge ee and ff (each 132\frac{1}{32}) to form parent node efef with probability 232\frac{2}{32}.
  3. Merge dd (232\frac{2}{32}) and efef (232\frac{2}{32}) to form parent node defdef with probability 432\frac{4}{32}.
  4. Merge cc (432\frac{4}{32}) and defdef (432\frac{4}{32}) to form parent node cdefcdef with probability 832\frac{8}{32}.
  5. Merge bb (832\frac{8}{32}) and cdefcdef (832\frac{8}{32}) to form parent node bcdefbcdef with probability 1632\frac{16}{32}.
  6. Merge aa (1632\frac{16}{32}) and bcdefbcdef (1632\frac{16}{32}) to form the root with probability 11.

From this tree, the code lengths (lil_i) for each letter are:

  • aa: 11 bit
  • bb: 22 bits
  • cc: 33 bits
  • dd: 44 bits
  • ee: 55 bits
  • ff: 55 bits

The average code length (LL) is calculated as:

L=∑(pi×li)=(12×1)+(14×2)+(18×3)+(116×4)+(132×5)+(132×5)L = \sum (p_i \times l_i) = \left(\frac{1}{2} \times 1\right) + \left(\frac{1}{4} \times 2\right) + \left(\frac{1}{8} \times 3\right) + \left(\frac{1}{16} \times 4\right) + \left(\frac{1}{32} \times 5\right) + \left(\frac{1}{32} \times 5\right) L=1632+1632+1232+832+532+532=6232=1.9375L = \frac{16}{32} + \frac{16}{32} + \frac{12}{32} + \frac{8}{32} + \frac{5}{32} + \frac{5}{32} = \frac{62}{32} = 1.9375

Thus, the average length of the Huffman code is 1.93751.9375.

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

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