-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixChainMultiplication.java
More file actions
65 lines (53 loc) · 1.75 KB
/
MatrixChainMultiplication.java
File metadata and controls
65 lines (53 loc) · 1.75 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package java_algo;
import java.util.*;
/* A naive recursive implementation that simply follows
the above optimal substructure property */
class MatrixChainMultiplication {
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
static String[] MatrixChainOrder(int p[], int i, int j) {
String str[] = new String[2];
str[0] = "0";
if (i == j)
return str;
int min = Integer.MAX_VALUE;
// System.out.println("...................");
for (int k = i; k < j; k++) {
int count = Integer.parseInt(MatrixChainOrder(p, i, k)[0])
+ Integer.parseInt(MatrixChainOrder(p, k + 1, j)[0]) + p[i - 1] * p[k] * p[j];
if (count < min) {
min = count;
str[0] = String.valueOf(min);
String a = MatrixChainOrder(p, i, k)[1];
if (MatrixChainOrder(p, i, k)[1] == null) {
a = "0";
}
String b = MatrixChainOrder(p, k + 1, j)[1];
if (MatrixChainOrder(p, k + 1, j)[1] == null) {
b = "0";
}
str[1] = a + " + " + b + " + " + p[i - 1] + "*" + p[k] + "*" + p[j];
}
}
// System.out.println(str[1] + " count " + str[0]);
// Return minimum count
return str;
}
// Driver code
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter No Of Matrix To Be Multipied:");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter Size Array : ");
System.out.println("(((( e.g. Suppose There are 4 matrices of dimensions 40x20, 20x30, 30x10 and 10x30."
+ "then Size Array is [[40, 20, 30, 10, 30]] ))))");
int i = 0;
while (i < n) {
int ai = sc.nextInt();
arr[i] = ai;
i++;
}
System.out.println("\n \n Minimum number of multiplications is " + MatrixChainOrder(arr, 1, n - 1)[0] + "\n"
+ "obtained by " + MatrixChainOrder(arr, 1, n - 1)[1]);
}
}