-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixChainMultiplication.java
More file actions
45 lines (31 loc) · 1.19 KB
/
MatrixChainMultiplication.java
File metadata and controls
45 lines (31 loc) · 1.19 KB
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
import java.util.*;
public class MatrixChainMultiplication {
public static int matrixChainOrder(int[] dimensions) {
int n = dimensions.length;
int[][] dp = new int[n][n];
// Initialize dp[i][i] as 0 (since a single matrix has no multiplication cost)
for (int i = 0; i < n; i++) {
dp[i][i] = 0;
}
// Fill dp table diagonally
for (int len = 2; len < n; len++) {
for (int i = 1; i < n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int cost = dp[i][k] + dp[k + 1][j] + dimensions[i - 1] * dimensions[k] * dimensions[j];
if (cost < dp[i][j]) {
dp[i][j] = cost;
}
}
}
}
// The final result is stored in dp[1][n-1]
return dp[1][n - 1];
}
public static void main(String[] args) {
int[] dimensions = {10, 30, 5, 60};
int minCost = matrixChainOrder(dimensions);
System.out.println("Minimum cost of matrix chain multiplication: " + minCost);
}
}