Dynamic programming Algorithm (DP)

Source: Internet
Author: User
Tags radar

Multi-stage decision-making (multistep decision process) refers to a special kind of activity process, which can be decomposed into several interconnected phases in chronological order, and the decision-making is a decision sequence in every stage. Dynamic programming (dynamical programming) algorithm is a common method to solve the optimization problem of multi-stage decision process, which is more difficult and skillful. By using the dynamic programming algorithm, many greedy algorithms or divide-and-conquer algorithms can be solved gracefully and efficiently. 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.

The validity of the dynamic programming algorithm depends on the two important properties of the problem to be solved: the property of the optimal substructure and the overlapping property of the sub-problem.

1, the optimal substructure properties. If the solution of the problem is also optimal, we call the problem to have the optimal substructure property (i.e. to satisfy the optimization principle). The optimal Substructure property provides an important clue for solving the problem of dynamic programming algorithm.

2, sub-problem overlapping nature. The overlapping nature of sub-problems is that when the problem is solved by a recursive algorithm from top to bottom, the sub-problem is not always a new problem, and some sub-problems are counted repeatedly. The dynamic programming algorithm takes advantage of the overlapping nature of this seed problem, calculates the result of each sub-problem only once, and then saves its results in a table, and when it is necessary to calculate the sub-problem that has already been calculated, it simply looks at the result in the table, thus obtaining the higher efficiency of solving the problem.

When we have determined that the problem to be solved needs to be solved with a dynamic programming algorithm, you can usually design a dynamic planning algorithm by following these steps:

1. The optimal solution of the problem is analyzed, the property of the optimal solution is found, and its structural characteristics are depicted.

2. To define the optimal value recursively;

3, the optimal value of the problem is calculated by the bottom-up method;

4, the optimal solution is constructed according to the information obtained when calculating the optimal value.

The first step is the basic step of the dynamic programming algorithm to solve the problem, in order to calculate the optimal value of the problem, complete these three basic steps can be. If the problem requires the construction of the optimal solution, the 4th step is also performed, in which case the 3rd step usually requires more information to be recorded so that in step 4 there is enough information to quickly construct the optimal solution.

The following analysis of specific examples to help you understand the available dynamic programming algorithm to solve the problem should have two properties and dynamic programming algorithm design ideas.

Example: Interception of missiles (source: 1999 National Youth Informatics (computer) Olympic Regional League Senior High School first question)

A missile interception system has been developed by a country to defend against enemy missile attacks. But the missile interception system has a flaw: although its first artillery shells can reach any height, each projectile cannot be higher than the previous one. One day, the radar caught the enemy's missiles to attack. Since the system is still in the trial phase, there is only one set of systems that may not intercept all missiles.

The height of the input missile in turn (the height data given by the radar is a positive integer not greater than 30000), the maximum number of missiles that can be intercepted by the system, and the height at which the intercepted missiles are flown in turn.

Examples:

INPUT

389 207 155 300 299 170 158 65

OUTPUT

6 (maximum number of missiles to intercept)

389 300 299 170 158 65

Analysis: Because there is only a set of missile interception system, and this system in addition to the first shells can reach any height, each subsequent shells will not be higher than the height of the previous projectile, therefore, the intercepted missiles should be the height of the fly to form a non-incremental sequence. The topic requires us to calculate the maximum number of missiles the system can intercept, and sequentially to output the height of the intercepted missiles, which in effect requires us to look for a maximum non-ascending subsequence in the sequence of heights in which the missiles fly sequentially.

Set x={x1,x2,..., xn} for the sequence of missiles, Y={y1,y2,..., YK} is the optimal solution of the problem (that is, the longest non-ascending subsequence of X), S is the state of the problem (indicating the maximum height at which the missile intercept system is currently sending shells, the initial value is s=∞-- The first projectile can reach any height). If Y1= X1, the first missile to be flown is successfully intercepted. Then, according to test instructions, "each projectile cannot be higher than the previous one", the state of the problem will be changed from s=∞ to S≤X1 (X1 is the height of the first missile); In the current state, sequence y1={y2,..., YK} should also be sequence x1={x2,..., xn} The longest non-incrementing subsequence (you can easily prove it with contradiction). In other words, under the current state s≤x1, the solution of sub-problems (sequence X1) of the optimal solution Y of the problem (sequence Y1) is also optimal. This is the optimal sub-structural property of the interceptor missile problem.

Set D (i) the maximum number of missiles (including intercepted block I) that can be intercepted by the system after the interception of the first missile. We can envisage that when the system intercepts the K-missile xk, and XK is the sequence x={x1, the minimum value of X2,..., xn}, that is, the K-missiles are the lowest of all the missiles flown, there is d (k) = 1; When the system intercepts the last missile xn, The system can only intercept this missile at most, i.e. d (n) = 1, and in other cases, D (i) ≥1. According to the above analysis, the recursive equation of dynamic programming can be summed up as follows:


Assuming that the maximum number of missiles the system can intercept is dmax (the optimal value of the problem), the

DMax (i is the sequence number of the first missile intercepted by the system)

Therefore, to calculate the optimal value of the problem dmax, we need to calculate the D (1), D (2) separately 、...... D (n) and compare them to find the maximum value. Based on the recursive equation analyzed above, we can design a recursive function and calculate the value of D (i) by a top-down method. Then, by calling this recursive function from 1 to n, I can calculate D (1), D (2) 、...... D (n). However, there will be a lot of sub-problems to be repeated calculation. For example, when you call a recursive function to calculate D (1), you may need to calculate the value of D (5), and then, when you call the recursive function to calculate D (2), D (3), D (4), you may need to first calculate the value of D (5). Thus, in the whole problem solving process, D (5) may be repeated calculation many times, resulting in redundancy, reducing the efficiency of the program.

In fact, through the above analysis, we already know: D (n) = 1. If n is used as a stage to divide the problem, according to the problem of the dynamic programming recursive equation, we can use the bottom-up method to calculate the D (n-1), D (n-2) 、...... The value of D (1). Thus, the value of each d (i) is computed only once, and the calculation results are saved at the same time, thus avoiding the occurrence of some sub-problems being duplicated and improving the efficiency of the program. The algorithm code is as follows:

For I=1 to N

D (i) =1

Next I

For i=n-1 to 1 step-1

For J=i+1 to N

If X (j) <=x (i) and D (i) <d (j) +1 Then

D (i) =d (j) +1

endif

Next J

Next I

Dmax=0:xh=1

The REM DMax is used to preserve the optimal value of the problem (the maximum number of missiles the system can intercept); XH is used to preserve the sequence number of the first intercepted missile so that the optimal solution is constructed later

For I=1 to N

If D (i) >dmax then

Dmax=d (i)

Xh=i

endif

Next I

We saved the order number XH of the first intercepted missile when we calculated the optimal value. This information is then constructed to construct the optimal solution of the problem (a non-incremental sequence consisting of the heights of all intercepted missiles). The algorithm code is as follows:

Print x (XH)

For I=xh+1 to N

If x (i) <=x (i-1) Then

Print x (i)

endif

Next I


Conclusion: Dynamic programming algorithm and greedy algorithm are common methods to construct optimal solution. The dynamic programming algorithm does not have a fixed problem-solving mode and is very skillful. It can be said that the dynamic programming algorithm in real life every application is a kind of creation. We should practice more, practice more, grasp the essence of dynamic programming algorithm from practice, improve their application ability continuously.

Dynamic programming Algorithm (DP)

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.