-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixChain.java
46 lines (38 loc) · 1.5 KB
/
MatrixChain.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
import java.util.*;
public class MatrixChain {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of matrices: ");
int n = s.nextInt();
int[] rows = new int[n];
int[] columns = new int[n];
s.close();
for (int i = 0; i < n; i++) {
System.out.print("Enter the number of rows for matrix " + (i + 1) + ": ");
rows[i] = s.nextInt();
System.out.print("Enter the number of columns for matrix " + (i + 1) + ": ");
columns[i] = s.nextInt();
}
System.out.println("\n");
for(int i = 0; i < n; i++){
System.out.println("The size of matrix " + i + " is: " + rows[i] + " * " + columns[i]);
}
System.out.println("\nMinimum number of multiplications is: " + matrixChain(rows, columns, n));
}
static int matrixChain(int rows[], int columns[], int n) {
int[][] dp = new int[n][n];
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
int k = j + i;
dp[j][k] = Integer.MAX_VALUE;
for (int x = j; x < k; x++) {
int count = dp[j][x] + dp[x + 1][k] + rows[j] * columns[x] * columns[k];
if (count < dp[j][k]) {
dp[j][k] = count;
}
}
}
}
return dp[0][n - 1];
}
}