GATE CS 2021 Set 2 — Question 36
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67MediumHuffman CodingGreedy AlgorithmsAlgorithms
Algorithms → Greedy Algorithms → Huffman Coding
Last updated
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?Correct answer
(B) 23
Solution
First, determine the frequencies of each character in the string abbccddeee:
Property 2 states that for characters with the same frequency, the one earlier in alphabetical order must have a code length the one later. Here, b, c, and d all have frequency 2. So, .Constructing the Huffman tree for frequencies {1, 2, 2, 2, 3}:
To satisfy the property while maintaining minimum total length, we swap the codes of b and c (since they have the same frequency, the total length remains the same).
New lengths: . Still violates .
Optimal assignment of lengths {3, 3, 2, 2, 2} to {a, b, c, d, e} to satisfy :
.
Total length = .
- a: 1
- b: 2
- c: 2
- d: 2
- e: 3
Property 2 states that for characters with the same frequency, the one earlier in alphabetical order must have a code length the one later. Here, b, c, and d all have frequency 2. So, .Constructing the Huffman tree for frequencies {1, 2, 2, 2, 3}:
1.Combine 1(a) and 2(b) 3. Remaining: {2(c), 2(d), 3(e), 3(node1)}
2.Combine 2(c) and 2(d) 4. Remaining: {3(e), 3(node1), 4(node2)}
3.Combine 3(e) and 3(node1) 6. Remaining: {4(node2), 6(node3)}
4.Combine 4 and 6 10.
Code lengths from this tree:- a: 3 bits (node4 node3 node1 a)
- b: 3 bits (node4 node3 node1 b)
- e: 2 bits (node4 node3 e)
- c: 2 bits (node4 node2 c)
- d: 2 bits (node4 node2 d)
To satisfy the property while maintaining minimum total length, we swap the codes of b and c (since they have the same frequency, the total length remains the same).
New lengths: . Still violates .
Optimal assignment of lengths {3, 3, 2, 2, 2} to {a, b, c, d, e} to satisfy :
.
Total length = .
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Greedy Algorithms
2026 Set 1 Q17Consider the following recurrence relations: For all ,…2026 Set 2 Q24Consider the following functions, where is a positive integer.…2026 Set 2 Q25Which of the following can be recurrence relation(s) corresponding to an algorithm with time…2026 Set 2 Q32Consider an array . Suppose the merge sort algorithm is…2026 Set 2 Q37Let be a weighted directed acyclic graph with edges and vertices. Given and a…