Dynamic programming of algorithm introduction (longest common subsequence and optimal binary search tree)

Source: Internet
Author: User
Tags scalar

The Dynamic planner solves the whole problem by combining the solution of sub-problem, divides the problem into sub-problem, solves each sub-problem recursively, and then merges the solution of the sub-problem to get the solution of the original problem. And divide and conquer the idea of the same algorithm, the difference is that the divide-and-conquer algorithm is suitable for independent sub-problems, and for the non-independent sub-problem, that is, each sub-problem contains the common sub-sub-problem, if the use of split-treatment method will be repeated, dynamic programming to save sub-problem results in a table,

Dynamic programming chooses an optimal solution in multiple values, and its algorithm design is generally divided into 4 steps: Describe the structure of the optimal solution, define the value of the optimal solution by recursion, calculate the value of the optimal solution by the bottom-up method, and construct an optimal solution from the calculated result.

1) assembly line scheduling

Solve the problem of the fastest route through the factory assembly line, the problem description: There are two assembly lines for the installation of parts of the car chassis, the construction time of different use of technology has caused the installation time is slow; each assembly line has n assembly stations, the assembly line I of the J Assembly station is Sij, the corresponding assembly time is AIJ, The same numbered assembly stations on each assembly line perform the same functions, such as s1j and s2j.

Three point-in-time descriptions:

Entry time: An automotive site enters the assembly line (I is 1 or 2) and takes time for EI;

Moving time: There is no time cost to move adjacent assembly stations (from J to j+1) on the same assembly line, but it takes time Tij to move from the assembly station SIJ of one assembly line to another assembly line;

Departure time: After completing an assembly line N assembly stations, it takes XI time to leave the assembly line.

To solve the problem is: solve the assembly line 1 of several stations and assembly line 2 of several stations, to make the car chassis parts to complete the installation of the minimum time.

If a sequence is known to use those stations on assembly line 1, and those stations are used on assembly line 2, it is possible to draw a time in linear time for a chassis to pass through the factory assembly line. But to know that the route is time-consuming, but it is possible in 2n, an assembly station used by an assembly line can be seen as a subset of {1,2,...,n}. Dynamic programming method is to find the best solution from 2n, we follow the dynamic programming method four steps to illustrate:

First step: Describe the structure of the optimal solution

This step is to find the characteristics of the optimal solution, so that the problem can be transformed into a number of sub-problems to solve. To describe the optimal solution for the minimal time-consuming problem of assembly line scheduling, we should start with a basic hypothesis. Assuming that the quickest route through the assembly station S1J through the Assembly station S1J-1, then the chassis is the fastest route from the start point to the Assembly station S1J-1, if not, there is another bar from the start point to the assembly station S1j-1 faster route, which creates a contradiction. This idea is very simple to understand, that is, if you are the quickest way, then the route ahead of the point must be the fastest, otherwise it will not pass the store.

According to this, finding the optimal solution of the quickest route s1j through the assembly station can be transformed into the optimal solution to find the sub-problem of the quickest route of S1j-1 or S2j-1. This is: To find the fastest point, then find the point in front of the point of the fastest, where the assembly line only two points will be sub-structure, assembly line 1 J-1 station or assembly line 2 J-1 station. This is consistent with the route planning, to find a certain point of the shortest path, just find this point in front of a little bit of the shortest path can be, the problem of borrowing into sub-problems.

In summary, the optimal solution structure of the fastest time scheduling problem in assembly line:

The quickest route through the assembly station S1J is two possible: first, the quickest route through the assembly station S1J-1, then directly through the assembly station S1J, the second is through the assembly station S2j-1 the fastest route, from assembly line 2 to the transfer Cable 1, and then through the Assembly station S1J; The quickest route to the s2j of the assembly station is also two possible.

Step Two: Define the value of the optimal solution recursively


Input: Assembly station execution time AIJ, assembly station movement time Tij between assembly lines, assembly line entry time EI and assembly line exit time Xi and number of assembly stations per assembly line N

Fun_fastestway (a,t,e,x,n) {

F1[1]=e1+a11

F2[1]=e2+a21

For j=2 to N

Do if f1[j-1]+ a1j=<f2[j-1]+ a1j +t2j-1

Then f1[j]=f1[j-1]+a1j

L1[j]=1//assembly line

else f1[j]= f2[j-1]+a1j +t2j-1

l1[j]=2

If f2[j-1]+ a2j=<f1[j-1]+ a2j +t1j-1

Then f2[j]=f2[j-1]+a2j

l2[j]=2

else f2[j]= f1[j-1]+a2j +t1j-1

L2[j]=1

If f1[n]+ x1 =< f2[n]+x2

Then f= f1[n]+ x1

L=1

else f= f2[n]+ x2

l=2

}

The algorithm process is to calculate the value of each assembly station, recorded in the table and saved.

Step four: Construct the optimal solution

The optimal solution of the quickest route can be output by calculating the time of each assembly station at the last step.

2) matrix chain multiplication

In this paper, the dynamic programming method is proposed to solve matrix chain multiplication, there are two prerequisites, one is matrix multiplication satisfies the binding law, the other is to multiply when two matrices are compatible, so-called compatibility, that is, the number of columns of a is equal to the number of rows of B. The multiplication of n matrix chains is solved by parentheses, but the different parentheses are different in the time of quadrature. Matrix A (PXQ) and B (QXR) are multiplied, the number of operations is PXQXR times, and the different grouping order of a matrix group obviously differs greatly in time performance.

This puts forward the problem of matrix chain multiplication: A chain <a1,a2 consisting of n matrices,..., Ai,..., an>, wherein, i=1,2,..., N, the dimension of the matrix Ai (row x column) is Pi-1xqi, the matrix chain is grouped in parentheses to minimize the number of operations, and the A1A2 is obtained. An product. In order to solve the problem, the dynamic programming method is adopted in four steps.

First step: Describe the structure of the optimal solution

The first step of the dynamic programming method is to find the optimal sub-structure, and the optimal solution of the original problem can be constructed according to the optimal solution of sub-problem. The optimal solution of a problem can be composed of the optimal solution of the sub-problem, and the optimal substructure can obviously be recursive. The optimal sub-structure of matrix chain multiplication is also well understood, and ai,j represents the matrix product aiai+1 ... The results of AJ Evaluation, wherein I<=J, take K for the value between I and J, you can divide the matrix ai,j evaluation into aiai+1 ... AK to Akak+1 ... AJ two interval and find the optimal solution, similar to the assembly line scheduling problem, assuming aiai+1 ... When AJ is the optimal solution, the two intervals separated from K are the best brackets.

With such an optimal substructure, an optimal solution of the original problem can be constructed based on the optimal solution of the sub-problem. Matrix chain multiplication solves the problem, can construct the optimal sub-structure, or the existence substructure can obtain the optimal solution, namely the Division product, the problem's optimal solution contains the sub-problem the optimal solution.

Step two: Define recursive optimal solutions

This step is to recursively define the code of an optimal solution based on the optimal solution of the sub-problem. The problem of matrix chain multiplication is to determine aiai+1 ... The minimum cost of the product of AJ plus all parentheses. Set M[I,J] is the minimum value of the number of scalar multiplication operations required to compute the matrix ai,j, that is, how to divide the matrix chain (with parentheses) to minimize the cost of the product operation. So, for the whole problem, the minimum cost of calculating a1,n is m[1,n].

Assuming the optimal plus all parentheses will multiply the product aiai+1 ... AJ separates from AK and Ak+1, where i=<k<j, making m[i,j] is a calculated sub-product aiai+1 ... AK and akak+1 ... The cost of AJ, plus the cost of multiplying by two matrices, assumes that each matrix AI is pi-1xpi, then AI...KAK+1...J to perform PI-1PKPJ scalar multiplication, resulting in:


Step three: Calculate the optimal cost

Similarly, if the recursive implementation of the algorithm refers to a number of times, and the matrix chain multiplication problem obviously satisfies the nature of overlap, you can use the bottom-up table method to calculate the optimal cost. For the problem of dynamic programming, the first feature is the optimal substructure, and the second feature is the overlap of sub-problems.

Suppose that the dimension of the matrix AI is pi-1xpi,i=1,2,..., N. Enter a sequence p=<p0,p1,..., pn>,length[p]=n+1, design a secondary table M[1...N,1...N] to save the cost of m[i,j], and use the auxiliary table S[1...N,1...N] to record the value of K when the optimal solution is obtained.

Fun_matrix_chain_order (p) {

N= length[p]-1;

For I=1 to N

The cost of Do m[i,j]=0;//i=j is 0

For l=2 to N

Do-I=1 to N-l+1

Do j=i+l-1;

m[i,j]=∞;//each interval cost of initializing a i<j

For K=i to J-1

Do q=m[i,k]+m[k+1,j]+ PI-1PKPJ

If Q<M[I,J]

Then m[i,j]=q;

S[i,j]=k;

return m and S;

}

This algorithm is better understood, that is, the cost of the loop calculation of each interval, and then with two auxiliary table records, three-layer loop, the algorithm time is O (N3), the space needs two table cost.

Fourth step: Constructing the optimal solution

With the third step, we can easily construct the optimal solution by using the M and s tables by multiplying the optimal scalar multiplication times of the matrix chain.

3) Characteristics of dynamic programming

Four steps of the dynamic programming method through matrix chain multiplication and assembly line scheduling two problems have been relatively clear outline, need to summarize the reality of what the problem is appropriate to use dynamic programming method to solve it?

As mentioned earlier, it is suitable to use the dynamic programming method to solve the optimal problem, which needs to satisfy two characteristics: the optimal substructure and the overlapping sub-problem, and the sub-problem is independent.

If an optimal solution of the problem contains the optimal solution of the sub-problem, the problem has the optimal substructure. To find the optimal substructure, you can follow the following pattern:

A solution of the problem is a choice, the choice of the optimal solution for a given problem, and the optimal solution of sub-problem has the same structure, which can be composed of the optimal solution of sub-problem.

In layman's words: This problem can be divided into sub-problems with the same structure, so that the optimal solution of the problem can be divided into the optimal solution of the problem. The same structure will also be said that the problem is overlapping, you can use the recursive algorithm of the original solution to solve the sub-problem repeatedly. The problem solved by this division is not the same, the sub-problems produced by the division-ruling method are all new and do not have the same structure. In other words, the dynamic programming method to solve the problem, with the same nature as the Russian set of children, and the division of the law is faced with a sub-problem has different structure and the nature of the solution, the same algorithm can not be used for problems and different sub-problems. It is also necessary to mention that the greedy algorithm and the dynamic programming method are the same for the problem has the optimal substructure, the difference is that the greedy algorithm is the top-down approach to the optimal sub-structure, and dynamic planner bottom-up; that is, the greedy algorithm and the dynamic programming approach to the optimal sub-structure, The greedy algorithm is the optimal solution of the first-order optimal solution, and the dynamic programming is opposite, the optimal solution of the sub-problem is solved first, and the greedy algorithm is the first choice and then the solution, and the dynamic programming method is the first to solve the optimal and then select.

It is worth explaining that dynamic planning requires overlapping of its sub-problems and also requires its independence. What is sub-problem independence? Is the two sub-problems of the same problem do not share resources, then they are independent. Overlapping refers to sub-problems that are the same, but as sub-problems of different problems. For the understanding of the sub-problem is independent, the introduction of the lack of the shortest path and no right to the longest simple path analysis, back to the reality, is also a good understanding of the case explained that the longest simple path of the sub-problem is not independent, can not be used to solve the dynamic programming method. The shortest path is not said, the longest problem is decomposed into sub-problems, obviously on a graph node is not independent.

It is more practical to use dynamic programming method to find the longest common subsequence and the best binary search tree. The maximum common subsequence (LCS) mentioned in this article can be used for DNA string similarity matching, which is to look for the maximum length of a common sequence between two strings. The best binary search tree is mentioned for word translation, and the optimal binary tree is constructed according to frequency, which is obviously suitable for the scene which is sorted by frequency. These two cases of dynamic programming method of constructing four step process is not described, the principle is similar, the most important in real-life applications, to find a solution to the dynamic planning method to solve the problem, and improve to apply.

Follow up if you encounter similarLongest common subsequence and optimal binary search treeOf the scene, and then study its dynamic programming method to solve.

Dynamic programming of algorithm introduction (longest common subsequence and optimal binary search tree)

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.