[C + +] matrix multiplication, longest common subsequence, maximal sub-Che, longest monotone increment subsequence of dynamic programming

Source: Internet
Author: User

First, the basic idea of dynamic planning

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 problem is decomposed into several sub-problems, the first problem is solved, then the solution of the original problem is obtained from the solution of these sub-problems. Suitable for solving problems with dynamic programming, the sub-problems are often 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. For this purpose, 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.

Second, the basic elements of dynamic planning (characteristics)

1, the best sub-structure :

When the optimal solution of the problem contains the optimal solution of the sub-problem, the problem is called the optimal substructure property.

2, overlapping sub-problem:

 When using recursive algorithm to solve the problem 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, only once for each sub-problem, then saves it in a table, and uses the solution of these sub-problems as much as possible later.

The main steps of solving the problem with dynamic programming

1, find out the nature of the optimal solution, and describe its structural characteristics;

2, recursively define the optimal value (write the dynamic programming equation);

3, the best value is calculated from the bottom-up method;

4, according to the information obtained when calculating the optimal value, constructs an optimal solution.

Note: (1) The steps are the basic steps of the dynamic programming algorithm;
(2) In the case of requiring only the best value, step 4 can be omitted;
(3) If you need to ask for an optimal solution to the problem, you must perform step 4.

Four, dynamic programming examples

1, matrix multiplication problem

The time it takes to multiply the MXN matrix A and the NXP matrix B. We measured the time required to multiply the m x N x p as two matrices.

Now suppose to calculate the product of three matrices A, B, and C, there are two ways of calculating this product.

(1) first with A multiplied by B to get the matrix D, and then D times c to get the final result, the order of this multiplication is (AB) C;

(2) first with B multiplied by C to get the matrix E, and then e times a to get the final result, the order of this multiplication is a (BC).

Although the results of these two different calculation sequences are the same, there is a significant gap in time consumption.

Instance:

Figure 1.1 A, B, and C matrices

Matrix multiplication conforms to the binding law, so there are two options when calculating the ABC matrix connection, i.e. (AB) C and A (BC).

For the first scheme (AB) C and, the calculation:

Figure 1.2 AB matrix multiplication

The number of multiplication operations is: 2x3x2 = 12

Figure 1.3 (AB) C matrix multiplication

  

The number of multiplication operations is: 2x2x4 = 16
The total amount of calculation is: 12 + 16 = 28

For the second scenario A (BC), Calculate:

Figure 1.4 BC Matrix multiplication

The number of multiplication operations is: 3x2x4 = 24

Figure 1.5 A, B, and C matrix multiplication

The number of multiplication operations is: 2x3x4 = 24

The total amount of calculation is: 24 + 24 = 48

It can be seen that the multiplication operation of different schemes may vary widely.

Problem definition:

Given n matrices {A1, A2, ..., an}, where Ai and ai+1 are multiplicative, i =,..., n-1. To investigate the a1a2 of the n matrices. An. Because matrix multiplication satisfies the binding law, there are many different order of computation for the multiplicative multiplication of the matrix.

This order of calculation can be determined in parentheses. The complete parentheses of the matrix product can be recursively defined as:

(1) A single matrix is fully enclosed in parentheses;

(2) The matrix of the product A is fully parenthesized, then a can be expressed as 2 full parentheses of the matrix of the product of the products B and C and parentheses, that is, a = (BC). There are four matrices A, B, C, D, and a total of five full brackets: (A ((BC) d)), (A (B (CD))), (((AB) (CD)), (((AB) C) d), (((A (BC) d)).

A. Identify the nature of the optimal solution and characterize its structure;

Multiply matrix aiai+1 ... Aj, précis-writers for a[i:j], here i≤j; the optimal order of calculation A[1:n] is investigated.

Set this order between the Matrix Ak and ak+1 to break the matrix chain, 1≤k < n, then its corresponding full parenthesis (A1A2 ... Ak) (Ak+1ak+2 ... AN).

Calculated amount of total calculated = A[1:k] (calculated amount + a[k+1:n] calculation + a[1:k] and a[k+1:n] multiplied by

  Characteristics: The order of the computational matrix sub-chains a[1:k] and a[k+1:n] in the optimal order of calculation a[1:n] is also optimal.

B. To define the optimal value recursively (write out the dynamic programming equation);

Figure 1.6 Establishing a recursive relationship

C. Calculate the optimal value in a bottom-up manner. 

1#include <iostream>2 using namespacestd;3 4 #defineNUM 515 intP[num];//matrix Dimensions P0 x p1,p1 x p2,p2 x P3,..., P5 x P66 intM[num][num];//Minimum multiplier/optimal array of values7 intS[num][num];//Optimal disconnection position8 9 voidMatrixchain (intN)Ten { One      for(inti =1; I <= N; i++) M[i][i] =0; A      -      for(intR =2; R <= N; r++)//Number of matrices -          for(inti =1; I <= n-r+1; i++)//starting the         { -             intj=i+r-1;//End -M[I][J] = m[i+1][j]+ p[i-1]*P[I]*P[J];//calculates the initial value, disconnects from I, calculates the optimal break position -S[I][J] =i; +              for(intK = i+1; K < J; k++)  -             { +                 intt = m[i][k] + m[k+1][J] + p[i-1]*p[k]*P[j]; A                 if(T < M[i][j]) {M[i][j] = t; s[i][j] =K;} at             } -         } - } -  - voidTraceBack (intIintj) - {  in     if(i==j) -cout<<"A"<<i; to     Else  +     { -cout<<"("; the TraceBack (I,s[i][j]); *TraceBack (s[i][j]+1, J);  $cout<<")"; Panax Notoginseng     } - }  the  + intMain () A { the     intN; +scanf"%d", &n); -     intI, temp; $      for(i=0; i<n; i++) $scanf"%d%d", &p[i], &temp); -P[n] =temp; - Matrixchain (n); theprintf"%d\n", m[1][n]); -TraceBack (1, n);Wuyi     return 0; the}

  

[C + +] matrix multiplication, longest common subsequence, maximal sub-Che, longest monotone increment subsequence of dynamic programming

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.