Skip to content

Commit 1e9d488

Browse files
author
linyiqun
committed
基于连通图的分裂聚类算法
基于连通图的分裂聚类算法
1 parent 5afe53a commit 1e9d488

File tree

5 files changed

+490
-0
lines changed

5 files changed

+490
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package DataMining_CABDDCC;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.text.MessageFormat;
8+
import java.util.ArrayList;
9+
10+
/**
11+
* 基于连通图的分裂聚类算法
12+
*
13+
* @author lyq
14+
*
15+
*/
16+
public class CABDDCCTool {
17+
// 测试数据点数据
18+
private String filePath;
19+
// 连通图距离阈值l
20+
private int length;
21+
// 原始坐标点
22+
public static ArrayList<Point> totalPoints;
23+
// 聚类结果坐标点集合
24+
private ArrayList<ArrayList<Point>> resultClusters;
25+
// 连通图
26+
private Graph graph;
27+
28+
public CABDDCCTool(String filePath, int length) {
29+
this.filePath = filePath;
30+
this.length = length;
31+
32+
readDataFile();
33+
}
34+
35+
/**
36+
* 从文件中读取数据
37+
*/
38+
public void readDataFile() {
39+
File file = new File(filePath);
40+
ArrayList<String[]> dataArray = new ArrayList<String[]>();
41+
42+
try {
43+
BufferedReader in = new BufferedReader(new FileReader(file));
44+
String str;
45+
String[] tempArray;
46+
while ((str = in.readLine()) != null) {
47+
tempArray = str.split(" ");
48+
dataArray.add(tempArray);
49+
}
50+
in.close();
51+
} catch (IOException e) {
52+
e.getStackTrace();
53+
}
54+
55+
Point p;
56+
totalPoints = new ArrayList<>();
57+
for (String[] array : dataArray) {
58+
p = new Point(array[0], array[1], array[2]);
59+
totalPoints.add(p);
60+
}
61+
62+
// 用边和点构造图
63+
graph = new Graph(null, totalPoints);
64+
}
65+
66+
/**
67+
* 分裂连通图得到聚类
68+
*/
69+
public void splitCluster() {
70+
// 获取形成连通子图
71+
ArrayList<Graph> subGraphs;
72+
ArrayList<ArrayList<Point>> pointList;
73+
resultClusters = new ArrayList<>();
74+
75+
subGraphs = graph.splitGraphByLength(length);
76+
77+
for (Graph g : subGraphs) {
78+
// 获取每个连通子图分裂后的聚类结果
79+
pointList = g.getClusterByDivding();
80+
resultClusters.addAll(pointList);
81+
}
82+
83+
printResultCluster();
84+
}
85+
86+
/**
87+
* 输出结果聚簇
88+
*/
89+
private void printResultCluster() {
90+
int i = 1;
91+
for (ArrayList<Point> cluster : resultClusters) {
92+
System.out.print("聚簇" + i + ":");
93+
for (Point p : cluster){
94+
System.out.print(MessageFormat.format("({0}, {1}) ", p.x, p.y));
95+
}
96+
System.out.println();
97+
i++;
98+
}
99+
100+
}
101+
102+
}

Others/DataMining_CABDDCC/Client.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package DataMining_CABDDCC;
2+
3+
/**
4+
* 基于连通图的分裂聚类算法
5+
* @author lyq
6+
*
7+
*/
8+
public class Client {
9+
public static void main(String[] agrs){
10+
String filePath = "C:\\Users\\lyq\\Desktop\\icon\\graphData.txt";
11+
//连通距离阈值
12+
int length = 3;
13+
14+
CABDDCCTool tool = new CABDDCCTool(filePath, length);
15+
tool.splitCluster();
16+
}
17+
}

0 commit comments

Comments
 (0)