一、要求
有如下二维数据集,取ε =2,minpts=3,请使用DBSCAN算法对其聚类(使用曼哈顿距离)
序号 | 横坐标 | 纵坐标 | 序号 | 横坐标 | 纵坐标 | 序号 | 横坐标 | 纵坐标 |
1 | 20 | 45 | 22 | 20 | 45 | 43 | 6 | 36 |
2 | 34 | 23 | 23 | 34 | 23 | 44 | 78 | 37 |
3 | 53 | 67 | 24 | 53 | 67 | 45 | 7 | 38 |
4 | 54 | 85 | 25 | 54 | 85 | 46 | 70 | 39 |
5 | 67 | 4 | 26 | 67 | 4 | 47 | 6 | 36 |
6 | 33 | 67 | 27 | 33 | 67 | 48 | 45 | 45 |
7 | 24 | 78 | 28 | 24 | 78 | 49 | 67 | 67 |
8 | 37 | 90 | 29 | 37 | 90 | 50 | 84 | 6 |
9 | 67 | 34 | 30 | 67 | 34 | 51 | 23 | 78 |
10 | 34 | 56 | 31 | 78 | 32 | 52 | 13 | 7 |
11 | 89 | 78 | 32 | 23 | 33 | 53 | 45 | 70 |
12 | 65 | 23 | 33 | 45 | 34 | 54 | 67 | 76 |
13 | 45 | 45 | 34 | 67 | 35 | 55 | 4 | 54 |
14 | 67 | 67 | 35 | 67 | 76 | 56 | 68 | 60 |
15 | 84 | 6 | 36 | 4 | 54 | 57 | 45 | 45 |
16 | 23 | 78 | 37 | 68 | 60 | 58 | 34 | 67 |
17 | 13 | 7 | 38 | 7 | 38 | 59 | 35 | 67 |
18 | 45 | 70 | 39 | 70 | 39 | 60 | 36 | 4 |
19 | 67 | 76 | 40 | 76 | 40 | 61 | 37 | 68 |
20 | 4 | 54 | 41 | 45 | 70 | 62 | 38 | 45 |
21 | 68 | 60 | 42 | 67 | 76 | 63 | 20 | 34 |
二、代码
#include <stdio.h>
#include <math.h>
int r = 2;
int minpts = 3;
typedef struct {
int x;
int y;
}DATA;
DATA data[63] = {
{20,45},
{34,23},
{53,67},
{54,85},
{67,4},
{33,67},
{24,78},
{37,90},
{67,34},
{34,56},
{89,78},
{65,23},
{45,45},
{67,67},
{84,6},
{23,78},
{13,7},
{45,70},
{67,76},
{4,54},
{68,60},
{20,45},
{34,23},
{53,67},
{54,85},
{67,4},
{33,67},
{24,78},
{37,90},
{67,34},
{78,32},
{23,33},
{45,34},
{67,35},
{67,76},
{4,54},
{68,60},
{7,38},
{70,39},
{76,40},
{45,70},
{67,76},
{6,36},
{78,37},
{7,38},
{70,39},
{6,36},
{45,45},
{67,67},
{84,6},
{23,78},
{13,7},
{45,70},
{67,76},
{4,54},
{68,60},
{45,45},
{34,67},
{35,67},
{36,4},
{37,68},
{38,45},
{20,34}
};
typedef struct {
int cluster[63];
}DB;
DB db[63];
typedef struct {
int x;
int y;
int flag;
}SC;
SC sc[64];
void init(){
for (int i = 1; i <= 63; ++i) {
sc[i].x = data[i-1].x;
sc[i].y = data[i-1].y;
sc[i].flag = 0;
db[i-1].cluster[0] = -1;
}
}
int pot(int i){
int all;
int count = 0;
for (int j = 1; j <= 63; ++j) {
all = abs(sc[i].x-sc[j].x) + abs(sc[i].y-sc[j].y);
if(all <= r){
count++;
}
}
return count;
}
void reach() {
int cc = 0;
int df[100];
for (int i = 0; i < 100; ++i) {
df[i] = -1;
}
for (int i = 1; i <= 63; ++i) {
if (pot(i) >= minpts){
df[cc] = i;
cc++;
}
}
int yy = 0;
for (int i = 0; i<cc; ++i) {
if(sc[df[i]].flag == 0){
db[yy].cluster[0] = df[i];
sc[df[i]].flag = 1;
int jj = 1;
while(1){
int all;
int fl = 0;
for (int j = 0; j < jj; ++j) {
for (int k = 1; k <= 63; ++k) {
all = abs(sc[db[yy].cluster[j]].x-sc[k].x) + abs(sc[db[yy].cluster[j]].y-sc[k].y);
if((sc[k].flag == 0) && (all <= r)){
db[yy].cluster[jj] = k;
jj++;
fl++;
sc[k].flag = 1;
}
}
}
if(fl == 0){
db[yy].cluster[jj] = -1;
break;
}
}
printf("簇%d:",yy+1);
for (int j = 0; db[yy].cluster[j] != -1; ++j) {
printf("%d ",db[yy].cluster[j]);
}
printf("\n");
yy++;
}
}
}
int main() {
init();
reach();
int x = 0;
printf("噪声点: ");
for (int i = 0; i < 63; ++i) {
if(sc[i].flag == 0){
x++;
printf("%d ",i);
}
}
//printf("\n总共%d个噪声点",x);
return 0;
}