forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix Chain Multiplication.java
More file actions
70 lines (54 loc) · 1.73 KB
/
Copy pathMatrix Chain Multiplication.java
File metadata and controls
70 lines (54 loc) · 1.73 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
66
67
68
69
70
class MatrixChainMultiplication {
static int MatrixChainOrder(int p[], int i, int j)
{
if (i == j)
return 0;
int min = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k + 1, j) +
p[i - 1] * p[k] * p[j];
if (count < min)
min = count;
}
// Return minimum count
return min;
}
public static void main(String args[])
{
int arr[] = new int[] { 1, 2, 3, 4, 3 };
int n = arr.length;
System.out.println("Minimum number of multiplications is " + MatrixChainOrder(arr, 1, n - 1));
}
}
class MatrixChainMultiplication {
static int MatrixChainOrder(int p[], int n)
{
int m[][] = new int[n][n];
int i, j, k, L, q;
for (i = 1; i < n; i++)
m[i][i] = 0;
for (L = 2; L < n; L++) {
for (i = 1; i < n - L + 1; i++) {
j = i + L - 1;
if (j == n)
continue;
m[i][j] = Integer.MAX_VALUE;
for (k = i; k <= j - 1; k++) {
// q = cost/scalar multiplications
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n - 1];
}
public static void main(String args[])
{
int arr[] = new int[] { 1, 2, 3, 4 };
int size = arr.length;
System.out.println("Minimum number of multiplications is "
+ MatrixChainOrder(arr, size));
}
}