Sub topic 18
Choosing Between Greedy and Dynamic Programming in GATE Questions
Prerequisites and the central decision
Assume you know Big-O notation, basic recurrences, arrays, and the standard greedy and dynamic programming algorithms.
Both greedy algorithms and dynamic programming (DP) solve optimization problems by using smaller decisions. The important difference is how they commit to those decisions:
- A greedy algorithm makes the best-looking local choice and never revises it.
- A dynamic programming algorithm compares alternatives and stores results for repeated subproblems.
A problem may have optimal substructure, meaning an optimal solution contains optimal solutions to smaller instances. This property is necessary for both methods, but it does not prove that greedy is safe. Greedy also needs the greedy-choice property: there is an optimal solution that begins with the greedy choice.
A decision process for GATE questions
Use this sequence when examining a problem:
- Identify the objective and the allowed choices.
- Ask whether one local choice can be proved safe for every remaining instance.
- Look for an exchange argument. Can an optimal solution be changed to use the greedy choice without becoming worse?
- If no such proof is apparent, define a state and recurrence for DP.
- Check whether subproblems overlap. If they do, memoization or tabulation may reduce repeated work.
The following diagram summarizes the decision.
Recognizing common patterns
| Problem pattern | Typical choice | Main complexity |
|---|---|---|
| Activity selection | Earliest finishing activity | O(n log n) after sorting |
| Fractional knapsack | Largest value-to-weight ratio | O(n log n) |
| 0/1 knapsack | Compare taking and skipping each item | O(nW) |
| Coin change with arbitrary denominations | Compare several previous states | O(nA) for many formulations |
In activity selection, choosing the activity with the earliest finish leaves the largest possible remaining interval. An exchange argument replaces the first activity in an optimal schedule with this choice. Therefore, greedy works.
Fractional knapsack also permits greedy selection because an item can be divided. Taking the greatest value-to-weight ratio first is safe. In 0/1 knapsack, an item must be taken whole or skipped. The same ratio rule can fail, so DP evaluates both possibilities.
Comparing complexity and correctness
Greedy algorithms often use less memory and have simpler proofs. Their running time is commonly dominated by sorting. DP uses a state table, so its cost depends on the number of states and transitions. For 0/1 knapsack, the usual O(nW) bound is pseudo-polynomial because it depends on the numeric capacity W, not on the number of bits needed to represent W.
For an exam question, state both the algorithm and its justification. “Greedy because it chooses the largest ratio” is incomplete. State why the choice is safe, or give a counterexample showing why DP is required.
Exercise
Why is the value-to-weight ratio rule valid for fractional knapsack but not generally for 0/1 knapsack?
Show answer
Fractional knapsack allows an item to be divided, so replacing lower-ratio material with higher-ratio material never reduces value. In 0/1 knapsack, items cannot be divided, so a locally best ratio can prevent a better combination of whole items.