Skip to main content

GATE CSE 2021Algorithms › Greedy Method, Dynamic Programming & Algorithm Design TechniquesVerified

GATE CSE 2021 PYQ: Consider the string abbccddeee \textrm{abbccddeee} abbccddeee . Each letter in the string must be assigned a…

Consider the string abbccddeee\textrm{abbccddeee}. Each letter in the string must be assigned a binary code satisfying the following properties:

  1. For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter.
  2. For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter.

Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?

  1. A
    21
  2. B
    23
    Correct answer
  3. C
    25
  4. D
    30

Answer

(B) 23 23 23

Approach

Use the Huffman-coding lower bound: any prefix-free code must have weighted length at least the Huffman cost for the given frequencies. If that lower bound can be achieved while obeying the equal-frequency length-ordering condition, it is the minimum.

The Huffman lower bound for the given frequencies is 23, and a valid prefix code achieving this bound exists while satisfying the equal-frequency ordering condition. Therefore the minimum encoded length is 23.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.