An understanding of dynamic planning and greedy Algorithms

Source: Internet
Author: User
Document directory
  • Backpack Problems
  • Another problem: is the problem with a backpack completely NP?
The two important algorithms have always been vague. The first is a very effective method for solving the optimal problem, and the second is an obvious optimal decision: determine the next step through the max and Min operations on some values. The differences and differences are unclear. The key is that the dynamic planning algorithm itself is not particularly understood. Today, I have read about the introduction to MIT algorithms.

(Digression: there are not many introductions to algorithms, but for the two chapters, we can see that this teaching material is very different from the domestic algorithm teaching material. Many textbooks in China are directly "easy and difficult". They first introduce greed and then introduce dynamic planning. It seems that there is a heavy "competition. The introduction to algorithms first introduces dynamic planning and then greedy. from an academic point of view, dynamic planning is the basis and greedy is a special case. In this case, it seems that domestic textbooks are too utilitarian, however, there is a lack of purely theoretical training value .) 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 to 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 do not need to know all the sub-trees of a node, you can find the value of this 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, its price substitute only depends on the number of sub-problems, and the total number of choices is 1. The backpack problem is used as an example. The greedy algorithm is to directly select the item with the largest current price/weight ratio and put it into the backpack, in an attempt to keep the total price/weight ratio of the item in the backpack always the largest, to obtain the optimal value. The idea of dynamic planning is to consider the "put it" and "not put it" situations for each item, and take the best one as the solution. Dynamic Planning performs better traversal of the solution space than greedy. Apparently, if we can ensure that the backpack is always full, the greedy algorithm is correct. But the 0-1 backpack problem cannot ensure that the backpack is always full, which reduces the overall price/weight ratio of the backpack. In this case, we can only use dynamic planning to solve the problem. It seems that the problem with a backpack is a special case. The current situation of the problem (the remaining items) can determine the current choice, but other restrictions (the total weight of the backpack) make greed wrong. Other problems, such as matrix concatenation and substring matching, are obviously not greedy and can only be solved using dynamic planning. Another problem: backpack problem is NP complete problem it recommends a Website: http://www.answers.com, set a very good definition, information, the following text from this website. The Dynamic Programming solution for the knapsack problem is a pseudo polynomial order algorithm. If the integer c of the total weight is used as the scale, it is a polynomial order (O (NC )). If the binary digits of C are used as the scale, it is exponential. Therefore, the optimal form of solution (optimization) is still an NP complete problem, but it has a pseudo polynomial order solution. An example of the pseudo polynomial order is as follows: Calculate the factor of integer n. Obviously, at most 2 ~ N-1 all the factors can be obtained after one division. It seems that this is the order of O (N), but if we consider it from the number of digits of N, it becomes an exponential order and becomes unsolvable. This happens to be the basis of the current network encryption algorithm. The determination form of the knapsack problem is the true NP-complete problem: Given the value and weight of a group of items, while meeting the weight limit, can we select a subset of an item so that the total value is exactly the same as a given value c. This is also known as the Subset sum problem. For more information, see http://www.answers.com/topic/knapsack-problem.

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.