forked from IEEESFIT1/OpenOctober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix Chain Multiplication.cpp
43 lines (36 loc) · 1.08 KB
/
Matrix Chain Multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
/* A naive recursive implementation that simply follows
the above optimal substructure property */
class MatrixChainMultiplication
{
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
static int MatrixChainOrder(int p[], int i, int j)
{
if (i+1 == j)
return 0;
int min = Integer.MAX_VALUE;
// place parenthesis at different places between first
// and last matrix, recursively calculate count of
// multiplications for each parenthesis placement and
// return the minimum count
for (int k=i+1; k<j; k++)
{
int count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k, j) +
p[i]*p[k]*p[j];
if (count < min)
min = count;
}
// Return minimum count
return min;
}
// Driver program to test above function
public static void main(String args[])
{
int arr[] = new int[] {40, 20, 30, 10, 30};
int n = arr.length;
System.out.println("Minimum number of multiplications is "+
MatrixChainOrder(arr, 0, n-1));
}
}