GateAI
Greedy Algorithms

GATE CSE 2021 Set 2 · Q26

MCQ2 marksMedium

Time

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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 23

Greedy Algorithms

First, calculate the frequency of each character in the string abbccddeee:

  • a:1a: 1

  • b:2b: 2

  • c:2c: 2

  • d:2d: 2

  • e:3e: 3

Property 1 requires the binary code to be prefix-free, which can be represented as a binary tree with characters as leaves.

Property 2 states that for letters with identical frequencies, earlier letters in alphabetical order must have a code length less than or equal to later letters. The letters with the same frequency are b,c,db, c, d (each having frequency 22). Since the alphabetical order is b<c<db < c < d, their code lengths must satisfy:

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

To minimize the total encoded length:

  • An optimal prefix code tree has leaf depths of either 22 or 33.

  • Specifically, we can have three leaves at depth 22 and two leaves at depth 33, satisfying Kraft's inequality:

3×2−2+2×2−3=34+28=13 \times 2^{-2} + 2 \times 2^{-3} = \frac{3}{4} + \frac{2}{8} = 1

To minimize the weighted path length while satisfying length(b)≤length(c)≤length(d)\text{length}(b) \le \text{length}(c) \le \text{length}(d):

  • Assign length 22 to the highest frequency symbol ee.

  • Assign length 22 to bb and cc, and length 33 to dd, which satisfies length(b)=2≤length(c)=2≤length(d)=3\text{length}(b) = 2 \le \text{length}(c) = 2 \le \text{length}(d) = 3.

  • Assign length 33 to aa.

Now, calculate the total encoded string length:

Total Length=1(la)+2(lb)+2(lc)+2(ld)+3(le)\text{Total Length} = 1(l_a) + 2(l_b) + 2(l_c) + 2(l_d) + 3(l_e)
Total Length=1(3)+2(2)+2(2)+2(3)+3(2)=3+4+4+6+6=23\text{Total Length} = 1(3) + 2(2) + 2(2) + 2(3) + 3(2) = 3 + 4 + 4 + 6 + 6 = 23

Thus, the minimum length of the encoded string is 23 (Option B).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Greedy Algorithms previous year question is Q26 of the GATE CSE 2021 Set 2 paper. Find more GATE CSE PYQs on Greedy Algorithms in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 1 related Greedy Algorithms PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Greedy Algorithms in GATE CSE

GATE CSE 2021 Set 2 Q26 is the only Greedy Algorithms question in the GATE CSE papers so far.

How GATE tests Greedy Algorithms

More from GATE CSE 2021 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 2 · Q26 · MCQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.