Short answer: the greedy approach. A greedy algorithm builds a solution one step at a time and picks the option that looks best at that moment. It does not go back to revisit earlier choices. The hope is that a series of locally best choices adds up to a globally best result.
How the greedy approach works
Imagine making change with coins worth 1, 3, and 4 units to reach a total of 6. A greedy method takes the largest coin that still fits, so it picks 4, then 1, then 1. That is three coins. Two 3-unit coins would have done the job with fewer. The method made a sensible-looking choice at the first step and never reconsidered it.
Local optimum versus global optimum
A local optimum is the best option within a limited view, such as the best next step or the best nearby solution. A global optimum is the best solution overall. Greedy methods look only at local choices, so they can settle on a local optimum without ever seeing a better global one.
A hill-climbing picture helps. Suppose you are walking in fog and can only feel which direction slopes up. Each step uphill feels right, but you may end on a small hill while a taller peak lies elsewhere. That is the risk a greedy method takes.
When the greedy approach finds the best answer
For some problems, the locally best choice is always part of a best overall solution. Well-known examples include:
- Kruskal’s and Prim’s algorithms find a minimum spanning tree, which is the cheapest set of connections that links every node in a connected graph. Both are greedy, and both always produce an optimal result. Studying Prim’s algorithm vs Kruskal’s shows two different greedy routes to the same kind of answer.
- Dijkstra’s algorithm finds shortest paths from one starting node, as long as no edge has a negative weight. It repeatedly visits the closest unvisited node.
- Huffman coding builds a prefix code for compressing data by repeatedly merging the two least frequent symbols. It is greedy and produces an optimal prefix code for the symbol frequencies it is given.
When the greedy approach can miss
Greedy rules often fail on problems that need a broader view. The 0-1 knapsack problem is a good example. No simple greedy rule, such as always taking the most valuable item first, is guaranteed to fill the bag in the best way. A slightly less valuable item may fit better alongside the rest.
Greedy methods are still useful when they are not optimal. They are quick to write and often give good approximate answers, which is why they are common in practice.
How to check whether greedy is safe
Before you trust a greedy method, check two properties:
- Greedy choice property: a locally best choice can be part of some optimal solution.
- Optimal substructure: after making that choice, the leftover problem can be solved optimally, and the two parts combine into a best overall answer.
Proving both is the rigorous route. A quicker habit is to test a few small counterexamples. If you can find one where the greedy answer is worse, the method is not safe for that problem.
Greedy compared with other approaches
Brute force checks every possible answer. It always finds the best one, but it is slow for large problems. Greedy checks one option per step and commits to it. Readers sometimes ask, is brute force a greedy algorithm? It is not, because brute force never commits to an early choice at all.
Dynamic programming sits between these extremes. It stores answers to overlapping subproblems, so it can compare options that a greedy method would discard. Some problems that defeat greedy methods can be solved this way. For a fuller contrast of approaches, the divide and conquer algorithm vs dynamic programming comparison covers a related set of trade-offs.
Key takeaways
- The greedy approach makes the best-looking choice at each step and never revisits it.
- It aims for a global optimum but only sees local choices, so it can settle on a local optimum.
- Kruskal’s, Prim’s, Dijkstra’s (with non-negative weights), and Huffman coding are well-known greedy methods that reach optimal answers.
- Test a few small counterexamples before trusting a greedy rule on a new problem.