Skip to content
Official PYQ · 2007

GATE CS 2007 Q76 — 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\frac{1}{2}, \frac{1}{4}, \frac{1}{8}, \frac{1}{16}, \frac{1}{32}, \frac{1}{32}, respectively. 

Which of the following is the Huffman code for the letter a, b, c, d, e, fa, \,b, \,c, \,d, \,e, \,f?

Choose one option
View worked solution

Answer: A

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

  1. Initial probabilities: a:12a: \frac{1}{2}, b:14b: \frac{1}{4}, c:18c: \frac{1}{8}, d:116d: \frac{1}{16}, 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 116\frac{1}{16}.
  3. Merge dd (116\frac{1}{16}) and efef (116\frac{1}{16}) to form parent node defdef with probability 18\frac{1}{8}.
  4. Merge cc (18\frac{1}{8}) and defdef (18\frac{1}{8}) to form parent node cdefcdef with probability 14\frac{1}{4}.
  5. Merge bb (14\frac{1}{4}) and cdefcdef (14\frac{1}{4}) to form parent node bcdefbcdef with probability 12\frac{1}{2}.
  6. Merge aa (12\frac{1}{2}) and bcdefbcdef (12\frac{1}{2}) to form the root node with probability 11.

Assigning 0 to the left branch and 1 to the right branch at each step of the tree yields the following prefix codes:

  • aa: 00
  • bb: 1010
  • cc: 110110
  • dd: 11101110
  • ee: 1111011110
  • ff: 1111111111

This matches the code sequence given in Option A.

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

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