Greedy Algorithm
Updated 2026-07-28
Summary
A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. For example, in the traveling salesman problem, if the salesman always chooses the nearest city, that is a greedy algorithm. Greedy algorithms are particularly effective for problems with optimal substructure, meaning that local optimal solutions can determine the global optimal solution.
Definition
A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. For example, in the traveling salesman problem, if the salesman always chooses the nearest city, that is a greedy algorithm. Greedy algorithms are particularly effective for problems with optimal substructure, meaning that local optimal solutions can determine the global optimal solution. Simply put, the problem can be broken down into subproblems, and the optimal solutions to subproblems can be recursively extended to the optimal solution of the overall problem. The difference between greedy algorithms and dynamic programming is that greedy algorithms make a choice for each subproblem and cannot backtrack, whereas dynamic programming saves previous computation results and uses them to make current choices, allowing backtracking. Greedy methods can solve some optimization problems, such as finding the minimum spanning tree in a graph and Huffman coding. For other problems, greedy methods generally cannot produce the desired answer. Once a problem can be solved by a greedy method, it is usually the best approach.
Introduction
How to use
A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. For example, in the traveling salesman problem, if the salesman always chooses the nearest city, that is a greedy algorithm. Greedy algorithms are particularly effective for problems with optimal substructure, meaning that local optimal solutions can determine the global optimal solution.
Mechanism
A greedy algorithm makes the locally optimal decision at each step, expecting to achieve a global optimum. It is efficient but does not guarantee optimality; it is only valid for problems that satisfy the greedy choice property and optimal substructure.
Practice
In progress 0/1Practice this model in one real situation:
Cases
- Huffman coding uses a greedy approach to construct optimal prefix codes. Kruskal's and Prim's algorithms for minimum spanning trees, as well as Dijkstra's shortest path algorithm, are all greedy.
Common pitfalls
- Assuming that local optimality necessarily implies global optimality, leading to misuse in problems like the knapsack problem. Failing to verify the greedy property, resulting in only approximate solutions.
Sources
Source support: Explicit
References currently contain only Wikipedia links; primary sources still need review.
- zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95verified
account_treeGenealogyexpand_more
PRIVATE NOTES · Only visible to you
SAVED Q&A
ENTRY Q&A · Private saving available
Ask with a clear boundary
thinkingmodels answers from published entry context only.
Your question is sent to thinkingmodels. The answer uses public entry context only.
RELATED MODELS