利用C语言简单的实现DBSCAN算法

一、要求

有如下二维数据集,取ε =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;
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DBSCAN算法是一种基于密度的聚类算法,可以用于发现任意形状的簇。以下是用C语言实现DBSCAN算法的代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX_POINTS 1000 typedef struct { double x, y; } Point; typedef struct { int id; int is_core; int cluster_id; } PointInfo; int num_points; Point points[MAX_POINTS]; PointInfo point_info[MAX_POINTS]; double eps; int min_pts; double distance(Point p1, Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx * dx + dy * dy); } int region_query(int p) { int count = 0; for (int i = 0; i < num_points; i++) { if (i == p) continue; if (distance(points[p], points[i]) <= eps) { count++; } } return count; } void expand_cluster(int p, int cluster_id) { point_info[p].cluster_id = cluster_id; int neighbor_pts[MAX_POINTS]; int num_neighbors = 0; for (int i = 0; i < num_points; i++) { if (i == p) continue; if (distance(points[p], points[i]) <= eps) { neighbor_pts[num_neighbors++] = i; } } if (num_neighbors >= min_pts) { point_info[p].is_core = 1; for (int i = 0; i < num_neighbors; i++) { int q = neighbor_pts[i]; if (point_info[q].cluster_id == -1) { expand_cluster(q, cluster_id); } } } } void dbscan() { int cluster_id = 0; for (int i = 0; i < num_points; i++) { if (point_info[i].cluster_id != -1) continue; if (region_query(i) >= min_pts) { point_info[i].is_core = 1; expand_cluster(i, cluster_id); cluster_id++; } else { point_info[i].is_core = 0; } } } int main() { // Read input data scanf("%d %lf %d", &num_points, &eps, &min_pts); for (int i = 0; i < num_points; i++) { scanf("%lf %lf", &points[i].x, &points[i].y); point_info[i].id = i; point_info[i].is_core = -1; point_info[i].cluster_id = -1; } // Run DBSCAN algorithm dbscan(); // Print results for (int i = 0; i < num_points; i++) { printf("%d %d\n", point_info[i].id, point_info[i].cluster_id); } return 0; } ``` 该代码实现了一个简单DBSCAN算法,可以读入点集数据并输出每个点所属的簇的编号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值