CS Atlas GATE 2027
Algorithms course
0 / 79 complete

Algorithms/Phase 4 · Divide & Conquer and Greedy

Lesson 4.2.4 · Item 48 of 79

Optimal merge pattern; when greedy fails

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

1 · The Hook

🎣

Five sorted files must be merged into one, two at a time, and each merge costs the total size of the two files. The order matters: merging big files early makes their records move again and again. The cheapest order is Huffman's rule again. And some tempting greedy rules fail completely; GATE expects you to recognise which.

2 · Predict First

🤔

Files of sizes 20, 30, 10, 5, 30 records. Always merge the two smallest. What is the total cost (records moved)?

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

5 + 10 = 15; 15 + 20 = 35; 30 + 30 = 60; 35 + 60 = 95. Total 15 + 35 + 60 + 95 = 205.

Optimal merge pattern

Merging two sorted files of sizes a and b costs a + b. To merge n files into one with minimum total cost, repeatedly merge the two smallest (a min-heap): exactly Huffman's algorithm, with sizes as frequencies. Total cost = sum of merge sizes = Σ size × (number of merges it takes part in).

Where greedy fails

ProblemTempting greedyCounterexample
0/1 knapsackbest value/weightW 50; (60, 10), (100, 20), (120, 30): greedy 160, optimal 220
Coin change (min coins)largest coin firstcoins {1, 3, 4}, amount 6: greedy 4 + 1 + 1 (3 coins), optimal 3 + 3 (2 coins)
Coin changelargest coin firstcoins {1, 5, 6, 9}, amount 11: greedy 9 + 1 + 1 (3), optimal 5 + 6 (2)
Shortest pathnearest next vertexa short first edge can lead to a long detour
Activity selectionshortest first(1, 5), (4, 7), (6, 10)

Greedy coin change is optimal for "canonical" systems such as {1, 2, 5, 10, 20, 50, 100} (Indian rupees, US coins), but not for arbitrary coin sets. The general problems need dynamic programming (Phase 5).

Greedy algorithms still to come

Kruskal's and Prim's MST algorithms and Dijkstra's shortest paths (Phase 6) are greedy, each with its own correctness proof.

4 · Visual

Optimal merge of 20, 30, 10, 5, 30 (merge the two smallest each time)

  5 + 10 -> 15
 15 + 20 -> 35
 30 + 30 -> 60
 35 + 60 -> 95          total cost = 15 + 35 + 60 + 95 = 205

5 · Worked Examples

Example 1 Easy

Optimal merge cost of files of sizes 2, 3, 5, 7, 11, 13?

Solution

5, 10, 17, 24, 41 → 97.

Example 2 Medium

Coins {1, 3, 4}, amount 6: how many coins does largest-first use, and what is the minimum?

Solution

Greedy 4 + 1 + 1 = 3; optimal 3 + 3 = 2.

Example 3 GATE

Why does the optimal merge pattern use a min-heap?

Solution

To extract the two smallest sizes and insert their sum in O(log n) each: total Θ(n log n).

6 · GATE Traps

⚠️

  • Merge cost counts the merged sizes at every step, not the final size once.
  • Largest-coin-first is optimal only for canonical coin systems.
  • Optimal merge = Huffman with sizes as frequencies.
  • A greedy rule that works on examples still needs a proof.

7 · Memory Hook

🧠

"Small with small first; and check greedy before trusting it."

8 · Connect the Dots

Phase 4 is complete: divide and conquer and greedy. When greedy fails (0/1 knapsack, coin change) and subproblems overlap, the tool is dynamic programming, Phase 5.

9 · Self-Test

GATE format. Try every question before opening the vault.

Q1NAT2 marksOptimal merge

Optimal total cost of merging sorted files of sizes 20, 30, 10, 5, 30?

Numerical answer: ________

Q2NAT2 marksMerge pattern

Optimal total cost of merging files of sizes 8, 4, 6, 12?

Numerical answer: ________

Q3NAT1 markGreedy coins

Coins {1, 5, 6, 9}, amount 11: how many coins does largest-coin-first use?

Numerical answer: ________

Q4NAT1 markOptimal coins

Coins {1, 5, 6, 9}, amount 11: minimum number of coins?

Numerical answer: ________

Q5MSQ2 marksGreedy correctness

For which problems is a greedy algorithm optimal?

  • (A) Activity selection by earliest finish
  • (B) Huffman coding
  • (C) Fractional knapsack
  • (D) 0/1 knapsack

10 · Rule Card

  • Optimal merge: repeatedly merge the two smallest (min-heap); cost = Σ merges; = Huffman.
  • Greedy fails: 0/1 knapsack, coin change with non-canonical coins, shortest-first activities, nearest-neighbour paths.
  • Canonical coin systems: greedy works.

11 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

optimal merge patternmerge costnon-canonical coin systemsgreedy counterexamplescanonical coin systems

12 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • The merge weights.
  • The latest free slot ≤ its deadline.
  • Θ(p + q) (at most p + q − 1 comparisons).

Self-Test

Q1 Answer: 2052 marks

15 + 35 + 60 + 95.

Re-read: Lesson 4.2.4 — Optimal merge pattern; when greedy fails

Q2 Answer: 582 marks

4 + 6 = 10; 8 + 10 = 18; 12 + 18 = 30; 10 + 18 + 30 = 58.

Re-read: Lesson 4.2.4 — Optimal merge pattern; when greedy fails

Q3 Answer: 31 mark

9 + 1 + 1.

Re-read: Lesson 4.2.4 — Optimal merge pattern; when greedy fails

Q4 Answer: 21 mark

5 + 6.

Re-read: Lesson 4.2.4 — Optimal merge pattern; when greedy fails

Q5 Answer: (A) (B) (C)2 marks

0/1 knapsack needs DP.

  • (D) is wrong: Greedy fails.

Re-read: Lesson 4.2.4 — Optimal merge pattern; when greedy fails

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.