GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 2.20

Optimal merge cost

Algorithms · Greedy Algorithms · Full GATE CSE 1999 paper

GATE 1999, Q2.20 2 marks · MSQ
Report issue

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