This question is based on the concept of Optimal Merge Pattern (OMP), which is actually the same greedy logic used in Huffman coding.
We are given 5 files:
- A = 10 records
- B = 20 records
- C = 15 records
- D = 5 records
- E = 25 records
We want to merge them into one file with the minimum number of record movements.
The cost of merging two files = sum of their sizes (since every record must be copied once).
Optimal Merge Pattern
The greedy rule:
- Always merge the two smallest files first.
- Put the merged file back into the set.
- Repeat until only one file remains.
It ensures larger files are not copied repeatedly
Similarity with Huffman coding
Huffman coding works in the exact same way:
- Always combine the two smallest frequency symbols.
- Make a parent node with weight equal to the sum of the two.
- Repeat until a single root remains.
The total weighted path length of the Huffman tree = minimum cost of encoding.
So in both cases:
- File sizes ↔ frequencies
- Merge cost ↔ parent node weight
- Total movements ↔ total weighted path length
This is why the mathematics is identical.
Given question:
Files: 5, 10, 15, 20, 25
Steps:
1. Merge 5 + 10 = 15 (cost = 15)
2. Merge 15 + 15 = 30 (cost = 30)
3. Merge 20 + 25 = 45 (cost = 45)
4. Merge 30 + 45 = 75 (cost = 75)
Total cost = 15 + 30 + 45 + 75 = 165