Dynamic Programming matrix chain multiplication

Source: Internet
Author: User
Tags define function min

Description

Given n matrices {a1,a2,..., an}, where AI and ai+1 are multiplicative, i=1,2,..., n-1. How to determine the calculation order of the multiplication of the matrix, making it necessary to calculate the number of times of the matrix multiplication in this order.

Input

There are n matrix hyphens, represented by a row with an array of n+1, representing rows of n matrices and columns of n matrices separated by a space.

Output

Your output should have C line, that is, the output of each set of test data is a row, it is the calculated matrix of the minimum number of products, the output of the best total bracket structure

Sample Input

10 100 5 50

Sample Output

7500
((A1A2) A3)

Analysis:

  Matrix chain multiplication Problem Description:   Given a sequence consisting of n matrices [A1,a2,...,an], the product a1a2 ... An, find the parentheses method that minimizes multiplication times.  1) Finding the optimal sub-structure the hardest part of this problem is finding the best sub-structure. The Product a1a2 ... An arbitrary parenthesis method of an will divide the sequence somewhere in two parts, that is, where the last multiplication is calculated, we will write this position as K, that is to say first calculate A1 ... AK and ak+1...an, and then multiply the results of these two parts. The optimal sub-structure is as follows: Suppose a1a2 ... An optimal parenthesis of the product is divided between AK and ak+1, then the prefix sub-chain A1 ... AK's parentheses must be A1 ... An optimal bracket for AK, the suffix strand is the same. The exact location of K is not known at first, and all positions need to be traversed to ensure that the appropriate k is found to divide the product.   2) construct recursive solution set M[I,J] for matrix chain AI ... The cost of the optimal solution of AJ, then          ┌0    if i = j M[i,j] =│          └min (I≤k

#include <stdlib.h> #include <stdio.h> #include <memory.h> #define N 6 void print_optimal_parens (int s[ N+1][n+1],int I,int J)//define function to print the result of the best full parenthesis {if (i==j) printf ("a%d", I); else {printf ("("); Print_optimal_parens (S,i,s[i][j]); Recursive invocation of Print_optimal_parens (S,S[I][J]+1,J) at the division point; printf (")"); }} int main () {int matrix[n+1];//matrix the number of dimensions of the matrix in the record int i,j,k,q; int m[n+1][n+1]; the number of times that the record matrix is connected in//m int s[n+1][n+1];//s[i][j] Recorded to Ai ... AJ divides the optimal K-value for (i=0;i<=n;i++) scanf ("%d", &matrix[i]); Memset (m,0, (n+1) * (n+1) *sizeof (int)); for (j=1;j<=n;j++) for (i=j;i>=1;i--)//When i=j, m[i][j]=0, {//When i<j, M[i][j]=min{m[i][k]+m[k+1][j]+p (i-1) p (k) P (j)} I=<k<j if (j==i) m[i][j]=0; else {m[i][j]=600000; for (k=i;k<j;k++) {q=m[i][k]+m[k+1][j]+matrix[i-1]*matrix[k]*matrix[j]; if (Q<m[i][j]) {m I [J]=q; S[i][j]=k; }}}} printf ("%d/n", M[1][n]); Print_optimal_parens (S,1,n); return 0; }

Another way to implement the optimal matrix chain function:

The function is calculated using the bottom-up method

void Matrixchain () {int I, j, K, T;////////This is the case for initializing the underlying, that is, a matrix///////////for (i = 1; I <= n; i++) M[i][i] = 0;//is assigned a value of 0 because 1 matrices need to be multiplied by 0 times for (int r = 2; r <= N; r++) {//for (i = 1; I <= n-r + 1; i++) {//calculates the minimum number of consecutive r matrices that start with the I matrix J = i + r-1;//a[i:j], continuous R matrix////////////below is one case, break point I, i.e. the first matrix independent/////////////////m[i][j] = m[i + 1][j] + p[i-1] * P[i] * P[J]; Start looking for the best value///////////////////////for (k = i + 1; k < J; k++) {t = M[i][k] + m[k + 1][j] + p[i-1] * P[k] * P[J]; if (T < m[i][j]) m[i][j] = t; } } } }

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.