The greedy algorithm works in stages. At each stage, the decisions made can be considered good, regardless of future consequences. In general, this means that a local optimization is selected. This "Get it right now" strategy is the source of the name of this type of algorithm. When the algorithm terminates, we hope that the local optimal is the global optimal. If this is the case, then the algorithm is correct; otherwise, the algorithm obtains a suboptimal solution. If absolute best answers are not required, it is sometimes possible to generate approximate answers with simple greedy algorithms rather than using the complex algorithms required to produce accurate answers in general.
The greedy algorithm can be designed according to the following steps:
1. Transform the optimization problem into one such problem, that is, make a choice, and then solve the remaining sub-problem.
2. Prove that the original problem always has an optimal solution is to do greedy choice obtained, thus indicating the security of greedy choice.
3. Note that after making greedy choices, the remaining sub-problems have such a nature. If we combine the optimality of sub-problems with the greedy choice we make, we can get an optimal solution of the original problem.
The greedy choice nature and the optimal substructure property are the key features of the greedy algorithm.
Greedy Choice property: A global optimal solution can be achieved by local optimal (greedy) selection. This is where the greedy algorithm differs from the dynamic programming. In dynamic planning, each step has to make a choice, but these choices depend on the solution of the sub-problem. Therefore, the solution dynamic programming problem is usually bottom-up, from small to large. In the greedy algorithm, what we do is always the best choice at the moment, and then solve the sub-problems that arise after the selection. The current selection made by the greedy algorithm may depend on all the choices that have been made, when not relying on the solution of the choice or sub-problem being made. Therefore, unlike the dynamic programming approach which solves the problem from the bottom up, the greedy strategy is usually top-down, one to make a greedy choice, and constantly to the given problem instance specification to a smaller problem.
Greedy algorithm is a very effective method, which is suitable for a large class of problems. The minimum spanning tree, the ground connect Stella, the unit shortest path algorithm, the Huffman tree construction All is the greedy algorithm application.
"Classical algorithm" greedy algorithm