Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // matrix chain multiplication
- // dimensions of matrix are given [40 ,20 ,30,10,30] // find min cost of multiplication
- // 2 X 3 : 3 X6 multiple kara to new matrix 2 X 6 ki bani
- // iski cost 2 X3 X6 hogi
- // so move a partition and choose them solve them apply a condn
- // [40, 20, 30 ,10,30] to Array 1 is 40 X 20 ... array 2 is 20 X 30
- // so arr1 X (rest) gives diff result than (arr1 X arr2 ) *(rest)
- int dp[100][100];
- int mcm(vector<int>arr ,int i, int j){
- if(i>=j) return 0;
- if(dp[i][j]!= -1) return dp[i][j];
- int mn = INT_MAX;
- // arr_i = i-1 X i;
- for(int a=i;a<j;a++){
- int cost = mcm(arr,i,a) + mcm(arr,a+1,j) + arr[i-1]*arr[a]*arr[j] ;
- mn = min(mn,cost);
- }
- dp[i][j] = mn;
- return dp[i][j];
- }
- int main(){
- int n = arr.size();
- memset(dp,-1,sizeof(dp));
- int ans = mcm(arr , 1,n-1);
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement