Dynamic Programming--Algorithm summary (III.)

Source: Internet
Author: User

Dynamic programming algorithms are often used to solve problems with some kind of optimal properties. There may be many possible solutions to this type of problem. Each solution corresponds to a value, and we want to find the solution with the best value. The dynamic programming algorithm is similar to the partition method, and its basic idea is to decompose the problem into several sub-problems, solve the problem first, then get the solution of the original problem from the solution of these sub-problems. Different from the partition method, the problem which is suitable for solving with the dynamic programming is not independent of each other by decomposition. If divide-and-conquer method is used to solve this kind of problem, the number of sub-problems is too many, and some sub-problems have been calculated many times. If we can save the answers to the solved sub-problems and find out the answers when we need them, we can avoid a lot of repetitive calculations and save time. We can use a table to record the answers to all the solved sub-problems. Whether or not the sub-problem is used later, as long as it is computed, the results are filled into the table. This is the basic idea of the dynamic programming method. Specific dynamic programming algorithms are varied, but they have the same form of fill-in.

If you are not very familiar with the dynamic planning, may look at the above paragraph of the text will be unintelligible, my general learning method is to first scan the basic definition, do not drill down (a bit superficial understanding taste), and then go to see some examples, combined with their own experience, and finally review, the definition of intensive reading, So that I can really understand the definition. Below we rely on a classical algorithm problem to reflect the above paragraph of the idea of the text, 0-1 knapsack problem in the algorithm learning is a compulsory course, generally in the dynamic planning problems will use this example.
Problem Description: A traveler has a backpack with a maximum of M kg, and now has n items, their weights are w1,w2,...,wn, and their value is P1,P2,..., Pn respectively. If each item has only one item without exceeding m kg, the traveler can obtain the maximum total value of the program.

Input format: m,n w1,p1 w2,p2 ...
Problem Analysis: The most basic knapsack problem, the characteristic is: each kind of item only has one piece, can choose to put or not put. Define a state with a sub-question: F[i][j] Indicates the maximum value that the first I item can get in a backpack with a capacity of J. The state transfer equation is: f[i][j] = Max{f[i-1][j], F[i-1][j-w[i]]+p[i]} then the problem is solved.

The basic idea of the dynamic programming algorithm is that the problem to be solved is decomposed into several interrelated sub-problems, solving the sub-problems first, then obtaining the solution of the original problem from the solution of the sub-problems; For the recurring sub-problem, it is solved only at the first encounter, and the answer is saved. Let's refer to the answer again later, without having to solve the problem again. The dynamic programming algorithm treats the solution of the problem as the result of a series of decision-making, and unlike the greedy algorithm, it makes an irrevocable decision in the greedy algorithm, and in the dynamic programming algorithm, it also investigates whether an optimal decision sub-sequence is included in each optimal decision sequence. That is, whether the problem has the best sub-structure property.

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.