The problem of matrix multiplication in dynamic programming

Source: Internet
Author: User

Problem Description: Given n matrices (A1,a2,a3 ... An}, where AI and ai+1 are multiplicative, i=1,2,... n-1. A study on the a1a2a3,.... of n matrices with a continuous product An. Because the matrix multiplication satisfies the binding law, the continuous product of the computational matrix can have many different order of calculation, which can be determined by parentheses. The parentheses determine the entire amount of computation (that is, the number of multiplication calls). Therefore, the problem of optimal order of matrix continuous product is naturally proposed.

Naturally, the first thought is to use the enumeration method, the calculation of each kind of parentheses in the case of the computation, take the smallest case. The huge project is conceivable. Back to the source, it will be found that the "enumeration" of this idea is inevitable, only when all the circumstances are considered to compare, the result of the smallest multiplier will appear. One of the most important factors that lead to large projects is the "sub-problem repetition calculation". First, we need to make clear what is the sub-problem of matrix multiplication.

Take A1A2A3A4 as an example, its sub-problem is: A1 A2 A3 A4 a1a2 a2a3 a3a4 a1a2a3 a2a3a4 a1a2a3a4. The optimal order of the A1A2A3A4 is that you must first seek the optimal order of sub-problems with a length of 3, and the sub-problem of length 3 is based on the sub-problem of length 2 (this is a bottom-up recursion). With this push down, you will easily find two interesting phenomena: first, if you have calculated the optimal order of sub-problems with a section length of 3, then the sub-problem in the optimal order is also optimal (you can learn by contradiction); second, after calculating the sub-problem of section 2, and then calculating the sub-problem of 3 You'll also use the result of a sub-problem with a length of 2, so why not save the previous results and avoid repeating the calculations.

The following is the dynamic programming algorithm to solve the problem of matrix Connection code, the idea is nothing more than two points:

First, bottom-up recursion:

It should be noted that: M[i][j] means ai ..... The least number of times for AJ, K for solving ai .... The fracture position of AJ's sub-problem optimal value, PI-1PKPJ represents the multiplier of Aiai+1.....ak and Ak+1....aj multiplied by.

Second, in the calculation process, save the resolved sub-question answer.

#include <iostream>using namespace std;void outPut (int i,int j,int **s) {if (i==j) return;output (i,s[i][j],s); o Utput (s[i][j]+1,j,s);cout<< "Multiply a" <<i<< "," <<s[i][j];cout<< "and A" <<s[i] [j]+1<< "," &LT;&LT;J&LT;&LT;ENDL;}             void Martrixchain (int n,int *p,int **m,int **s) {for (int t=0;t<n;t++) {m[t][t]=0;                                 The case of a single matrix is a matrix number multiplied by 0s[t][t]=-1;}              for (int l=2;l<=n;l++)//L is a segment {for (int i=0;i<=n-l;i++) {int j=i+l-1;     J for the beginning of each paragraph m[i][j]=m[i][i]+m[i+1][j]+p[i]*p[i+1]*p[j+1]; Similar to the function of assigning an initial value, it is advisable to i<=k<j any one of the s[i][j]=i;for (int k=i+1;k<j;k++)//Change the breakpoint, to test the smallest case {if (m[i][k]+m[                            K+1][j]+p[i]*p[k+1]*p[j+1]<m[i][j]) {m[i][j]=m[i][k]+m[k+1][j]+p[i]*p[k+1]*p[j+1];s[i][j]=k; Record the breakpoint position}}}}cout<< "\ n The most rare multiply for" <<m[0][n-1]<<endl<<endl;output (0,n-1,s);} int main () {int num;int *dimension;int **mm;int **ss;cin> >num;dimension=new Int[num+1]; Matrix Dimension mm=new int*[num];ss=new int*[num];for (int i=0;i<num;i++) {mm[i]=new int[num];ss[i]=new int[num];} for (int r=0;r<=num;r++) cin>>dimension[r]; Martrixchain (NUM,DIMENSION,MM,SS);}

The implementation of the program is not difficult, but still have to explain a few easy to err on the details:

1. The array size that represents the number of matrix dimensions: should be the number of matrices +1, reason ... Oh

2. If AI represents the matrix I in the multiplicative matrix, you should always remember that the array subscript is actually starting from 0; (in the program, I start from 0)

These two points can be corrected by debugging, but it is not necessary to spend this time if you can figure it out at the beginning.

The results of the operation are as follows:

Let me explain. Run results a 1, 1 and a 2, 2 means (A1A2) is a chunked (A1A2) (A0 (A1A2)) (A3A4) ((A3A4) A5) (A0A1A2) (A3A4A5) After consideration, the final parentheses are as follows: ((A0 (A1 A2)) ((A3 A4) A5).

Finally, we summarize the features of the Dynamic programming algorithm: 1. Properties of the optimal substructure 2. Overlapping sub-problem properties

(not to be mentioned here, as reflected in the problem analysis).

In particular, I mentioned in the problem analysis of the word "enumeration", the individual think that dynamic planning is "disguised enumeration", but it through small-scale step-by-step enumeration in narrowing the scope, and thus reduce the repeated enumeration, can achieve this goal is based on the above two elements, and dynamic planning can get the correct answer, It is because it has already considered comparing all possible situations (which is also unavoidable to solve any problem, but some are implicitly compared).

There is also a very deficient place: how to output parentheses directly in the expression? such as ((A0 (A1 A2)) ((A3 A4) A5). If you ignore the cost of space, open a few more arrays, the idea of using the queue, can be achieved, could there be a more concise method? ~~~communicating!!!!!!!!

The problem of matrix multiplication in 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.