GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 26

GATE CSE 2021 Set 2 Q26

Algorithms · Greedy Algorithms · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q26 2 marks · MCQ
Report issue

Greedy Algorithms

Question 26, GATE 2021: Encode abbccddeee with a binary prefix code. Among equal-frequency letters, earlier dictionary letters must have code length no greater than later ones. Find minimum encoded length. A 21; B 23; C 25; D 30.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The frequencies are 1, 2, 2, 2, 3. Huffman merging costs are 3, 4, 6, 10, totaling 23 bits, the optimal weighted path length. The dictionary tie rule can also be met: assign b=00, c=01, e=10, a=110, d=111. Then lengths of b, c, d are 2, 2, 3, and total length is 3+4+4+6+6=23. Thus B.

Huffman repeatedly merges the two smallest available weights: 1+2=3; 2+2=4; 3+3=6; 4+6=10. Each merge adds its weight once to the encoded length because all leaves below it gain one bit. The displayed code is prefix-free and satisfies the alphabetical tie restriction, so the unrestricted Huffman lower bound is attainable with the extra condition.

Original paper, page 67 · Answer key