Question

Consider the string 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?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2021 answer key

Solution

Analyze character frequencies, apply the lexicographic constraint for equal-frequency letters, and construct an optimal prefix tree under the given rules.

The string is abbccddeee, with character counts:

  • a:1a: 1
  • b:2b: 2
  • c:2c: 2
  • d:2d: 2
  • e:3e: 3
    Total length of the string is 1+2+2+2+3=101 + 2 + 2 + 2 + 3 = 10 characters.

Constraint 1 requires a prefix code (valid binary tree leaf assignment). Constraint 2 requires that for letters of equal frequency, earlier dictionary letters must have codeword length ≤\le later letters. Specifically, for the equal-frequency letters b,c,db, c, d (all frequency 2):

length(b)≤length(c)≤length(d)\text{length}(b) \le \text{length}(c) \le \text{length}(d)

Consider a standard Huffman tree without constraint 2: grouping a(1)+b(2)=3a(1)+b(2)=3, then c(2)+d(2)=4c(2)+d(2)=4, then e(3)+ab(3)=6e(3)+ab(3)=6, then cd(4)+eab(6)=10cd(4)+eab(6)=10. This assigns length 2 to c,d,ec, d, e and length 3 to a,ba, b. However, this gives length(b)=3>length(c)=2\text{length}(b) = 3 > \text{length}(c) = 2, violating constraint 2 because bb occurs before cc in dictionary order.

To satisfy length(b)≤length(c)≤length(d)\text{length}(b) \le \text{length}(c) \le \text{length}(d), consider binary tree structures with 5 leaves:
Using codeword lengths {2,2,2,3,3}\{2, 2, 2, 3, 3\} (which satisfies Kraft's inequality: 2−2+2−2+2−2+2−3+2−3=12^{-2} + 2^{-2} + 2^{-2} + 2^{-3} + 2^{-3} = 1):

  • Assign length 2 to the highest frequency letter ee (frequency 3).
  • Among b,c,db, c, d, to satisfy constraint 2, assign length 2 to bb and cc, and length 3 to dd:
length(b)=2≤length(c)=2≤length(d)=3\text{length}(b) = 2 \le \text{length}(c) = 2 \le \text{length}(d) = 3
  • The remaining leaf of length 3 is assigned to aa (frequency 1).

Calculate the total encoded length:

Total length=(1×3)+(2×2)+(2×2)+(2×3)+(3×2)\text{Total length} = (1 \times 3) + (2 \times 2) + (2 \times 2) + (2 \times 3) + (3 \times 2)
Total length=3+4+4+6+6=23\text{Total length} = 3 + 4 + 4 + 6 + 6 = 23

This satisfies both conditions and achieves the minimum possible length, matching option B.

Final answer

B

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2021 Computer Science and Information Technology - Session 2 (CS2)
Question
36 of 65, printed as CS Q.26
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.