The greedy method and dynamic programming both work on optimization problems, where the goal is a best answer, such as the fewest coins or the lowest cost. They differ in how they make choices. The greedy method commits to the best local choice at each step and never revisits it. Dynamic programming looks at the answers to subproblems and combines them to find the best overall answer.
How the greedy method works
A greedy algorithm builds an answer one step at a time. At each step, it picks the option that looks best right now, and it does not go back to change that choice.
For this to produce the best overall answer, the problem needs the greedy-choice property. That means some locally best choice must belong to at least one optimal solution. Showing that a greedy choice is safe is usually the hardest part of designing a greedy algorithm.
How dynamic programming works
Dynamic programming breaks a problem into subproblems and works out the answer to each one, usually storing the answers in a table. It then picks the best answer by comparing options. It relies on optimal substructure, meaning the best answer to the whole problem is built from best answers to smaller parts. When the subproblems overlap, storing their answers avoids repeated work.
A coin change example where greedy fails
Suppose the coin values are 2, 6 and 8, and you need to make 12 with as few coins as possible.
- The greedy choice takes the largest coin that fits at each step. It takes 8, then 2, then 2, which is three coins in total.
- The best answer is 6 and 6, which uses two coins.
The 8 looked best at the first step, but it led to a worse total. Dynamic programming works out the fewest coins for every amount from 0 up to 12. The answer for 12 comes out to two coins, so it finds the better solution.
Where the greedy method works
Greedy methods are well known for several classic problems:
- Dijkstra’s algorithm for shortest paths, when edge weights are not negative.
- Kruskal’s and Prim’s algorithms for minimum spanning trees.
- Huffman coding, for building compact codes.
- The fractional knapsack problem, where items can be split into parts.
In each of these, the greedy choice can be proven safe. A useful pairing to compare is the dijkstra algorithm vs bellman ford question. Dijkstra’s greedy approach needs non-negative weights, while Bellman-Ford can handle negative weights. For minimum spanning trees, the greedy idea appears in Kruskal’s and Prim’s methods, and the comparison in prim’s algorithm vs kruskal explains how they differ.
Greedy choices can also serve as heuristics when a good answer is enough and the best one is not required. For background, see what are heuristics in computer science.
Where dynamic programming is needed
Dynamic programming handles problems where a greedy choice can fail:
- 0/1 knapsack, where each item must be taken whole or left out.
- Longest common subsequence, used to compare sequences such as text.
- Edit distance, the number of single-character changes that turn one string into another.
- Coin change with arbitrary coin values, as in the example above.
Speed and memory trade-offs
A greedy algorithm is often simpler to write, and it usually runs faster, since it makes one pass of decisions without tracking many options. Dynamic programming usually stores a table of answers, so it can use more memory, and it is often slower when the table is large. The trade-off is that dynamic programming can handle problems where a greedy choice is not safe, while greedy is only correct when its choice rule has been proven.
How to decide
- If you can show that the locally best choice always belongs to an optimal answer, a greedy method is a good fit.
- If you cannot, look for a small counterexample. The coin example above shows how quickly a greedy choice can go wrong.
- If the greedy choice can fail and the subproblems repeat, consider dynamic programming.
Summary
The greedy method commits to the best local choice at each step. When that choice is provably safe, the method is fast and simple. Dynamic programming checks answers to subproblems and combines them. It is often slower and uses more memory, but it handles cases where a greedy choice fails. In the coin example, greedy gives three coins, while dynamic programming finds the best answer of two.