GATE / CS ARCHIVE

SUBJECT COLLECTION

Algorithms · Greedy Algorithms

10 questions, newest first

GATE CSE has asked 10 questions on Greedy Algorithms (Algorithms) from 1999 to 2021, in 8 of the 46 papers; the most recent is in GATE CSE 2021 Set 2. In the last five years it carried 0 marks per 100-mark paper on average, against 0.4 since 2003. Since 2014 the questions have been 3 NAT and 1 MCQ. 2 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
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

2 marks · NAT
Report issue

Greedy versus optimal 0/1 knapsack

Greedy Algorithms

Question 48, GATE 2018: Capacity is 11 kg. Single available items (weight,value) are 1:(10,60),2:(7,28),3:(4,20),4:(2,24). Items cannot split. Greedy scans descending value/weight, taking items that fit. Find optimal value minus greedy value.
Tap to enlarge
Answer & solution

Official answer: 16

Step through it (2 tables)

Method

The ratio order is item 4 then 1 then 3 then 2. Taking item 4 leaves 9 kg, so item 1 no longer fits. Item 3 then fits and leaves 5 kg, too little for item 2. To verify optimality, item 1 alone gives 60 and cannot pair with even the smallest other item. Without item 1, feasible pairs (2,3),(2,4),(3,4) have values 48,52,44; all three weigh 13 and are infeasible. Thus exhaustive feasible alternatives confirm the optimum.

Solution

Value/weight ratios order the items as 4, 1, 3, 2, with ratios 12, 6, 5, 4. Greedy takes item4 (weight 2, value 24), skips item1 because it no longer fits, then takes item3 (weight 4, value 20), and skips item2. Its value is 44. The best feasible subset is item1 alone, value 60: pairs among 2, 3, 4 have maximum 52, all three weigh13, and item1 cannot accompany any other item under capacity 11. Hence60−44=16.

Original paper, page 20 · Answer key

2 marks · NAT
Report issue

Greedy Algorithms

Question 50, GATE 2017: A message uses symbols P,Q,R,S,T with probabilities 0.22,0.34,0.17,0.19,0.08 respectively. For a 100-character message encoded with binary Huffman coding, what is the expected encoded length in bits?
Tap to enlarge
Answer & solution

Official answer: 225.0

Our answer: 225

1. Huffman coding repeatedly merges the two smallest current weights. Merge T and R:0.08+0.17=0.25. Next merge S and P:0.19+0.22=0.41. The remaining weights are 0.25,0.34,0.41; merge 0.25+0.34=0.59, then 0.41+0.59=1.

2. The resulting depths are 2 for P,Q,S and 3 for R,T. One valid code assignment is S=00,P=01,T=100,R=101,Q=11; swapping branch labels changes codewords but not lengths.

3. Expected bits per symbol are 2(0.22+0.34+0.19)+3(0.17+0.08)=1.5+0.75=2.25. Equivalently sum the merge weights 0.25+0.41+0.59+1=2.25. For 100 symbols the expectation is 225 bits, excluding codebook/container overhead. It is an expectation, not a guarantee for every possible 100-symbol sequence.

Original paper, page 20 · Answer key

2 marks · NAT
Report issue

Greedy Algorithms

Question 38, GATE 2014: Sorted sequences P,Q,R,S,T have lengths 20,24,30,35,50. Merge two sequences at a time until one remains. Find the minimum possible worst-case total number of comparisons.
Tap to enlarge
Answer & solution

Official answer: 358

1. Set up the reasoning. A merge of lengths a and b needs a+b-1 comparisons in the worst case. The optimal merge-pattern rule repeatedly joins the two currently shortest sequences, reinserting the combined length.

2. Derive the result. Optimal merging repeatedly combines the two shortest lists: 20+24=44, 30+35=65, 44+50=94, 65+94=159. A merge of lengths a, b uses at most a+b−1 comparisons, so total 43+64+93+158=358. The shortest-pair rule minimizes weighted leaf depth, as in Huffman coding.

3. Check the interpretation. Track lengths after each merge: 44 joins the queue after 20 and 24; 65 after 30 and 35; then combine 44 and 50. Subtract one comparison for each of the four merges, not just once from the final length.

Original paper, page 39 · Answer key

2 marks · MCQ
Report issue

Huffman code for dyadic probabilities

Greedy Algorithms

Question 76, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. Which list, in a,b,c,d,e,f order, is a Huffman code?
Options:
(A) 0,10,110,1110,11110,11111
(B) 11,10,011,010,001,000
(C) 11,10,01,001,0001,0000
(D) 110,100,010,000,001,111
Question 76, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. Which list, in a,b,c,d,e,f order, is a Huffman code?
Options:
(A) 0,10,110,1110,11110,11111
(B) 11,10,011,010,001,000
(C) 11,10,01,001,0001,0000
(D) 110,100,010,000,001,111
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. Repeatedly merge the two lowest-probability nodes. Code length is leaf depth in the resulting binary tree; high-probability a should get a one-bit word. Prefix-free alone is not enough to guarantee optimality.

2. Derivation and interpretation. Huffman merging combines e and f (1/32 each) into 1/16, merges with d to 1/8, merges with c to 1/4, merges with b to 1/2, then merges with a. The resulting leaf depths for a,b,c,d,e,f are 1,2,3,4,5,5. Option A supplies a prefix code with these depths: 0,10,110,1110,11110,11111. Bit reversal at internal branches can give other equivalent codes, but A is the listed optimal code.

Original paper, page 19

2 marks · MCQ
Report issue

Average Huffman code length

Greedy Algorithms

Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Question 77, GATE 2007: Letters a,b,c,d,e,f occur with probabilities 1/2,1/4,1/8,1/16,1/32,1/32, respectively. What is the average length of an optimal binary Huffman code (the linked Q76 code)? Derive the code lengths from these probabilities.
Options:
(A) 3
(B) 2.1875
(C) 2.25
(D) 1.9375
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. Average code length is probability-weighted, not the arithmetic mean of the six word lengths. Huffman merging gives lengths 1,2,3,4,5,5, with the shortest code assigned to the most frequent symbol.

2. Derivation and interpretation. Using the independently derived lengths 1,2,3,4,5,5, the weighted average is (1/2)*1+(1/4)*2+(1/8)*3+(1/16)*4+(1/32)*5+(1/32)*5 = 31/16 = 1.9375 bits per symbol. Answer D.

Original paper, page 19

2 marks · MCQ
Report issue

Profitable unit-time jobs before deadlines

Greedy Algorithms

Question 84a, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(a): in a maximum-profit schedule A all jobs done; B T1, T6 omitted; C T1, T8 omitted; D T4, T6 omitted.
Question 84a, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(a): in a maximum-profit schedule A all jobs done; B T1, T6 omitted; C T1, T8 omitted; D T4, T6 omitted.
Question 84a, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(a): in a maximum-profit schedule A all jobs done; B T1, T6 omitted; C T1, T8 omitted; D T4, T6 omitted.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Order jobs by descending profit and put each in the latest free slot not exceeding its deadline. One resulting schedule in slots 1…7 is T2, T7, T9, T5, T3, T8, T1. Their deadlines are respectively 2, 2, 3, 4, 5, 7, 7, so all meet their deadlines. T4 and T6 are omitted, option D. In particular T2, T4, T7, T9 all have deadline≤3, so at least one of them must be omitted; T4 has their lowest profit. Only seven slots exist through the latest deadline, requiring another omission, optimally T6.

The greedy unit-job method processes larger profits first and places each job as late as its deadline permits, leaving earlier slots for tighter deadlines. It yields T2, T7, T9, T5, T3, T8, T1. Check every slot against its deadline:1≤2, 2≤2, 3≤3, 4≤4, 5≤5, 6≤7, 7≤7. T4 and T6 are absent. The three-slot deadline bottleneck among T2, T4, T7, T9 explains why a job must be omitted even before considering total capacity.

Original paper, page 12

2 marks · MCQ
Report issue

Maximum deadline-scheduling profit

Greedy Algorithms

Question 84b, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(b): maximum profit A147; B165; C167; D175.
Question 84b, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(b): maximum profit A147; B165; C167; D175.
Question 84b, GATE 2005: Nine unit-time jobs T1…T9, one at a time, earn profits[15, 20, 30, 18, 18, 10, 23, 16, 25] if finished by deadlines[7, 2, 5, 3, 4, 5, 2, 7, 3], respectively. Printed 84(b): maximum profit A147; B165; C167; D175.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

The schedule T2, T7, T9, T5, T3, T8, T1 earns 20+23+25+18+30+16+15=147. All nine profits sum to 175. At least one of the four jobs with deadline≤3 must be dropped, losing at least 18, and at least one additional job must be dropped since the latest deadline is 7, losing at least 10. Therefore profit≤175−18−10=147, attained above. Option A.

Sum the feasible schedule’s seven profits to obtain 147. For optimality, the four jobs with deadline at most 3 cannot all fit in three slots, requiring a loss of at least 18. At most seven jobs can finish by the latest deadline 7, so another distinct job must be omitted, losing at least 10. Subtract these unavoidable losses from 175 to get upper bound 147, matched by the schedule. Thus the result is proven optimal rather than just a greedy candidate.

Original paper, page 12

2 marks · MCQ
Report issue

Room allocation from activity endpoints

Greedy Algorithms

Question 69, GATE 2003: Activities A,...,H must each occupy a room throughout their duration. Let xs and xe denote the start and finish of X. The complete GateForum chronological sequence is:
as,bs,cs,ae,ds,ce,es,fs,be,de,gs,ee,fe,hs,ge,he.
GFG instead prints the same sequence with ce omitted. What is the minimum number of rooms?
A. 3  B. 4  C. 5  D. 6
The omission is a material source discrepancy; do not silently insert ce into the GFG version.
Tap to enlarge
Your answer
Answer & solution

Answer: None (no official key was published)

1. For the complete sequence, count a start as +1 and a finish as −1. The successive active counts are 1,2,3,2,3,2,3,4,3,2,3,2,1,2,1,0. The maximum is four, at fs with B,D,E,F simultaneously active.
2. Four rooms suffice constructively: assign room 1 to A,D,G; room 2 to B,H; room 3 to C,E; room 4 to F. Every reuse occurs after that room's previous finish.
4. Without ce, C may or may not still be running at fs, so the the paper sequence can require five rather than four. Preserve the baseline unanswered status for the defective primary version rather than guessing the missing event.

The maximum overlap is four, attained after F starts while B,D,E,F are active. The the paper omits C's finishing event. The incomplete the paper sequence cannot determine a unique answer without supplying the missing endpoint.

Original paper, page 10

2 marks · MSQ
Report issue

Optimal merge cost

Greedy Algorithms

Question 2.20, GATE 1999: Merge file lengths 10,20,15,5,25 with pairwise merging; cost is records moved. Choose165,90,75,65.
Tap to enlarge
Select all that apply
Answer & solution

Answer: A: 165 (no official key was published)

Always merge the two shortest remaining files, as in the optimal merge/Huffman construction. Merge5+10=15, costing 15; then 15+15=30, costing 30; then 20+25=45, costing 45; finally 30+45=75, costing 75. Total cost is 15+30+45+75=165. The greedy choice is optimal because two smallest weights can occupy deepest sibling leaves of an optimal merge tree, after which contracting them leaves the same problem on fewer files. Counting only 75 ignores repeated moves through intermediate merges.

Original paper, page 5