-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.cpp
40 lines (35 loc) · 850 Bytes
/
q2.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
//5 Dynamic Programming
//5.2. Implement the Top Down Dynamic Programming Memoization Approach for matrix chain multiplication.
//SOURCE techiedelight.com
#include <iostream>
#include <climits>
using namespace std;
#define MAX 10
int lookup[MAX][MAX];
int MatrixChainMultiplication(int dims[], int i, int j)
{
if (j <= i + 1)
return 0;
int min = INT_MAX;
if (lookup[i][j] == 0)
{
for (int k = i + 1; k <= j - 1; k++)
{
int cost = MatrixChainMultiplication(dims, i, k);
cost += MatrixChainMultiplication(dims, k, j);
cost += dims[i] * dims[k] * dims[j];
if (cost < min)
min = cost;
}
lookup[i][j] = min;
}
return lookup[i][j];
}
int main()
{
int dims[] = {30, 40, 21, 5, 6, 10 };
int n = sizeof(dims) / sizeof(dims[0]);
cout << "Answer: " << MatrixChainMultiplication(dims, 0, n - 1);
cout<<endl;
return 0;
}