Differences between dynamic planning and greedy Algorithms

Source: Internet
Author: User

 

(From) http://hi.baidu.com/35661327/blog/item/d5463e17f1e8d011972b439c.html

 

Dynamic Planning and greedy Algorithms
Differences between dynamic planning and greedy Algorithms
Dynamic Programming and greedy algorithms are both recursive algorithms.
Local optimal solutions are provided to derive the global optimal solution.

Differences:
Greedy Algorithm:
1. In the greedy algorithm, the greedy decision made in each step cannot be changed, because the greedy policy derives the optimal solution of the next step from the optimal solution in the previous step, and the optimal solution in the previous step is not reserved.
2. According to the introduction in (1), the correct condition of the greedy method is that the optimal solution of each step must include the optimal solution of the previous step.

Dynamic Planning Algorithm:
1. The global optimal solution must contain a certain local optimal solution, but not necessarily the previous local optimal solution. Therefore, we need to record all the previous optimal solutions.
2. The key to dynamic planning is the state transition equation, that is, how to derive the global optimal solution from the obtained local optimal solution.
3. boundary condition: the simplest local optimal solution that can be obtained directly
========================================================== ==============================================
Greedy Algorithm and Dynamic Planning
The basic idea of greedy method:

Starting from an initial solution of the problem, we gradually approach the given goal to obtain a better solution as quickly as possible. When a certain step in an algorithm is reached, the algorithm stops.
This algorithm has the following problems:
1. There is no guarantee that the final solution obtained is optimal;
2. cannot be used to find the maximum or limit the problem;
3. only feasible solutions that meet certain constraints can be obtained. The process of implementing this algorithm:
Starting from an initial solution to the problem;

While can move one step forward to the given overall goal do
Find a solution element of the feasible solution;
A feasible solution that combines all solution elements into a problem

The most classic example of greedy algorithms is money.
For example, China's currency only looks at yuan, with 1 yuan, 2 yuan, 5 yuan, 10 yuan, 20, 50, and 100 yuan.

If I want 16 yuan, I can get 16 yuan 1 yuan and 8 yuan 2 yuan, but how can I get at least?
If you use greedy calculation, it is the biggest one I can get at a time.
For example 16, I couldn't afford 20 for the first time. I got 10 yuan, OK, 6 yuan for the rest, 5 yuan for the other, and 1 yuan for the rest.
That is, 3 images: 10, 5, and 1.

The greatest thing you can get each time is greed.

However, it must be noted that what greedy gets is not the optimal solution. That is to say, greedy is not necessarily the least number of images.
Greedy can only get a better solution, and the greedy algorithm is very easy to get.
Note: Why can we use greed for money? Because our country's money size design can make the greedy algorithm calculate the optimal solution (generally, coins in a country should be designed in this way ). It would be different if it was designed to be something else.
For example, coins in a country are divided into 1 RMB, 3 RMB, and 4 RMB.
How can I get 6 yuan for it? If you are greedy, take 4 first and then take two, one for three.
What is the actual optimum? Two three yuan tickets are enough.

The problem of finding the optimal solution is basically a traversal of the solution space. The most direct brute-force analysis is easy to obtain, and the space for solving the optimal solution is usually exponential growth. Therefore, brute-force exhaustion is not feasible.
Most of the optimal solution problems can be divided into sub-problems, and the traversal of the solution space is considered as the traversal of the sub-problem tree. Then, the optimal solution can be obtained through traversing the entire tree in some form, this analysis is not feasible.
Greedy and dynamic planning are essentially pruning of the Child problem tree. One of the problems required by the two algorithms is the "subproblem is the optimum ". That is, the solution of each subproblem that constitutes the optimal solution is certainly the best for this subproblem itself. If we look at the problem tree in the top-down direction (the original problem is used as the root), we only need to traverse down the child tree representing the optimal solution each time to guarantee the overall optimal solution. In the image, you can simply use a value (optimal value) to represent the entire subtree, instead of finding all the values that this subtree may represent.
The dynamic planning method represents a general solution to this type of problem. We construct a subproblem solution from the bottom up (from the leaves to the root). For the root of each subtree, we can find the values of each leaf below and use the optimal values as our own values, discard other values. The cost of dynamic planning depends on the number of options (the number of forks in the tree) and the number of subproblems (the number of nodes in the tree, or the height of the tree ?).
Greedy algorithms are a special case of dynamic planning. It can be proved that the root value of each subtree does not depend on the values of the following leaves, but only on the current problem. In other words, you can obtain the value of a node without knowing all the Subtrees of a node. Generally, this value is an obvious "Optimal" Condition for the current problem. Therefore, we use "greedy" to describe the essence of this algorithm. Because of the greedy algorithm, it does not need to traverse the spatial tree from the bottom up, but only needs to start from the root, select the optimal path, and continue to the end. In this way, compared with dynamic planning, the cost only depends on the number of subproblems, and the total number of choices is 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.