-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix-Chain-Multiplication.cpp
47 lines (44 loc) · 1.42 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
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
// Function to find the most efficient way to multiply
int solve(vector<int> &arr, int start, int end, vector<vector<int>> &dp) {
// If there is only one matrix
// then cost will be zero
if(start + 2 > end) {
return 0;
}
// If there are only two matrices
if(start + 2 == end) {
// Cost is number of multiplications
return arr[start] * arr[start + 1] * arr[end];
}
// If already calculated
if(dp[start][end] != -1) {
// Return the value
return dp[start][end];
}
int res = INT_MAX;
// Try every possible combination
for(int i = start + 1; i < end; i++) {
// Cost of multiplying the two matrices
// which are obtained after partitioning
int temp = arr[start] * arr[i] * arr[end];
// Add the cost of multiplying the left part
temp += solve(arr, start, i, dp);
// Add the cost of multiplying the right part
temp += solve(arr, i, end, dp);
// Update the minimum value
res = min(res, temp);
}
// Return the minimum value
return dp[start][end] = res;
}
int matrixMultiplication(vector<int> &arr, int N)
{
// Create a dp array
// dp[i][j] stores the minimum cost of
// multiplying the matrices from i to j
vector<vector<int>> dp(N, vector<int>(N, -1));
// Return the minimum cost
return solve(arr, 0, N - 1, dp);
}