-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgglomerativeClustering1.java
More file actions
78 lines (67 loc) · 2.76 KB
/
AgglomerativeClustering1.java
File metadata and controls
78 lines (67 loc) · 2.76 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
//Agglomerative Clustering (Method 2)
import java.util.Scanner;
public class AggloClustering {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("Enter number of points: ");
int N = kbd.nextInt(), M = 2;
double[][] data = new double[N][M];
String[] names = new String[N];
// Read points and store coordinates
for (int i = 0; i < N; i++) {
names[i] = "p" + (i + 1);
System.out.print("Enter x and y for " + names[i] + ": ");
data[i][0] = kbd.nextDouble(); // X coordinate
data[i][1] = kbd.nextDouble(); // Y coordinate
System.out.println(names[i] + ": X = " + data[i][0] + " Y = " + data[i][1]);
}
System.out.println("Enter Choice : 1.Single 2.Complete 3.Average");
int choice = kbd.nextInt();
double[][] d = new double[N][N];
double INF = Double.POSITIVE_INFINITY;
// Initialize distance matrix
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) d[i][j] = i == j ? INF : dist(data[i], data[j]);
// Clustering process
for (int s = 0; s < N - 1; s++) {
int i1 = 0, i2 = 1; // Start with the first two clusters
// Find the closest pair of clusters
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (d[i][j] < d[i1][i2]) {
i1 = i;
i2 = j;
}
}
}
// Print the clusters being merged
System.out.println("Cluster " + names[i1] + " and " + names[i2]);
// Update distances based on the chosen method
if (choice == 1) { // Single Linkage
for (int j = 0; j < N; j++) {
d[i1][j] = Math.min(d[i1][j], d[i2][j]);
d[j][i1] = d[i1][j]; // Symmetric
}
} else if (choice == 2) { // Complete Linkage
for (int j = 0; j < N; j++) {
d[i1][j] = Math.max(d[i1][j], d[i2][j]);
d[j][i1] = d[i1][j]; // Symmetric
}
} else if (choice == 3) { // Average Linkage
for (int j = 0; j < N; j++) {
d[i1][j] = (d[i1][j] + d[i2][j]) / 2;
d[j][i1] = d[i1][j]; // Symmetric
}
}
// Mark the merged cluster as INF
for (int j = 0; j < N; j++) {
d[i2][j] = INF;
d[j][i2] = INF;
}
}
kbd.close();
}
static double dist(double[] a, double[] b) {
return Math.sqrt(Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2));
}
}