The problem of "matrix chain multiplication" in dynamic programming of "Introduction to Algorithms"

Source: Internet
Author: User

in the previous article, we introduced the " pipe cutting" problem of dynamic programming , this time to look at "matrix chain multiplication". The so-called matrix chain multiplication is a continuous multiplication of one or more matrices, the main concern here is the number of multiplication.


I. Overview


With two matrices multiplied as an example, A1*A2,A1 and A2 are two matrices, assuming that the number of rows A1 is P*q,a2 is q*r. Note here that because it is A1 times A2, so the number of columns of A1 is equal to the number of rows of A2, otherwise it can not do matrix multiplication, meet the above conditions of the matrix, we call "compatible". So, for A1*A2, we need to execute the row elements of P*r A1 respectively multiplied by the A2 column elements, according to the linear algebra knowledge, it is not difficult to conclude that we need to perform p*q*r multiplication.


With two matrices multiplied, once the size of the matrix is determined, the number of multiplication times required is determined. So what about more than two matrices? Is that what it is. In fact, for multiple matrices multiplied, the number of times the multiplication was performed was related to "partitioning". For example:


Taking matrix chain <A1,A2,A3> as an example, it is assumed that the size of the three matrices is 10x100,100x5 and 5x50 respectively.


① is divided into ((A1*A2) *a3), multiply execution times: 10*100*5+10*5*50=5000+2500=7500 times


② in (a1* (A2*A3)) mode, multiply execution times are: 100*5*50+10*100*50=25000+50000=75000 times


We can see that, for the same matrix chain <A1,A2,A3> multiplication, different division, multiply the number of times actually difference 10 times times.



second, how to obtain the best matrix chain multiplication and minimum number of times


In fact, this is similar to "pipe cutting", in the steel tube cutting problem, we use a one-dimensional array to store the best revenue, another one-dimensional array to store the division of cutting.


Here, we can also consider the matrix chain as a "steel pipe" to be split, but the two arrays recorded here need to be two-dimensional, because we need to record not only where to "disconnect", but also to record "each section" to where the cutoff. Such as:




Use a one-dimensional array of length n+1 p to record the scale of each matrix, where n is the range of the matrix subscript I 1~n, for example, for matrix AI, it should be p[i-1] to p[i]. Since I is a value from 1 to N, the subscript of the array p is from 0 to N.


The structure used to store the minimum number of multiplication executions and the best segmentation method is two-dimensional arrays m and s, all from the 1~n value. M[I][J] records the minimum number of multiplication executions of the matrix chain <ai,ai+1,..., aj>, while S[i][j] records the split point K of the highest quality m[i][j].


One thing to note is that when i=j, m[i][j]=m[i][i]=0, because a matrix does not require any multiplication.


Suppose the matrix chain from Ai to AJ, there are j-i+1 matrices, we separate from the K, the matrix chain is divided into Ai~ak and ak+1 to AJ two blocks, then we can easily give m[i][j] from the K-delimited formula:


M[I][J]=M[I][K]+M[K+1][J]+P[I-1]*P[K]*P[J];


In the case of a set of determined I and J values, to minimize the value of m[i][j], we just need to i<=k<j in all k values, looking for a minimum value for M[I][J].


Suppose L is the length of the matrix chain, then l=j-i+1. When L=1, there is only one matrix and no calculation is required. Then we can cycle from l=2 to n, each reasonable combination of I and J values, traverse all k values corresponding to the m[i][j] value, the smallest one recorded, stored in m[i][j], and the corresponding k stored in the s[i][j], we have the desired results.


According to the above analysis, it is not difficult to give the code of the process, note here is also the use of the bottom-up method, see "Pipe cutting":


The ranks of the AI matrix are p[i-1] and p[i],1<=i<=n/* * Solving a minimum number of multiplication bracket schemes */void matrix_chain (int* p, int n, int** m, int** s) {//① values on the diagonal The first assignment is 0for (int i = 1; I <= n; i++) {m[i][i] = 0;} int l = 0; L for the length of the matrix chain//m[i][j] The first parameter int i = 0;//m[i][j] of the second parameter int j = 0;int tmp = 0;//② is divided by length L, l starting from 2 to nfor (L = 2; l <= N; l++) {// Ring the first parameter, because L is at least 2 in length, so I and J must not be equal in this loop for (i = 1; I <= n-l + 1; i++) {//Because j-i+1=l, so j=l+i-1j = i + l-1;//to M[i][j] assigns an initial value, which  To find the minimum value of m[i][j], it should be assigned to M[I][J] a positive infinity, but here directly assigned a i=j time of the special value can also m[i][j] = M[i][i] + m[i + 1][j] + p[i-1] * p[i] * P[J];S[I][J] = i;//for each particular combination of I and J, traverse all the appropriate K values at this time, K is greater than or equal to I less than jfor (int k = i + 1; k < J; k++) {//k cannot be equal to J, because the back is m[k+1][j], otherwise k+1 is larger than J. tmp = M[i ][k] + m[k + 1][j] + p[i-1] * p[k] * P[J];IF (TMP < M[I][J]) {m[i][j] = tmp;s[i][j] = k;}}}}


after the above code, we have obtained the minimum multiplication times and corresponding best division S[I][J] for each combination of I and J.



third, the optimal structure of the output division:


After running the above code, we are ready to s[i][j], which contains the best segmentation information. We can use a method similar to the middle order traversal to output the partitioning method, for example, for <A1,A2,A3,A4,A5> and their corresponding subscript array p.


void Print_optimal_parens (int** s, int i, int j) {if (i = = j) {cout << "A" << I;} else {cout << "(";p Rin T_optimal_parens (S, I, S[i][j]);p Rint_optimal_parens (S, s[i][j] + 1, j); cout << ")";}


For example, for arrays p={5,6,2,9,7,6} and <A1,A2,A3,A4,A5> through the above two code calls, the output partition result:


(( a1a2) ((A3A4) A5))


Minimum multiplication: 330 times





The problem of "matrix chain multiplication" in dynamic programming of "Introduction to Algorithms"

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.