Greedy Algorithms Pattern
Greedy Algorithms make locally optimal choices at each step with the hope of finding a global optimum. They don't reconsider previous choices and work well for optimization problems with certain properties.
When to Use This Pattern
- Problems with optimal substructure
- Problems with greedy choice property
- Interval scheduling problems
- Activity selection problems
- Minimum spanning tree problems
- Shortest path problems (Dijkstra's)
Key Concepts
Greedy Choice Property
A global optimum can be reached by making locally optimal choices. The choice made at each step is the best choice at that moment.
Optimal Substructure
An optimal solution contains optimal solutions to subproblems. This is similar to dynamic programming, but greedy algorithms don't store solutions to subproblems.