vendredi 12 juillet 2019

What will be the the order in which matrices should be multiply?

This is Matrix Chain Multiplication algorithm code for printing number of multiplication required for multiplying matrices. What will be the code for determining order in which matrices should be multiply.

#include<bits/stdc++.h> 
using namespace std; 
int MatrixChainOrder(int p[], int i, int j) 
{ 
if(i == j) 
    return 0; 
int k; 
int min = INT_MAX; 
int count; 

for (k = i; k < j; k++) 
{ 
    count = MatrixChainOrder(p, i, k) + 
            MatrixChainOrder(p, k + 1, j) + 
            p[i - 1] * p[k] * p[j]; 

    if (count < min) 
        min = count; 
} 


return min; 
} 

// Driver Code 
int main() 
{ 
int arr[] = {1, 2, 3, 4, 3}; 
int n = sizeof(arr)/sizeof(arr[0]); 

cout << "Minimum number of multiplications is "
     << MatrixChainOrder(arr, 1, n - 1); 
}

Aucun commentaire:

Enregistrer un commentaire