forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixChainMultiplication.java
47 lines (42 loc) · 1.14 KB
/
MatrixChainMultiplication.java
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
import java.util.*;
import java.io.*;
class MatrixChainMultiplication {
public static void printBrackets(int[][] bracket, int i, int j) {
if(i == j) {
System.out.print(i);
return;
}
System.out.print("(");
printBrackets(bracket, i, bracket[i][j]);
printBrackets(bracket, bracket[i][j]+1, j);
System.out.print(")");
}
public static int findMinCost(int[] arr) {
int[][] dp = new int[arr.length][arr.length];
int[][] bracket = new int[arr.length][arr.length];
for(int l=2; l<arr.length; l++) {
for(int i=1; i<arr.length-l+1; i++) {
int j = i+l-1;
dp[i][j] = Integer.MAX_VALUE;
for(int k=i; k<j; k++) {
int cost = dp[i][k] + dp[k+1][j] + arr[i-1]*arr[k]*arr[j];
if(cost < dp[i][j])
dp[i][j] = cost;
bracket[i][j] = k;
}
}
}
for(int i=0; i<dp.length; i++) {
for(int j=0; j<dp[i].length; j++)
System.out.print(dp[i][j] + " ");
System.out.println();
}
printBrackets(bracket, 1, arr.length-1);
return dp[1][arr.length-1];
}
public static void main(String args[]) {
int arr[] = {2, 3, 6, 4, 5};
int cost = findMinCost(arr);
System.out.println("Answer ==> " + cost);
}
}