GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 38

GATE CSE 2014 Set 2 Q38

Algorithms · Greedy Algorithms · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q38 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