Algorithm Design Method-Dynamic Planning

Source: Internet
Author: User

We have introduced a commonly used algorithm design technology, the division and control method. When a real problem arises, we can see that the division and control method can be used to solve the problem, the division and control law is applicable to the independent and non-overlapping sub-problems. However, if the Sub-problems overlap, the division and control law will not be used. Another algorithm design method, dynamic planning, is introduced here. Although the basic idea of this algorithm design method is similar to the division and control method, it solves the higher level problems through sub-problems. However, the obvious difference between the two lies in that the subproblems of Dynamic Planning and division are overlapping (but independence must be ensured), and they are generally used to solve optimization problems ~

Steps for solving the dynamic planning algorithm:

1. I think the most important thing is the correct abstraction problem, and then the optimal sub-structure of the problem is obtained. Some questions are described in a fully textual manner. Then, the mathematical structure is abstracted and the optimal sub-structure of the problem is obtained based on the mathematical structure,

2. According to the formula description and the most sub-structure abstracted in the first step, you can simply and correctly write the recursive formula with the most sub-structure. However, if the abstract method in the first step is not perfect, it is difficult to write a recursive formula. Many of the problems in the book are abstract mathematical structures, so I think it is still necessary to contact the mathematical expression of a problem,

3. Calculate the optimal solution based on the recursive bottom-up method in step 2. One feature of dynamic planning is overlapping subproblems. Although recursive program implementation can help the program easily implement, if recursion is used here, we need to solve the subproblems repeatedly, the time complexity may still be exponential. A bookkeeping method will be introduced later to help solve Repeated Computation. However, it is recommended to calculate the time complexity from the bottom up,

4. This is generally an optional step. By the third step, the optimal solution has been obtained. However, if an optimal solution is to be constructed, some records must be recorded during Algorithm Execution, the fourth step is to use these records to construct a specific solution to the optimal solution.

Method for discovering the optimal sub-structure:

1. Make a choice to split the problem into one or more sub-problems. Here is a test-free choice.

2. Assume that your choice is the best one.

3. After you have selected this option, determine which sub-problems will occur and how to best describe the obtained sub-problem space.

4. Using a "clipboard" technique, it is proved that in an optimal solution of the problem, the solution of the subproblem used must also be optimal.

Describe the sub-problem space according to an empirical rule. Try to keep the space simple and then expand it as needed.

Some of the above descriptions are hard to understand. The discovery method of the optimal sub-structure is taken from the introduction to algorithms. I still cannot understand the concept of sub-problematic space. Next, I will learn the truth through specific examples.

For the first assembly line problem, the problem is described as follows. Based on the problem, we can see that the problem has been abstracted. Next we only need to find the optimal sub-structure for the problem and then write the recursive formula, with recursion, you can complete pseudo-code. The entire process from pseudo-code to code conversion is very smooth.

First, the fastest solution must be S1, N + X1, S2, and N + x2 at the end of the first line to get a small value and the fastest value. Therefore, the problem is converted to the minimum values that reach S1, N, S2, and N. Here, we break down the problem and narrow down the problem. We can see that the value of N passes through S1, n-1, S2, n-1, however, any S1 and N of N here are the shortest paths. If S1 and n-1 are not the shortest paths, another shorter S1 and n-1 path must be used to replace S1, n-1, so S1 and n are shorter, but S1 and n are already the shortest path, so S1 and n-1 are the shortest paths. Therefore, the optimal sub-structure has been found here, followed by recursion, it is not too difficult to write a recursive formula based on the above summary:

The pseudo code based on the above structure is also relatively simple:

Data Records after code execution:

In order to restore the fastest path, l must be saved and cannot be omitted. However, the intermediate records of F can save some space, because only the first digit is used for computing, therefore, you only need a constant storage space. In fact, many of the following solutions can save some storage space. However, the general algorithm design focuses on time efficiency, less space, or even less.

Restore path:

The correct direction of recursion is to restore the path:

PRINT-STATIONS(l,j,n)    i = l*    if j=0        return    else        if(j>1) i=li[j]    PRINT-STATIONS(l,j-1,n)    if j=n        i=l*        print i    else        i=li[j+1]        print i        

The second problem is the combination of matrix chain products.

Time Complexity Calculation of Matrix Multiplication

 

Three steps for dynamic planning to solve the problem:

The pseudocode is as follows:

In this storage structure book, the matrix structure is often reversed. I personally think that reverse is not easy to understand, so we still use the normal square matrix to explain

One is the calculation of the optimal solution, and the other is the K value of the split that needs to be recorded to restore the optimal solution ~!

Restore the pseudocode of the method added by brackets

Factors Affecting the time complexity of the Dynamic Planning Algorithm:

The above two analyses can refer to these two factors:

Next, we will analyze the impact of the overlap subproblem: for example, the matrix chain multiplication example

 

So the following accounting method is generated...

The pseudocode is as follows:

Applicability:

There is a feeling of vomiting blood. This blog post is definitely not very good, with too much content, and many of them are directly copied from the visible, even if there are so many such content, why are we talking about dynamic planning? Here we are all about the principles. When we are free, we should implement these algorithms, there are two more dynamic planning problems, the longest common subsequence and the optimal binary search tree...

 

Longest Common subsequence

Problem description

Step

Optimal Binary Search Tree

One of the design ideas of algorithms is to grasp the ideas.

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.