Search
 
SCRIPT & CODE EXAMPLE
 

CPP

matrix chainmultiplication

#include <bits/stdc++.h>
using namespace std;
int MatrixChainOrder(int p[], int n)
{
	int m[n][n],s[n][n];

	int i, j, k, L, q;

	for (i = 1; i < n; i++)
	{	m[i][i] = 0;
	}
	for (L = 2; L < n; L++)
	{
		for (i = 1; i < n - L + 1; i++)
		{
			j = i + L - 1;
			m[i][j] = INT_MAX;
			for (k = i; k <= j - 1; k++)
			{
				q = m[i][k] + m[k + 1][j]
					+ p[i - 1] * p[k] * p[j];
				if (q <= m[i][j])
				{
					m[i][j] = q;
				}
			}
		}
	}
	cout<<endl<<endl;
    for(int i=1;i<n;i++)
    {
        for(int j=1;j<n;j++)
        {
            if(i<=j) 
            cout<<m[i][j]<<"	";
            else
            cout<<"-1	";
            
        }
        cout<<endl;
    }
    cout << "Minimum number of multiplications is ";
	return m[1][n - 1];
}
int main()
{
	int arr[] = { 4,45,3,2,7,5 };
	int size = sizeof(arr) / sizeof(arr[0]);
	cout <<MatrixChainOrder(arr, size);
	getchar();
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: GoPro camera for kids aus 
Cpp :: return multiple objects from a function C++ using references 
Cpp :: sfml get position 
Cpp :: Maximum Cake Tastiness codeforces solution 
Cpp :: create a table using pointers in C++ 
Cpp :: Explicit conversion casting 
Cpp :: C++ check if thread is joinable 
Cpp :: Extended Euclid Algorithm Recursive Solution 
Cpp :: JAJA 
Cpp :: fasdf 
Cpp :: c++ set value to inf 
Cpp :: copy constructor in c++ questions 
Cpp :: c++ unordered set count 
Cpp :: binary algebra cpp 
Cpp :: type defination in C++ 
Cpp :: count substrings codechef solution in c++ 
Cpp :: unreal ensureMsgf example 
Cpp :: Problems in your to-do list codechef solution in c++ 
Cpp :: c++ vs c# 
Cpp :: get player pawn 
Cpp :: c++ n in regex 
Cpp :: how to get the last digit of a number 
Cpp :: sum of 2 arrays c++ 
Cpp :: round function in c++ 
Cpp :: c++ get last element in array 
Cpp :: Determine if map contains a value for a key c++ 
Cpp :: convert from hex to decimal c++ 
Cpp :: c++ filesystem remove file 
Cpp :: create a bitset of 1024 bits, 
C :: What are the 3 basic types of Plate Boundaries? Explain their differences (how they act). 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =