[C ++] dynamic planning-matrix chain Multiplication

Source: Internet
Author: User

[This article is original to Paul's blog technology blog .]

[This article is for reprinting. for reprinting, please indicate the source in the form of a link .]

[All the articles in this blog have been carefully organized by the bloggers. Please respect the fruits of my work .]

[C ++] dynamic planning-matrix chain Multiplication

 

1. Problem Description

 

Given a chain composed of n matrices, {A1, A2 ,..., An}, where I = 1, 2 ,..., n. the dimension of matrix Ai is pi-1 * pi. How can we determine the order of calculation of matrix concatenation products so that the number of multiplication times required to calculate matrix concatenation products in this order is the least.

 

2. Optimal sub-structure

 

Product A1A2... any Bracket method of An divides the sequence into two parts, that is, the place where the last multiplication is calculated. We will mark this position as k, that is, calculate A1... ak and Ak + 1... and then multiply the results of the two parts.

 

The optimal sub-structure is as follows: Suppose A1A2... the product of An is separated by the product Ak and Ak + 1, then the prefix sub-chain A1... the angle brackets of Ak must be A1... an accesskey is enclosed in parentheses, And the suffix is the same as that of the sub-chain.

 

At the beginning, we didn't know the exact position of k. We had to traverse all the locations to ensure that we could find the appropriate k to separate the product.

 

3. State Transition Equation

 

 

4. Code Implementation

 

1 # include <iostream> 2 using namespace std; 3 // p is the matrix chain, p [0], p [1] represents the first matrix, p [1], p [2] indicates the second matrix. length is the length of p. 4 // if there are six matrices, length = 7, and m is the two-dimensional matrix that stores the optimal results, t is the 5 // two-dimensional matrix 6 void MatrixChainOrder (int * p, int (* m) [10], int (* t) [10], int length) 7 {8 int n = length-1; 9 int I, j, k, q, num = 0; 10 // A [I] [I] has only one matrix, so the number of times of multiplication is 0, that is, m [I] [I] = 0; 11 for (I = 1; I <length; I ++) 12 {13 m [I] [I] = 0; 14} 15 // I represents the length of the matrix chain, I = 2 indicates how to divide two matrices into 16 for (I = 2; I <= N; I ++) 17 {18 // j indicates how to divide the I matrix starting from the j matrix to the optimum 19 for (j = 1; j <= n-I + 1; j ++) 20 {21 // k indicates the number of j I matrices is k, from j to k represents how I matrices between them divide 22 k = j + I-1; 23 // m [j] [k] stores the optimal result from j to k using the optimal division 24 m [j] [k] = 0x7fffffff; 25 // q is the number between j and K-1, the objective is to use q to test the division of the matrix between j and k, 26 // to find the optimal division, this is an experience test. 27 for (q = j; q <= K-1; q ++) 28 {29 num = m [j] [q] + m [q + 1] [k] + p [J-1] * p [q] * p [k]; 30 if (num <m [j] [k]) 31 {32 m [j] [k] = num; 33 t [j] [k] = q; 34} 35} 36} 37} 38} 39 void PrintAnswer (int (* t) [10], int I, int j) 40 {41 if (I = j) 42 {43 cout <"A" <I; 44} 45 else46 {47 cout <"("; 48 PrintAnswer (t, I, t [I] [j]); 49 PrintAnswer (t, t [I] [j] + 1, j); 50 cout <")"; 51} 52 53} 54 int main () 55 {56 int p [7] = {30, 35, 15, 5, 10, 20, 25}; 57 int m [10] [10], t [10] [10]; 58 MatrixChainOrder (p, m, t, 7); 59 MatrixChainOrder (p, m, t, 7); 60 PrintAnswer (t, 1, 6 ); 61 cout <endl; 62 return 0; 63}

 

 

 

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.