Matrix chain Multiplication)

Source: Internet
Author: User

This problem is the basis of dynamic planning and discussed in the introduction to algorithms. Here is a brief description. Assume that there is a matrix that requires multiplication. But we know that matrix multiplication satisfies the combination law first. So we can perform multiplication in different order. In addition, the final number of multiplications in different order is different. For example 〈A1,A2,A3> 10x100,100x5, and 5 respectively
* 50. If ((A1A2)A3) The order is calculated for 7500 times, but if (A1 (A2A3) In this Order, the result is 75000 times !!! There is a 10-fold gap. Therefore, this question means finding a solution that minimizes the number of times that multiplication is required.

Now let's try again in the vernacular. The solution focuses on two aspects. One is the least multiplication times, and the other is the process of least multiplication times. In the code, we use M, s for representation. Let's talk about the meaning of M. M is a two-dimensional array. M [I, j] represents the minimum multiplication times from matrix I to matrix J. That is, the least multiplication times of a [I], a [I + 1]... a [J. It can be found that this number of times should be equal to a [I], a [I + 1]... A [k] * A [k + 1]... the number of times a [J-1] a [J], where k is the number of open intervals from input I to J. After finding this number, we can find the least multiplication number.

How can we find this number? Simply put, all possibilities are listed as K = I, I + 1, I + 2... J. Compare the minimum value. With this idea, we can write the recursive formula as follows:

Here, p is the array of your matrix dimensions.

Based on this formula, we can write the following code:

 

# Include "stdafx. H "<br/> # include <vector> <br/> # include <limits> <br/> # include <iostream> <br/> typedef STD :: vector <STD: vector <int> intmatrix; <br/>/** <br/> * description: calulate the matrix chain multiplication order <br/> * @ Param P, order of the matrix demension; <br/> * @ Param M, output value, save the number of the multiplication for specific I, j. <br/> * for instance, the number for n matrix will be stored in M [0] [n-1]. <br/> * @ Param S, output value, save the order of multiplication <br/> * For instance, s [I, j] = K, means Matrix need to multiple in order m [I... k] * m [k + 1 .... j]; <br/> * @ return number of the multiplication times. <br/> */<br/> int matrixchainorder (const STD: vector <int> & P, STD: vector <int> & M, STD: vector <int> & S) <br/> {<br/> int n = (INT) p. size ()-1; <br/> M. resize (n); <br/> for (INT I = 0; I <n; I ++) <br/> M [I]. resize (n); <br/> S. resize (n); <br/> for (INT I = 0; I <n; I ++) <br/> S [I]. resize (n); <br/> for (int l = 2; L <= N; l ++) <br/> {<br/> // length of the chain is L <br/> for (INT I = 0; I <n-L + 1; I ++) <br/> {<br/> Int J = I + L-1; <br/> M [I] [J] = STD: numeric_limits <int> :: max (); <br/> for (int K = I; k <j; k ++) <br/> {<br/> int q = m [I] [k] + M [k + 1] [J] + P [I] * P [k + 1] * P [J + 1]; <br/> If (q <m [I] [J]) <br/> {<br/> M [I] [J] = Q; <br/> S [I] [J] = K; <br/>}< br/> return M [0] [n-1]; <br/>}< br/> void printsequence (const intmatrix & S, int I, Int J) <br/>{< br/> if (I <j) <br/>{< br/> STD: cout <"("; <br/> printsequence (S, I, S [I] [J]); <br/> printsequence (S, s [I] [J] + 1, J); <br/> STD: cout <")"; <br/>}< br/> else <br/> STD: cout <"m [" <I <"]"; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> int K [] = {30, 35, 15, 5, 10, 20, 25 }; <br/> STD: vector <int> P (K, K + sizeof (k)/sizeof (K [0]); <br/> intmatrix m; <br/> intmatrix s; <br/> // Number of multiplication <br/> STD :: cout <"minimum multiplication times is" <matrixchainorder (p, M, S) <STD: Endl; <br/> // backtrack the sequence <br/> printsequence (S, 0, (INT) p. size ()-2); <br/> getchar (); <br/> return 0; <br/>}

Note that the complexity of this algorithm is O (n Limit 3 ). In addition, the pseudo-code ratio of this algorithm and introduction to algorithms is slightly different from that of other places, as shown in the fact that the array starts from 0 or 1. It is basically the same. Here, the space complexity also reaches O (n Limit 2 ). The approximate calculation order is controlled by the L in this function. When l = 2, calculate the matrix A [0] * A [1], a [1] * A [2] in sequence, A [2] * A [3]... the number of times. When l = 3, a [0] * A [1] * A [2], a [1] * A [2] * A [3] .. the number of times. In terms of image, it is the inverted triangle order. The following figure describes the process above:

 

Finally, this matrix chain problem is a relatively independent problem and is not closely related to other well-known problems. In the future, there may be many different variants.

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.