用c++写一个DBSCAN聚类算法
时间: 2023-07-01 09:22:45 浏览: 186
好的,以下是一个简单的C++实现DBSCAN聚类算法的示例代码:
```c++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct Point {
double x, y;
bool visited;
int cluster;
};
double distance(Point a, Point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
vector<Point> regionQuery(vector<Point>& points, int index, double eps) {
vector<Point> neighbors;
for (int i = 0; i < points.size(); i++) {
if (distance(points[i], points[index]) <= eps) {
neighbors.push_back(points[i]);
}
}
return neighbors;
}
void expandCluster(vector<Point>& points, int index, vector<Point>& neighbors, int cluster, double eps, int minPts) {
points[index].cluster = cluster;
for (int i = 0; i < neighbors.size(); i++) {
int neighborIndex = &neighbors[i] - &points[0];
if (!neighbors[i].visited) {
neighbors[i].visited = true;
vector<Point> newNeighbors = regionQuery(points, neighborIndex, eps);
if (newNeighbors.size() >= minPts) {
neighbors.insert(neighbors.end(), newNeighbors.begin(), newNeighbors.end());
}
}
if (points[neighborIndex].cluster == -1) {
points[neighborIndex].cluster = cluster;
}
}
}
void DBSCAN(vector<Point>& points, double eps, int minPts) {
int cluster = 0;
for (int i = 0; i < points.size(); i++) {
if (!points[i].visited) {
points[i].visited = true;
vector<Point> neighbors = regionQuery(points, i, eps);
if (neighbors.size() < minPts) {
points[i].cluster = -1;
} else {
expandCluster(points, i, neighbors, cluster, eps, minPts);
cluster++;
}
}
}
}
int main() {
vector<Point> points = {{1, 1}, {1, 2}, {2, 1}, {10, 10}, {10, 12}, {12, 10}, {50, 50}, {50, 52}, {52, 50}};
double eps = 3;
int minPts = 3;
DBSCAN(points, eps, minPts);
for (int i = 0; i < points.size(); i++) {
cout << "(" << points[i].x << ", " << points[i].y << ") - Cluster " << points[i].cluster << endl;
}
return 0;
}
```
这个示例代码使用了一个Point结构体来存储点的坐标、是否被访问过、聚类编号等信息。`distance()`函数用来计算两个点之间的距离,`regionQuery()`函数用来查询某个点的邻居点,`expandCluster()`函数用来将某个点及其邻居点分配到同一个聚类中。最后的`DBSCAN()`函数是整个算法的主体部分,通过遍历所有点来实现聚类。在这个示例代码中,我们使用了一个简单的点集来测试算法的正确性,并输出了每个点所属的聚类编号。
阅读全文