-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp2Agglomeritive.java
More file actions
84 lines (72 loc) · 2.75 KB
/
Exp2Agglomeritive.java
File metadata and controls
84 lines (72 loc) · 2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package zxcdwm;
public class Exp2Agglomeritive {
static double ecl[][];
static String names[];
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
double a[][] = {{0.4, 0.53}, {0.22, 0.38}, {0.35, 0.32}, {0.26, 0.19}, {0.08, 0.41}, {0.45, 0.30}};
names = new String[6];
for (int i = 0; i < 6; i++) {
System.out.println("P" + i + " " + a[i][0] + " " + a[i][1]);
names[i] = "P" + i;
}
System.out.println("\nAdj Matrix");
ecl = new double[6][6];
double min = 9999;
int m1 = -99, m2 = -99;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < ecl.length; j++) {
ecl[i][j] = Math.pow((Math.pow(a[i][0] - a[j][0], 2) + Math.pow(a[i][1] - a[j][1], 2)), 0.5);
System.out.print(Math.round(ecl[i][j] * 100D) / 100D + "\t");
if (min > ecl[i][j] && ecl[i][j] != 0) {
min = ecl[i][j];
m1 = i;
m2 = j;
}
}
System.out.println();
}
System.out.println("MINIMUM=" + Math.round(min * 100D) / 100D + " ");
for (int t = 0; t < 5; t++) {
System.out.println("--------Iteration " + t + "----------");
updateTable(m1, m2, min);
printTable(ecl);
min = 9999;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < ecl.length; j++) {
if (min > ecl[i][j] && ecl[i][j] != 0) {
min = ecl[i][j];
m1 = i;
m2 = j;
}
}
}
System.out.println("MINIMUM=" + Math.round(min * 100D) / 100D + " ");
}
}
private static void updateTable(int m1, int m2, double min) {
System.out.println(names[m1] + " and " + names[m2] + " will be merged ");
names[m1] = names[m1] + names[m2];
names[m2] = null;
ecl[m1][m2] = 0;
for (int i = 0; i < ecl.length; i++) {
ecl[m2][i] = 0;
ecl[m1][i] = min(ecl[i][m1], ecl[i][m2]);
}
}
private static double min(double ecl, double ecl0) {
if (ecl > ecl0) {
return ecl0;
}
return ecl;
}
private static void printTable(double[][] ecl) {
for (int i = 0; i < ecl.length; i++) {
System.out.print(names[i] + " ");
for (int j = 0; j <= i; j++) {
System.out.print("\t" + Math.round(ecl[i][j] * 100D) / 100D + " ");
}
System.out.println("\t");
}
}
}