Realization of Clustering Algorithm-dbscan-c++

Source: Internet
Author: User

Program Flowchart:


Dbscan core function function, calculates the number of point in the EPs range for each point pts;

For all PTS >minpts Point, note as core point;

For all Corepoint, the core point subscript within its EPS range is added to the vector<int>::corepts;

For all corepoint, all cluster of the core point are traversed in a depth-first way so that the connected core point has the same cluster number;

Calculates all pts < minpts in the core point range and is recorded as Borderpoint;

Add all borderpoint to any of the associated core point;

The remaining point is noise point, which is ignored when writing to the file;

Writes the point information to the clustering file and ends the program.

/*dbscan algorithm15s103182ethan*/#include <iostream> #include <sstream> #include <fstream># Include <vector> #include <ctime> #include <cstdlib> #include <limits> #include <cmath># Include <stack>using namespace Std;class point{public:float x;float y;int cluster=0;int pointtype=1;//1 Noise 2 bor  Der 3 Coreint pts=0;//points in minpts vector<int> corepts;int visited = 0;point () {}point (float a,float b,int c) {x = A;y = B;cluster = c;}}; Float Stringtofloat (String i) {StringStream sf;float Score=0;sf<<i;sf>>score;return score;} Vector<point> openFile (const char* DataSet) {FStream file;file.open (dataset,ios::in); if (!file) {cout <        < "Open File failed!" <<endl;        Vector<point> A;    return A; } vector<point> data;int I=1;while (!file.eof ()) {string Temp;file>>temp;int split = Temp.find (', ', 0);p oint P (Stringtofloat (TEMP.SUBSTR (0,split)), Stringtofloat (Temp.substr (split+1,temp.leNgth ()-1)), i++);d ata.push_back (p);} File.close ();cout<< "successful!" <<endl;return data;} Float Squaredistance (Point A,point b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} void DBSCAN (vector<point> dataset,float eps,int minpts) {int len = dataset.size ();//calculate ptscout<< " Calculate pts "<<endl;for (int i=0;i<len;i++) {for (int j=i+1;j<len;j++) {if (Squaredistance (dataset[i), DATASET[J] <eps) dataset[i].pts++;d ataset[j].pts++;}} Core Point cout<< ' core point ' <<endl;vector<point> corepoint;for (int i=0;i<len;i++) {if ( dataset[i].pts>=minpts) {Dataset[i].pointtype = 3;corepoint.push_back (Dataset[i]);}} cout<< "Joint core point" <<endl;//joint core pointfor (int i=0;i<corepoint.size (); i++) {for (int j=i+1;j <corepoint.size (); j + +) {if (Squaredistance (Corepoint[i],corepoint[j]) <eps) {Corepoint[i].corepts.push_back ( j); Corepoint[j].corepts.push_back (i);}}} for (int i=0;i<corepoint.size (); i++) {stack<point*> Ps;if (COrepoint[i].visited = = 1) continue;ps.push (&corepoint[i]);p oint *v;while (!ps.empty ()) {v = ps.top (); v->visited = 1;ps.pop (); for (int j=0;j<v->corepts.size (); j + +) {if (corepoint[v->corepts[j]].visited==1) continue; Corepoint[v->corepts[j]].cluster = corepoint[i].cluster;corepoint[v->corepts[j]].visited = 1;ps.push (& COREPOINT[V-&GT;COREPTS[J]]);}} cout<< "Border Point,joint border Point-to-core point" <<endl;//border point,joint border point to core pointfor (int i=0;i<len;i++) {if (dataset[i].pointtype==3) continue;for (int j=0;j<corepoint.size (); j + +) {if (Squaredistance (dataset[i), COREPOINT[J]) <eps) {Dataset[i].pointtype = 2;dataset[i].cluster = Corepoint[j].cluster;break;}}} cout<< "Output" <<endl;//outputfstream clustering;clustering.open ("Clustering.txt", ios::out); for (int i =0;i<len;i++) {if (Dataset[i].pointtype = = 2) clustering<<dataset[i].x<< "," <<dataset[i].y< < "," <<dataset[i].cluster<< "\ n";} for (int i=0;i<corepoint.size (); i++) {clustering<<corepoint[i].x<< "," <<corePoint[i].y<< "," << corepoint[i].cluster<< "\ n";} Clustering.close ();} int main (int argc, char** argv) {vector<point> DataSet = OpenFile ("Dataset3.txt");D bscan (dataset,1.5,2); return 0;}
Data file Format: (x, Y)


Run result format: (x,y,cluster)


Graphical presentation:

Special shapes:


Bridge:


Change density:


Summarize:

The Dbscan algorithm works well with data of different shapes and sizes, and noise-resistant data. But for the density of change, the Dbscan algorithm has limitations. When implementing a core point connection, it was a little cumbersome to unify the cluster number of the connected core, and later by adding an array to each core point to record the subscript information for the connected core point, The method of depth-first traversal not only improves the calculation speed, but also guarantees the accuracy. For the results of experimental data, it is difficult to see the effect of clustering by using WPF (C #) technique to deal with the data points, to give different colors to different cluster numbered points, and to display them graphically.



Realization of Clustering Algorithm-dbscan-c++

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.