Matrix chain multiplication of dynamic programming (programming) (Chain multiplication)

Source: Internet
Author: User

This problem is the basis of dynamic programming, but also the problem discussed in the introduction of algorithms. Let's briefly describe it here. Suppose there is a set of matrices that need to be multiplied. But we know that the matrix multiplication satisfies the binding law first. So you can do multiplication in different order. And the last multiplication times of multiplication in different order are different. such as 〈A1, A2, a3〉 are 10x100, 100x5, and 5x50 respectively. If the Order of ((A1A2) A3) is calculated, that is, 7,500 times, but if (A1 (A2A3)) The order, the result is 75,000 times ... There are 10 times times the gap here. So this topic means finding a solution that requires the least number of times to multiply.

Now we try to use plain English to continue. The solution to the problem is focused on two areas. One is the least number of times, and the other is the process of minimizing the number of multiplication times. In the code we use M,s to represent each other. Let me start with the meaning of M. M is a two-dimensional array, M[i,j] represents the minimum number of multiplication from matrix I to Matrix J. That is a[i],a[i+1] .... A[J] The minimum number of multiplication times. It can be found that this number should be equal to a[i],a[i+1] ... A[K] * a[k+1] .... A[j-1]a[j], where k is the input I to J a number of this open interval. Finding this number, we also found the minimum number of multiplication.

So how do we find this number? Simply put all the possibilities in the list k=i, i+1, I+2...J try again. You can compare the minimum value. With this idea, we can write the recursive formula:

The p here is an array of your matrix dimensions.

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

#include "stdafx.h" #include <vector> #include <limits> #include <iostream> typedef std::vector< std::vector<int> > Intmatrix; /** * Description:calulate The matrix chain multiplication order * @param p, order of the Matrix demension; * @param m, output value, Save the number of the multiplication for specific i,j. * For instance, the number for n matrix W Ill is stored in m[0][n-1]. * @param s, output value, Save the order of multiplication * For instance, s[i,j]=k, means matrix need to multiple in order M[I...K]*M[K+1....J]; * @return number of the multiplication times. */int Matrixchainorder (const std::vector<int>& p, std::vector < std::vector<int> >& m, std::v Ector < std::vector<int> >&s) {int n = (int) p.size ()-1; m.resize (n); for (int i=0;i<n;i++) m[i].resize (n); S.resize (n); for (int i=0;i<n;i++) s[i].resize (n); for (int l=2;l<=n;l++) {//length of the chain is L for (int i=0;i<n-l+1;i++) {int j=I+L-1; M[I][J] = Std::numeric_limits<int>::max (); for (int k=i;k<j;k++) {int q=m[i][k]+m[k+1][j]+p[i]*p[k+1]*p[j+1], if (Q<m[i][j]) {m[i][j]=q; s[i][j]=k;}}} } return m[0][n-1]; } void Printsequence (const intmatrix& S, int i, int j) {if (i<j) {std::cout<< "("; Printsequence (S, I, s[i][j]); Printsequence (S, s[i][j]+1,j); std::cout<< ")"; } else std::cout<< "m[" <<i<< "]"; } int _tmain (int argc, _tchar* argv[]) {int k[]={30,35,15,5,10,20,25}; std::vector<int> P (k,k+sizeof (k)/sizeof (k[ 0])); Intmatrix m; Intmatrix s; Number of multiplication std::cout<< "Minimum multiplication Times is" <<matrixchainorder (p, M, s) << Std::endl; Backtrack the sequence printsequence (s, 0, (int) p.size ()-2); GetChar (); return 0; }

The problem to be aware of is that the complexity of this algorithm is in O (n∧3). And this algorithm and the introduction of the algorithm, such as the pseudo-code than some subtle differences in the array starting from 0 or 1. is generally consistent. The space complexity here is also O (n∧2). The approximate order of calculation is controlled by the L in this function. l=2, the matrix is computed a[0]*a[1], a[1]*a[2], a[2]*a[3] ... The number of times. L=3 began to calculate a[0]*a[1]*a[2], a[1]*a[2]*a[3]. The number of times. In a more figurative sense, it is the order of inverted triangles. The following diagram is a description of the above process:

Finally, this matrix chain problem is relatively independent of a problem, and other well-known problems are not much associated. Future summaries may have 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.