The dynamic programming of reading notes in the introduction of algorithms-matrix chain multiplication

Source: Internet
Author: User
Tags scalar

Today, we will study dynamic programming algorithm and learn how to solve the problem of matrix chain multiplication with dynamic programming. First, the matrix multiplication algorithm is reviewed and the implementation process of C + + language is given. Then the dynamic programming algorithm is used to analyze the matrix chain multiplication problem and the C language realization process is given.

1. Matrix multiplicationAs you can see from the definition: axB is only meaningful if the number of columns in matrix a is equal to the number of rows in matrix B . A mxr Matrix A is left multiply by an Rxn Matrix Band will get a mxn Matrix C。 In a computer, a matrix is simply a two-dimensional array. The matrix of a M row R column can be multiplied by an R row N column matrix, resulting in a M-row N-column matrix, where the number of rows J of row I is equal to the sum of R on the first row of the previous matrix and the number of R on the next matrix J column, corresponding to the sum of all r products after multiplying. The complete two matrix multiplication is implemented in the C + + language, as shown in the following procedure:
 1 #include <iostream> 2 using namespace std; 3 #define A_ROWS 3 4 #define A_COLUMNS 2 5 #define B_ROWS 2 6 #define B_COLUMNS 3 7 void Matrix_mult iply (int a[a_rows][a_columns],int b[b_rows][b_columns],int c[a_rows][b_columns]);                                 8 int Main () 9 {ten int a[a_rows][a_columns] = {1,0,11 1,2,12 1,1};13 int B[b_rows][b_columns] = {1,1,2,14 2,1,2};15 int C[a_rows][b_colu MNS] = {0};16 matrix_multiply (a,b,c); + for (int i=0;i<a_rows;i++) (Int. j=0;j<b_columns; J + +) cout<<c[i][j]<< ""; cout<<endl;22}23 return 0;24}25 void matrix_m         ultiply (int a[a_rows][a_columns],int b[b_rows][b_columns],int C[a_rows][b_columns]) + {if (a_columns! = B_ROWS) 28 cout<< "error:incompatible dimensions." <<endl;29 else30 {int i,j,k;32 for (i=0;i<a_rowsi++) for (j=0;j<b_columns;j++), {c[i][j] = 0;36 for (k=0;k <a_columns;k++) PNs C[i][j] + = a[i][k] * B[k][j]; Sum the product of each column of each row of a with the sum of each row of each column of B 38}39}40}

The program test results are as follows:

2, matrix chain multiplication Problem description

A chain <a1,a2,a3 of a given n matrix,....... An>, where i=1,2,... N, the dimension of matrix A is PI-1PI, and the product a1a2 ... An adds all parentheses in a way that minimizes scalar multiplication times.

Note: In the matrix chain multiplication problem, the matrix is not actually multiplied to determine a matrix multiplication order with the least cost. Finding such a binding order makes the cost of multiplication the lowest.

3. Dynamic Programming Analysis Process

1) The structure of the best plus all brackets

The first step in dynamic planning is to find an optimal substructure. Assuming that the value of Aiai+1....aj is now calculated, there is a certain K-value (I<=K<J) in the AI...J process that will divide the ai...j into two parts, thus minimizing the amount of AI...J calculation. Divided into two sub-problems AI...K and AK+1...J, it is necessary to continue to find the optimal solution of these two sub-problems recursively.

An analysis can be made to the optimal substructure: Suppose that an optimal plus full parenthesis of Aiai+1....aj separates the product between AK and ak+1, then AI. K and AK+1..J are also the best plus full brackets.

2) A recursive solution

Set M[I,J] to the minimum value of the number of scalar multiplication operations required for the computer matrix AI...J, A1 for this calculation. The minimum cost of n is M[1,n]. It is now necessary to recursively define M[I,J], which can be discussed in two different situations:

When i==j: m[i,j] = 0, (contains only one matrix at this time)

When i<j: You need to look for a K (I≤k

3) Calculate the optimal cost

Although the process of recursive solution is given, it is implemented without recursive implementation, but with the help space and bottom-up table. The dimension of the set matrix AI is PI-1PI,I=1,2.....N. The input sequence is: P=<p0,p1,... pn>,length[p] = n+1. Use M[n][n] to save the cost of M[i,j], s[n][n "Save the calculation M[i,j] at the best cost of the value of K, and finally can be used in S records to construct an optimal solution. The pseudo-code of the calculation process is given in the book, which is excerpted as follows:

1 Maxtrix_chain_order (P) 2   n = length[p]-1; 3 for   I=1 to n 4 does       m[i][i] = 0; 5 for   t = 2 to n  //t is t He chain length 6 does for        I=1 to n-t+1 7                      j=i+t-1, 8                      m[i][j] = maxlimit; 9 for                      k=i to j-110                             q = m[i][k ] + m[k+1][i] + qi-1qkqj;11                             if q < m[i][j]12 then                                m[i][j] = q;13                                     s[i][j] = k;14   return m and S;

The Matrix_chain_order has a loop nesting with a depth of 3 layers and an O (N3) run time. If implemented recursively, you need to point to a time Ω (2n), because there is some repetition in the middle of the calculation. recursion is calculated exactly as the recursive formula obtained in the second step, and the recursive implementation is as follows:

1 int recursive_matrix_chain (int *p,int i,int j,int m[n+1][n+1],int s[n+1][n+1]) 2 {3     if (i==j) 4        m[i][j] = 0; 5
   else 6     {7         int k; 8         m[i][j] = MAXVALUE; 9 for         (k=i;k<j;k++) Ten         {one             int temp = Recursive_ Matrix_chain (p,i,k,m,s) +recursive_matrix_chain (p,k+1,j,m,s) + p[i-1]*p[k]*p[j];12             if (temp < m[i][j]) 13             {                 M[i][j] = temp;15                 s[i][j] = k;16             }17}18     }19     return m[i][j];20}

For the improvement of recursive calculation, we can introduce the memo, adopt the top-down strategy, maintain a table that records the sub-problem, and control the structure like recursive algorithm. The complete program looks like this:

1 int memoized_matrix_chain (int *p,int m[n+1][n+1],int s[n+1][n+1]) 2 {3     int i,j; 4 for     (i=1;i<=n;++i) 5         F or (J=1;J<=N;++J) 6         {7            m[i][j] = MAXVALUE; 8         } 9     return Lookup_chain (p,1,n,m,s);}11 int Lookup_c Hain (int *p,int i,int j,int m[n+1][n+1],int s[n+1][n+1]) {     if (M[i][j] < MAXVALUE)         return m[i][j]; Direct return, equivalent to look up     if (i = = j)         M[i][j] = 0;18     else19     {         int k;21 for         (k=i;k<j;++k) 22         {$             temp = Lookup_chain (p,i,k,m,s) +lookup_chain (p,k+1,j,m,s) + p[i-1]*p[k]*p[j];  Calculated by recursion, calculated only once, the second check table gets the             if (temp < m[i][j]) (                 m[i][j] = temp;27                 s[i][j] = k;28             }         }30     }31     return m[i][j];32}

4) construct an optimal solution

The minimum cost is calculated in the third step, and the relevant record information is saved. Therefore, only a recursive call to the S table can be used to get an optimal solution. The pseudo-code is given in the book and is excerpted as follows:

1 Print_optimal_parens (S,I,J) 2   if i== J 3 then      PRINT "Ai" 4   else5      PRINT "("; 6      Print_optimal_parens (S,i,s[i][j]); 7      Print_optimal_parens (S,S[I][J]+1,J); 8      PRINT ")";

4, programming implementation

This process is implemented in the C + + language, with existing matrices A1 (30x35), A2 (35x15), A3 (15x5), A4 (5x10), A5 (10x20), A6 (20x25), p=<30,35,15,5,10,20,25>. The implementation process defines two-dimensional arrays m and s, in order to facilitate calculation of their first and first columns are ignored, row and column labels are 1 start. The complete program is as follows:

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

The program test results are as follows:

5. Summary

The problem of dynamic programming is the analysis process, the difficulty lies in how to discover the structure of its sub-problems and the recursive solution of sub-problems. It takes a lot of thinking, not a short time to understand. In the implementation process encountered problems is the array, array subscript problem is a more troublesome thing, how can be reasonable to deal with, need a certain skill.

From:http://www.cnblogs.com/anker/archive/2013/03/10/2952475.html

The dynamic programming of reading notes in the introduction of algorithms-matrix chain multiplication

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.