Before introducing the Minimum Spanning Tree Algorithm (MST), let's briefly talk about the implementation process of the average link algorithm (average-link). The average link clustering algorithm is similar to that of a single link, additional steps for calculating the distance matrix between clusters
The implementation steps are as follows:
1. Separate the elements into one group and put these groups into the container H.
2. loop element distance array. Obtain the two elements A and B to be compared based on the two-layer subscript.
3. Search for groups AH and BH that contain A and B respectively in H. If AH is not equal to BH (that is, the distance between A and B), the distance between AH and BH is the distance between A and B.
4. After obtaining the distance array between groups, compare the distance and threshold between groups cyclically. If the distance is smaller than the threshold value, the two are combined into a group, before merging, delete the two in H as the original group for comparison.
The MST algorithm is interesting. It is used not only for clustering, but also for the shortest paving cost.
Let's assume a scenario: What is the shortest distance to build a network between multiple cities? Each city is regarded as a data point, and the distance between each point is called an edge. The shortest distance is actually the case where every point can be connected to an edge but it does not loop.
The implementation process is as follows:
1. First, create the City class and edge class, as shown below:
01
/**
02
* City
03
*
04
* <A href = "http://my.oschina.net/arthor" class = "referer" target = "_ blank"> @ author </a> duyf
05
*
06
*/
07
Class City {
08
09
Private String name;
10
// Longitude
11
Private double x;
12
13
// Latitude
14
Private double y;
15
16
Public double getX (){
17
Return x;
18
}
19
20
Public void setX (double x ){
21
This. x = x;
22
}
23
24
Public double getY (){
25
Return y;
26
}
27
28
Public void setY (double y ){
29
This. y = y;
30
}
31
32
Public String getName (){
33
Return name;
34
}
35
36
Public void setName (String name ){
37
This. name = name;
38
}
39
40
Public boolean equals (Object obj ){
41
If (obj = null ){
42
Return false;
43
}
44
If (this = obj ){
45
Return true;
46
}
47
City other = (City) obj;
48
If (this. getX () = other. getX () & this. getY () = other. getY ()){
49
Return true;
50
}
51
Return false;
52
}
53
}
54
55
/**
56
* The margin contains the index of data points (cities) at both ends
57
* <A href = "http://my.oschina.net/arthor" class = "referer" target = "_ blank"> @ author </a> duyf
58
*
59
*/
60
Class Edge {
61
62
Private int I;
63
Private int j;
64
Private double w;
65
66
Edge (int I, int j, double w ){
67
This. I = I;
68
This. j = j;
69
This. w = w;
70
}
71
72
Public int getI (){
73
Return I;
74
}
75
76
Public int getJ (){
77
Return j;
78
}
79
80
Public double getW (){
81
Return w;
82
}
83
84
}
2. the MST core class. The Edge Class represents two points and distance of an Edge,
The process of finding the shortest edge is: Constantly incorporating the shortest edge, and then find the shortest Edge Based on the two ends of the known shortest edge (md)
01
Public class MST {
02
03
Private List <City> data;
04
05
Private double [] [] ds;
06
07
Public MST (List <City> data ){
08
This. data = data;
09
}
10
11
Public List <Edge> compute (){
12
// Distance Matrix
13
Ds = new double [data. size ()] [data. size ()];
14
15
For (int I = 0; I <data. size (); I ++ ){
16
City city1 = data. get (I );
17
For (int j = I + 1; j <data. size (); j ++ ){
18
City city2 = data. get (j );
19
Ds [I] [j] = getDistance (city1, city2 );
20
// Matrix Symmetry
21
Ds [j] [I] = ds [I] [j];
22
}
23
Ds [I] [I] = 0.0;
24
}
25
26
Boolean [] isMst = new boolean [data. size ()];
27
IsMst [0] = true;
28
Edge edge = null;
29
List <Edge> edges = new ArrayList <Edge> ();
30
While (edge = findMinEdge (isMst ))! = Null ){
31
Edges. add (edge );
32
33
// Mark as a known MST data point
34
IsMst [edge. getJ ()] = true;
35
}
36
Return edges;
37
38
}
39
40
// Find the minimum distance from known MST data points
41 www.2cto.com
Private Edge findMinEdge (boolean [] isMst ){
42
// Initialize infinite
43
Double minds = Double. POSITIVE_INFINITY;
44
Int minI =-1;
45
Int minJ =-1;
46
Edge edge = null;
47
For (int I = 0; I <ds. length; I ++ ){
48
If (isMst [I] = true ){
49
For (int j = 0; j <ds. length; j ++ ){
50
If (isMst [j] = false ){
51
If (minds> ds [I] [j]) {
52
Minds = ds [I] [j];
53
MinI = I;
54
MinJ = j;
55
}
56
}
57
}
58
}
59
}
60
If (minI>-1 ){
61
Edge = new Edge (minI, minJ, minds );
62
}
63
Return edge;
64
}
65
66
// Calculate the spatial distance
67
Private double getDistance (City city1, City city2 ){
68
Double distance = Math. pow (city1.getX ()-city2.getX (), 2) + Math. pow (city1.getY ()-city2.getY (), 2 );
69
Return Math. sqrt (distance );
70
71
}
72
73
74
}
The first step is to calculate the near distance matrix.
3. Test it.
01
Public static void main (String [] args ){
02
List <City> citys = new ArrayList <City> ();
03
04
City city0 = new City ();
05
City0.setName ("Beijing ");
06
City0.setX (116.28 );
07
City0.setY (39.54 );
08
Citys. add (city0 );
09
10
City city1 = new City ();
11
City1.setName ("Shanghai ");
12
City1.setX (121.29 );
13
City1.setY (31.14 );
14
Citys. add (city1 );
15
16
City city2 = new City ();
17
City2.setName ("Tian Jin ");
18
City2.setX (117.11 );
19
City2.setY (39.09 );
20
Citys. add (city2 );
21
22
City city3 = new City ();
23
City3.setName ("Chongqing ");
24
City3.setX (106.32 );
25
City3.setY (29.32 );
26
Citys. add (city3 );
27
28
City city4 = new City ();
29
City4.setName ("Harbin ");
30
City4.setX (126.41 );
31
City4.setY (45.45 );
32
Citys. add (city4 );
33
34
City city5 = new City ();
35
City5.setName ("Changchun ");
36
City5.setX (125.19 );
37
City5.setY (43.52 );
38
Citys. add (city5 );
39
40
City city6 = new City ();
41
City6.setName ("Nanjing ");
42
City6.setX (118.50 );
43
City6.setY (32.02 );
44
Citys. add (city6 );
45
46
City city7 = new City ();
47
City7.setName ("Wuhan ");
48
City7.setx( 114.21 );
49
City7.setY (30.37 );
50
Citys. add (city7 );
51
52
City city8 = new City ();
53
City8.setName (" Bei ");
54
City8.setX (121.31 );
55
City8.setY (25.03 );
56
Citys. add (city8 );
57
58
City city9 = new City ();
59
City9.setName ("Hong Kong ");
60
City9.setX (114.10 );
61
City9.setY (22.18 );
62
Citys. add (city9 );
63
64
MST mst = new MST (citys );
65
List <Edge> edges = mst. compute ();
66
67
System. out. println ("------------------ the line best solution is as follows ------------------");
68
For (Edge edge: edges ){
69
City from = citys. get (edge. getI ());
70
City to = citys. get (edge. getJ ());
71
Double length = edge. getW ();
72
System. out. println (edge. getI () + "========>" + edge. getJ ());
73
System. out. println (from. getName () + "to" + to. getName () + ", full length" + length );
74
}
75
76
}
Author: AngelAndAngel