Dbscan algorithm C + + implementation

Source: Internet
Author: User

Tag:dbscan    Data mining     density     based;

#include <bits/stdc++.h> #define Dimense 10//10 Dimensional data # define N 5005#define MAX 0xffffff#define CLR (a) memset (a,0, sizeof (a)) using namespace std;double radius=60;int min_num=400;int num=5000;//Data volume int K;int now=0;//is currently the first cluster int ans=0;//    Number of clusters that are not outliers, struct point{int id;    Double Dir[dimense];    int belong;    BOOL IsKey;    BOOL Vis; int rec;//Statistics How many points are in their region vector<int>area;};    Point center[15];vector<point>data;vector<int>save[15];d ouble dis (point a,point b) {double ret=0;    for (int ii=0;ii<dimense;ii++) {ret+= (A.dir[ii]-b.dir[ii]) * (A.dir[ii]-b.dir[ii]); } return sqrt (ret);}    int cnt=0;//The total number of visited void DBscan (int cur) {int i,j;    if (Data[cur].vis) return;    Data[cur].vis=true;    Data[cur].belong=now;    cnt++;        for (i=0;i<num;i++) {if (Data[i].vis) continue;            if (DIS (data[i],data[cur]) <radius) {data[cur].area.push_back (i);            Data[i].belong=now;        data[cur].rec++; }} if (Data[cur].rec>min_num) {data[cur].iskey=true;        for (i=0;i<data[cur].rec;i++) {DBscan (data[cur].area[i]); }}else Data[cur].iskey=false;}    Set<int>tot;int vis2[n];void dfs (int a) {if (Vis2[a]) return;    Vis2[a]=1;    Save[now].push_back (a); for (int i=0;i<data[a].rec;i++) DFS (Data[a].area[i]);}    int main () {CLR (VIS2);    int i,j;    Freopen ("In.txt", "R", stdin);    Freopen ("Out2.txt", "w", stdout);        for (i=0;i<num;i++) {point T;        T.vis=false;        T.id=i;        t.rec=0;        for (j=0;j<dimense;j++) scanf ("%lf", &t.dir[j]);    Data.push_back (t);            } for (i=0;i<num;i++) if (!data[i].vis) {DBscan (i);        now++;    } now=0;            for (i=0;i<num;i++) {if (!vis2[i]) {DFS (i);        now++;    }} ans=0;    int tmp_rec[n];    for (i=0;i<now;i++) {if (Save[i].size () >min_num) tmp_rec[ans++]=i;    } printf ("Altogether%d clusters \ n Total:%d points \ n", ans,cnt); for (i=0;i<ans;i++) {int len=save[tmp_rec[i]].size ();        printf ("%d points in%d clusters", i+1,len); for (j=0;j<len;j++) {printf ("%d%c", save[tmp_rec[i]][j],j==len-1? ')        \ n ': '); }} return 0;}

Copy to Google TranslateTranslation Results

Dbscan algorithm C + + implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.