-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMCP.java
More file actions
136 lines (119 loc) · 3.44 KB
/
MCP.java
File metadata and controls
136 lines (119 loc) · 3.44 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.ub.dynamic;
import org.omg.CORBA.INTERNAL;
public class MCP {
// m[i,j] = 0 if i==j
// = min i<k<j {m[i,k]+m[k+1,j]+di-1.dk.dj} if i<j
// = undefined
private int dummyCalculation(int input[]) {
int noOfMatrices = 6;
int UNDEFINED = 0;
int m[][] = new int [7][7];
int passes = noOfMatrices-1; //no of parenthesis
//Initialization not necessary
// for (int i=1;i<=noOfMatrices;i++) {
// for (int j=1;j<=noOfMatrices;j++) {
// if(i == j){
// m[i][j] = 0;
// }else if(i > j) {
// m[i][j] = UNDEFINED;
// }
// }
// }
int path[][] = new int[7][7];
// We can't fill the matrix like normal matrix horizontally it should be fill diagonally
for (int p=0;p<=passes;p++) {
for (int i=1,j=1+p;i<=noOfMatrices-p;i++,j++) {
int min = Integer.MAX_VALUE;
for(int k=i;k<j;k++) {
int current_min = (m[i][k]+m[k+1][j])+(input[i-1]*input[k]*input[j]);
if(min > current_min) {
min = current_min;
m[i][j] = min;
path[i][j] = k;
}
}
}
}
// //Print the matrices
// for (int i=1;i<noOfMatrices+1;i++) {
// for (int j=1;j< noOfMatrices+1;j++) {
// System.out.print(m[i][j] + "\t");
// }
// System.out.print("\n");
// }
// System.out.println();
// //Print the paths
// for (int i=1;i<noOfMatrices+1;i++) {
// for (int j=1;j< noOfMatrices+1;j++) {
// System.out.print(path[i][j] + "\t");
// }
// System.out.print("\n");
// }
return m[1][noOfMatrices];
}
private int calculateNoofMultiplication(int input[]) {
int noOfMatrices = 6;
int UNDEFINED = 0;
int m[][] = new int [7][7];
/**
* We can't fill the matrix like normal matrix horizontally it should be fill diagonally
*/
// for (int i=1;i<=noOfMatrices;i++) {
// for (int j=1;j<=noOfMatrices;j++) {
// if(i == j){
// m[i][j] = 0;
// }else if(i > j) {
// m[i][j] = UNDEFINED;
// }else if(i < j) {
// int min = Integer.MAX_VALUE;
// for(int k=i;k<j;k++) {
// int current_min = (m[i][k]+m[k+1][j])+(input[i-1]*input[k]*input[j]);
// if (min > current_min) {
// m[i][j] = current_min;
// min = current_min;
// }
// }
// }
// System.out.print(m[i][j] + "\t");
// }
// System.out.println();
// }
for (int i=1;i<=noOfMatrices;i++) {
for (int j=1;j<=noOfMatrices;j++) {
if(i == j){
m[i][j] = 0;
}else if(i > j) {
m[i][j] = UNDEFINED;
}
}
}
int passes = noOfMatrices -1; //no of parenthesis
for (int i=1;i<=passes;i++) {
for (int j=1;j<noOfMatrices;j++) {
int min = Integer.MAX_VALUE;
for(int k=i;k<=j;k++) {
int current_min = (m[i][k]+m[k+1][j])+(input[i-1]*input[k]*input[j]);
if (min > current_min) {
m[j][j+1] = current_min;
min = current_min;
}
}
}
}
for (int i=1;i<noOfMatrices+1;i++) {
for (int j=1;j< noOfMatrices+1;j++) {
System.out.print(m[i][j] + "\t");
}
System.out.print("\n");
}
return m[1][noOfMatrices];
}
public static void main(String args[]) {
//matrix is represented by the array
int input[] = {10,20,1,40,5,30,15}; // there are 6 matrices it is just a representation
MCP mcp = new MCP();
// int cost = mcp.calculateNoofMultiplicatio(input);
int cost = mcp.dummyCalculation(input);
System.out.println("total no of calulcation = "+cost);
}
}