# 贪心算法

Greedy algorithms determine minimum number of coins to give while making change. These are the steps a human would take to emulate a greedy algorithm to represent 36 cents using only coins with values {1, 5, 10, 20}. The coin of the highest value, less than the remaining change owed, is the local optimum. (In general the change-making problem requires dynamic programming to find an optimal solution; however, most currency systems, including the Euro and US Dollar, are special cases where the greedy strategy does find an optimal solution.)
Greedy algorithms determine minimum number of coins to give while making change. These are the steps a human would take to emulate a greedy algorithm to represent 36 cents using only coins with values {1, 5, 10, 20}. The coin of the highest value, less than the remaining change owed, is the local optimum. (In general the change-making problem requires dynamic programming to find an optimal solution; however, most currency systems, including the Euro and US Dollar, are special cases where the greedy strategy does find an optimal solution.)


A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage[1]. In many problems, a greedy strategy does not usually produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.

A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. In many problems, a greedy strategy does not usually produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.

For example, a greedy strategy for the travelling salesman problem (which is of a high computational complexity) is the following heuristic: "At each step of the journey, visit the nearest unvisited city." This heuristic does not intend to find a best solution, but it terminates in a reasonable number of steps; finding an optimal solution to such a complex problem typically requires unreasonably many steps. In mathematical optimization, greedy algorithms optimally solve combinatorial problems having the properties of matroids, and give constant-factor approximations to optimization problems with submodular structure.

For example, a greedy strategy for the travelling salesman problem (which is of a high computational complexity) is the following heuristic: "At each step of the journey, visit the nearest unvisited city." This heuristic does not intend to find a best solution, but it terminates in a reasonable number of steps; finding an optimal solution to such a complex problem typically requires unreasonably many steps. In mathematical optimization, greedy algorithms optimally solve combinatorial problems having the properties of matroids, and give constant-factor approximations to optimization problems with submodular structure.

## Specifics

In general, greedy algorithms have five components:

In general, greedy algorithms have five components:

1. A candidate set, from which a solution is created
A candidate set, from which a solution is created


1. A selection function, which chooses the best candidate to be added to the solution
A selection function, which chooses the best candidate to be added to the solution


1. A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
A feasibility function, that is used to determine if a candidate can be used to contribute to a solution


1. An objective function, which assigns a value to a solution, or a partial solution, and
An objective function, which assigns a value to a solution, or a partial solution, and


1. A solution function, which will indicate when we have discovered a complete solution
A solution function, which will indicate when we have discovered a complete solution


Greedy algorithms produce good solutions on some mathematical problems, but not on others. Most problems for which they work will have two properties:

Greedy algorithms produce good solutions on some mathematical problems, but not on others. Most problems for which they work will have two properties:

Greedy choice property
We can make whatever choice seems best at the moment and then solve the subproblems that arise later. The choice made by a greedy algorithm may depend on choices made so far, but not on future choices or all the solutions to the subproblem. It iteratively makes one greedy choice after another, reducing each given problem into a smaller one. In other words, a greedy algorithm never reconsiders its choices. This is the main difference from dynamic programming, which is exhaustive and is guaranteed to find the solution. After every stage, dynamic programming makes decisions based on all the decisions made in the previous stage, and may reconsider the previous stage's algorithmic path to solution.
Greedy choice property: We can make whatever choice seems best at the moment and then solve the subproblems that arise later. The choice made by a greedy algorithm may depend on choices made so far, but not on future choices or all the solutions to the subproblem.  It iteratively makes one greedy choice after another, reducing each given problem into a smaller one. In other words, a greedy algorithm never reconsiders its choices. This is the main difference from dynamic programming, which is exhaustive and is guaranteed to find the solution. After every stage, dynamic programming makes decisions based on all the decisions made in the previous stage, and may reconsider the previous stage's algorithmic path to solution.


Optimal substructure
"A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to the sub-problems."[2]

Optimal substructure: "A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to the sub-problems."

### Cases of failure

{{multiple image

{{multiple image

{多重图像

  | align =

  | align =


# align

| direction = vertical

| direction = vertical

| direction = vertical

  | width     = 300

  | width     = 300


300

  | header    = Examples on how a greedy algorithm may fail to achieve the optimal solution.

  | header    = Examples on how a greedy algorithm may fail to achieve the optimal solution.


  | image1    = Greedy Glouton.svg

  | image1    = Greedy Glouton.svg


1 = Greedy Glouton.svg

  | alt1      =

  | alt1      =


1 =

  | caption1  = Starting from A, a greedy algorithm that tries to find the maximum by following the greatest slope will find the local maximum at "m", oblivious to the global maximum at "M".

  | caption1  = Starting from A, a greedy algorithm that tries to find the maximum by following the greatest slope will find the local maximum at "m", oblivious to the global maximum at "M".


| caption1 = 从 a 开始，一个贪婪算法试图通过跟随最大斜率来寻找最大值，会在“ m”处找到局部最大值，不会注意到“ m”处的全局最大值。

  | image2    = Greedy-search-path-example.gif

  | image2    = Greedy-search-path-example.gif


2 = Greedy-search-path-example. gif

  | alt2      =

  | alt2      =


2 =

  | caption2  =

  | caption2  =


2 =

With a goal of reaching the largest sum, at each step, the greedy algorithm will choose what appears to be the optimal immediate choice, so it will choose 12 instead of 3 at the second step, and will not reach the best solution, which contains 99.

With a goal of reaching the largest sum, at each step, the greedy algorithm will choose what appears to be the optimal immediate choice, so it will choose 12 instead of 3 at the second step, and will not reach the best solution, which contains 99.

}}

}}

}}

For many other problems, greedy algorithms fail to produce the optimal solution, and may even produce the unique worst possible solution. One example is the traveling salesman problem mentioned above: for each number of cities, there is an assignment of distances between the cities for which the nearest-neighbor heuristic produces the unique worst possible tour.[3]

For many other problems, greedy algorithms fail to produce the optimal solution, and may even produce the unique worst possible solution. One example is the traveling salesman problem mentioned above: for each number of cities, there is an assignment of distances between the cities for which the nearest-neighbor heuristic produces the unique worst possible tour.

## Types

Greedy algorithms can be characterized as being 'short sighted', and also as 'non-recoverable'. They are ideal only for problems which have 'optimal substructure'. Despite this, for many simple problems, the best suited algorithms are greedy algorithms. It is important, however, to note that the greedy algorithm can be used as a selection algorithm to prioritize options within a search, or branch-and-bound algorithm. There are a few variations to the greedy algorithm:

Greedy algorithms can be characterized as being 'short sighted', and also as 'non-recoverable'. They are ideal only for problems which have 'optimal substructure'. Despite this, for many simple problems, the best suited algorithms are greedy algorithms. It is important, however, to note that the greedy algorithm can be used as a selection algorithm to prioritize options within a search, or branch-and-bound algorithm. There are a few variations to the greedy algorithm:

• Pure greedy algorithms
• Orthogonal greedy algorithms
• Relaxed greedy algorithms

## Theory

Greedy algorithms have a long history of study in combinatorial optimization and theoretical computer science. Greedy heuristics are known to produce suboptimal results on many problems,[4] and so natural questions are:

Greedy algorithms have a long history of study in combinatorial optimization and theoretical computer science. Greedy heuristics are known to produce suboptimal results on many problems, and so natural questions are:

• For which problems do greedy algorithms perform optimally?
• For which problems do greedy algorithms guarantee an approximately optimal solution?
• For which problems is the greedy algorithm guaranteed not to produce an optimal solution?

A large body of literature exists answering these questions for general classes of problems, such as matroids, as well as for specific problems, such as set cover.

A large body of literature exists answering these questions for general classes of problems, such as matroids, as well as for specific problems, such as set cover.

### Matroids

A matroid is a mathematical structure that generalizes the notion of linear independence from vector spaces to arbitrary sets. If an optimization problem has the structure of a matroid, then the appropriate greedy algorithm will solve it optimally.[5]

A matroid is a mathematical structure that generalizes the notion of linear independence from vector spaces to arbitrary sets. If an optimization problem has the structure of a matroid, then the appropriate greedy algorithm will solve it optimally.

### Submodular functions

A function $\displaystyle{ f }$ defined on subsets of a set $\displaystyle{ \Omega }$ is called submodular if for every $\displaystyle{ S, T \subseteq \Omega }$ we have that $\displaystyle{ f(S)+f(T)\geq f(S\cup T)+f(S\cap T) }$.

A function $\displaystyle{ f }$ defined on subsets of a set $\displaystyle{ \Omega }$ is called submodular if for every $\displaystyle{ S, T \subseteq \Omega }$ we have that $\displaystyle{ f(S)+f(T)\geq f(S\cup T)+f(S\cap T) }$.

Suppose one wants to find a set $\displaystyle{ S }$ which maximizes $\displaystyle{ f }$. The greedy algorithm, which builds up a set $\displaystyle{ S }$ by incrementally adding the element which increases $\displaystyle{ f }$ the most at each step, produces as output a set that is at least $\displaystyle{ (1 - 1/e) \max_{X \subseteq \Omega} f(X) }$.[6] That is, greedy performs within a constant factor of $\displaystyle{ (1 - 1/e) \approx 0.63 }$ as good as the optimal solution.

Suppose one wants to find a set $\displaystyle{ S }$ which maximizes $\displaystyle{ f }$. The greedy algorithm, which builds up a set $\displaystyle{ S }$ by incrementally adding the element which increases $\displaystyle{ f }$ the most at each step, produces as output a set that is at least $\displaystyle{ (1 - 1/e) \max_{X \subseteq \Omega} f(X) }$. That is, greedy performs within a constant factor of $\displaystyle{ (1 - 1/e) \approx 0.63 }$ as good as the optimal solution.

Similar guarantees are provable when additional constraints, such as cardinality constraints,[7] are imposed on the output, though often slight variations on the greedy algorithm are required. See [8] for an overview.

Similar guarantees are provable when additional constraints, such as cardinality constraints, are imposed on the output, though often slight variations on the greedy algorithm are required. See for an overview.

### Other problems with guarantees

Other problems for which the greedy algorithm gives a strong guarantee, but not an optimal solution, include

Other problems for which the greedy algorithm gives a strong guarantee, but not an optimal solution, include

Many of these problems have matching lower bounds; i.e., the greedy algorithm does not perform better, in the worst case, than the guarantee.

Many of these problems have matching lower bounds; i.e., the greedy algorithm does not perform better, in the worst case, than the guarantee.

## Applications

Greedy algorithms mostly (but not always) fail to find the globally optimal solution because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them from finding the best overall solution later. For example, all known greedy coloring algorithms for the graph coloring problem and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum.

Greedy algorithms mostly (but not always) fail to find the globally optimal solution because they usually do not operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them from finding the best overall solution later. For example, all known greedy coloring algorithms for the graph coloring problem and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum.

If a greedy algorithm can be proven to yield the global optimum for a given problem class, it typically becomes the method of choice because it is faster than other optimization methods like dynamic programming. Examples of such greedy algorithms are Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees, and the algorithm for finding optimum Huffman trees.

If a greedy algorithm can be proven to yield the global optimum for a given problem class, it typically becomes the method of choice because it is faster than other optimization methods like dynamic programming. Examples of such greedy algorithms are Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees, and the algorithm for finding optimum Huffman trees.

Greedy algorithms appear in network routing as well. Using greedy routing, a message is forwarded to the neighboring node which is "closest" to the destination. The notion of a node's location (and hence "closeness") may be determined by its physical location, as in geographic routing used by ad hoc networks. Location may also be an entirely artificial construct as in small world routing and distributed hash table.

Greedy algorithms appear in network routing as well. Using greedy routing, a message is forwarded to the neighboring node which is "closest" to the destination. The notion of a node's location (and hence "closeness") may be determined by its physical location, as in geographic routing used by ad hoc networks. Location may also be an entirely artificial construct as in small world routing and distributed hash table.

## Examples

• The activity selection problem is characteristic to this class of problems, where the goal is to pick the maximum number of activities that do not clash with each other.
• The matching pursuit is an example of greedy algorithm applied on signal approximation.
• A greedy algorithm finds the optimal solution to Malfatti's problem of finding three disjoint circles within a given triangle that maximize the total area of the circles; it is conjectured that the same greedy algorithm is optimal for any number of circles.
• A greedy algorithm is used to construct a Huffman tree during Huffman coding where it finds an optimal solution.
• In decision tree learning, greedy algorithms are commonly used, however they are not guaranteed to find the optimal solution.
• One popular such algorithm is the ID3 algorithm for decision tree construction.
• A* search is conditionally optimal, requiring an "admissible heuristic" that will not overestimate path costs.

## Notes

1. Black, Paul E. (2 February 2005). "greedy algorithm". Dictionary of Algorithms and Data Structures. U.S. National Institute of Standards and Technology (NIST). Retrieved 17 August 2012.
2. Introduction to Algorithms (Cormen, Leiserson, Rivest, and Stein) 2001, Chapter 16 "Greedy Algorithms".
3. Gutin, Gregory; Yeo, Anders; Zverovich, Alexey (2002). "Traveling salesman should not be greedy: Domination analysis of greedy-type heuristics for the TSP". Discrete Applied Mathematics. 117 (1–3): 81–86. doi:10.1016/S0166-218X(01)00195-0.
4. U. Feige. A threshold of ln n for approximating set cover. Journal of the ACM, 45(4):634–652, 1998.
5. Papadimitriou, Christos H., and Kenneth Steiglitz. Combinatorial optimization: algorithms and complexity. Courier Corporation, 1998.
6. G. Nemhauser, L.A. Wolsey, and M.L. Fisher. "An analysis of approximations for maximizing submodular set functions—I." Mathematical Programming 14.1 (1978): 265-294.
7. N. Buchbinder, et al. "Submodular maximization with cardinality constraints." Proceedings of the twenty-fifth annual ACM-SIAM symposium on Discrete algorithms. Society for Industrial and Applied Mathematics, 2014.
8. Krause, Andreas, and Daniel Golovin. "Submodular function maximization." (2014): 71-104.