Greedy algorithm and dynamic programming algorithm

Source: Internet
Author: User

Both dynamic programming and greedy algorithm are recursive algorithms, i.e., the global optimal solution is deduced from the local optimal solution (it is not considered from the whole optimal solution, always makes the best choice at present.) )

different points: greedy algorithm and the difference between dynamic planning: greedy algorithm, every step of the greedy decision can not be changed, from the last step of the optimal solution to deduce the next optimal solution, so the previous optimal solution is not reserved. can use greedy method to solve the conditions : whether to find a greedy standard. Let's look at a coin-finding example, if a monetary system has three denominations, one angle, five points, and one point, which can be solved by greedy method when the minimum number of coins is found; If you change these three currencies to 1.1 cents, five and one, you cannot use the greedy method to solve them. Example: the choice of the greedy law standard there are n positive integers, which are concatenated into a row to form one of the largest multi-bit integers. For example: n=3, 3 integers 13,312,343, the maximum number of integers is 34331213. also such as: n=4, 4 integer 7,13,4,246, the largest integer is 7424613. input: N number output: Number of multiple digits connectedAlgorithm Analysis: This problem is very easy to think of using greedy method, in the exam there are many students to the whole number from the big to the small sequence of connections, test questions are also in line with the example, but the final test results are not all right. According to this standard, we can easily find a counter example: 12,121 should be composed of 12121 instead of 12112, then is not mutually inclusive when the small to large? Not necessarily, such as 12,123 is 12312 instead of 12123, this situation has many kinds of. Is not this question can not use greedy method? In fact, this problem can be solved by greedy method, just the standard is wrong, the correct standard is: first convert integers into strings, and then compare a+b and B+a, if A+b>=b+a, put a in front of B, and vice versa, a row in the back of B.

Dynamic Programming algorithm

difference from greedy method: The next optimal solution is not deduced directly from the optimal solution of the previous step, so it is necessary to record all the solutions of the previous step (f[i][j in the following example) to represent the J-Solution of line I

Conditions that enable the use of dynamic programming algorithms:

If a problem is divided into stages, the state of phase I can only be obtained by state transition equations in the stage I-1, which is not related to other States, especially to the non-occurrence state, then the problem is "no effect", which can be solved by dynamic programming algorithm

Dynamic Programming Algorithm Solution:

1. Definition phase: Row i, column J, value A[i][j]

2. Define state: Go to the maximum value of column J of row I F[i][j]

3. State transition equation: f[i][j] = A[i][j]+max (F[i+1][j], f[i+1][j+1])

4. Define boundary conditions: When i = N, f[i][j] = a[i][j]; Local optimal solution that can be directly derived from the beginning

Greedy algorithm and dynamic programming algorithm

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.