Maximum minimum distance algorithm

Source: Internet
Author: User
Tags pow

Title: Maximum Minimum distance algorithm
Date:2017-12-16 17:36:54
Tags: Clustering algorithm
Categories:algorithms

Course Design

Clustering analysis using maximum minimum distance algorithm

/*clustering analysis using maximum minimum distance method1. Choose a sample as the cluster center Z12. Select the sample with the largest distance from Z1 as the second cluster center Z23. Calculate the distance between the remaining samples and {z1,z2}, di1=| xi-z1|, di2=| Xi-z2|,di=min (Di1,di2)4. If Max (di,{i=1..2.}) >θ|z1-z2|, where the sample is too far from the two centers, take XI in Max (Di) as the new cluster center Z3,If there is no new clustering center, the cluster center process is completed, and the sample is directly divided into the nearest center. 5. Go back to the 3rd step to calculate the minimum distance,*//*Distance measure:European distance Absolute distance he distance from tangent distance*//*intra-class distance*//*two value features and matching measures*//*1. Input scale factor2. Input sample count and sample dimension3. Enter a sample4. Determining the distance measure5. First sample as first cluster center6. Find the sample farthest from the first sample as a second cluster center7. Find samples that are far from both cluster centers as other possible centers8. Divide all samples into the nearest cluster center9. Calculating the distance between classes*/#include "algorithm"#include "Cmath"#include "CString"#include "iostream"#include "vector"using namespaceStdintz[ -];//Cluster CenterintZn//number of cluster centersDoublea[ -][6];//sample, up to 30 samples, up to 6 dimensions per sampleDoubled[ -];//sample distance to Z1Doublemind[ -];distance to the nearest cluster center//SampleintN//number of samplesintD//Sample Dimensions//Initialize classification vectors, up to 30 classesvector<vector<int>> V ( -);intDistancemeasure;//Distance measure//Calculate European distance between two pointsDoubleEuclidean (DoubleX[],DoubleY[]) {intsum =0; for(inti =1; I <= D; i++) sum = sum + (X[i]-y[i]) * (X[i]-y[i]);returnsqrt (sum);}//Absolute distanceDoubleManhattan (DoubleX[],DoubleY[]) {intsum =0; for(inti =1; I <= D; i++) sum + = ABS (X[i]-y[i]);returnsum;}//Chesi distanceDoubleChebyahev (DoubleX[],DoubleY[]) {DoubleAns =0; for(inti =1; I <= D;    i++) {ans = max (ans, ABS (X[i]-y[i)); }returnAns;}//he distanceDoubleMinkowski (DoubleX[],DoubleY[],intm) {intsum =0; for(inti =1; I <= D;    i++) {sum + = POW (ABS (X[i]-y[i]), m); }returnPow (SUM,1.0/m);}//using Distancemeasure to select different distance measuresDoubleDistan (DoubleX[],DoubleY[]) {if(Distancemeasure = =1)//European distance        returnEuclidean (x, y);Else if(Distancemeasure = =2)//Absolute distance        returnManhattan (x, y);Else if(Distancemeasure = =3)//Chesi distance        returnChebyahev (x, y);Else if(Distancemeasure = =4) {//he distance        Static intMif(!m) {cout <<"Please enter the He distance parameter m:";        Cin >> m; }returnMinkowski (x, Y, M); }Else{cout <<"parameter error !!"\ n";return -1; }}/*distance between classes:Method of distance method of center distance method and distance approach for the farthest distance method*///Nearest distance//calculates the minimum distance between the elements of the X and Y categoriesDoubleClustermin (intXintY) {DoubleMindistan =1<< -; for(Autoi = V[x].begin (); I! = V[x].end (); i++) { for(Autoj = V[y].begin (); J! = V[y].end (); J + +) {DoubleTEMPD = Distan (A[*i], a[*j]);        Mindistan = min (Mindistan, TEMPD); }    }returnMindistan;}//Farthest distanceDoubleClustermax (intXintY) {DoubleMaxdistab =0; for(Autoi = V[x].begin (); I! = V[x].end (); i++) for(Autoj = V[y].begin (); J! = V[y].end (); J + +) {DoubleTEMPD = Distan (A[*i], a[*j]);        Maxdistab = Max (Maxdistab, TEMPD); }returnMaxdistab;}//center of gravity distance//With sample mean as the center of gravity of the classDoubleClustercentroid (intXintY) {Doublecenterx[ -], centery[ -]; memset (CenterX,0,sizeof(CenterX)); memset (CenterY,0,sizeof(CenterY)); for(Autoi = V[x].begin (); I! = V[x].end (); i++) { for(intj =1; J <= D;        J + +) {Centerx[j] + = a[*i][j]; }    } for(Autoi = V[y].begin (); I! = V[y].end (); i++) { for(intj =1; J <= D;        J + +) {Centery[j] + = a[*i][j]; }    }//Get center position     for(inti =1; I <= D;        i++) {Centerx[i]/= v[x].size ();    Centery[i]/= v[y].size (); }//Calculate intermediate distance    DoubleAns = Distan (CenterX, centery);returnAns;}//Center distance//double Clustermedian (int x, int y) {return 0;}//Average distanceDoubleClusteraverage (intXintY) {Doublesum =0, TEMPD; for(Autoi = V[x].begin (); I! = V[x].end (); i++) { for(Autoj = V[y].begin (); J! = V[y].end ();            J + +) {tempd = Distan (A[*i], a[*j]);        sum + = TEMPD; }} sum/= (V[x].size () * v[y].size ());returnsqrt (sum);}//class within distance//input x is category, Z[x] is X's class heart, V[x] is an in-class sampleDoubleClusterindistance (intx) {//defined as the distance from the sample in the class to the center of the class and    Doublesum =0;intt = z[x];DoubleD for(Autoi = V[x].begin (); I! = V[x].end ();        i++) {d = Distan (A[t], a[*i]);    sum + = D; }returnsum;}intMain () {Freopen ("In.txt","R", stdin);DoubleK//Threshold valuecout <<"Please enter a threshold value:\ n";    Cin >> K; cout <<"Please enter the number of samples and the sample dimensions:\ n";    CIN >> N >> D; cout <<"Input Sample:\ n";intI, J;//Input Sample     for(i =1; I <= N; i++) for(j =1; J <= D; J + +) cin >> A[i][j];//Select distance measurecout <<"Select distance measure:\ n"; cout <<"1. Euclidean distance\ n"; cout <<"2. Absolute distance\ n"; cout <<"3. Chesi Distance\ n"; cout <<"4. He distance\ n"; Cin >> Distancemeasure;//Select the first sample point as the initial cluster centerz[1] =1;//Select the sample farthest from Z1 as a 2nd cluster center    Doublet =0;//MAX distance     for(i =2; I <= N; i++) {D[i] = Distan (a[1], a[i]);//Sample I distance from Z1        //cout << i << "to X1 Distance:" << d[i] << Endl;        if(D[i] > t)            {t = d[i]; z[2] = i; }} zn =2;//Find a cluster center     while(1) {//Calculate the distance from the remaining samples to the nearest cluster center        //Cluster Center number >=2         for(i =1; I <= N; i++) {Doublet =1<< -;//temporary as minimum distance             for(j =1; J <= Zn;            J + +) {t = min (t, Distan (A[z[j]], a[i]));        } Mind[i] = t; }//Select the maximum distance in the minimum distance        intTempz;//Possible new clustering centers        DoubleTEMPD =0;maximum distance in//min. distance         for(inti =1; I <= N; i++) {if(Mind[i] > TEMPD)                {tempd = Mind[i];            Tempz = i; }        }if(Tempd > K * Distan (a[1], a[z[2]])) {//Add new cluster CenterZ[++ZN] = Tempz; }Else{//Find cluster center end             Break; }    }//Output Cluster Center    /* cout << "Cluster Center: \ n";For (i = 1; I <= Zn; i++)cout << z[i] << ";cout << endl;*/    //Assign samples to the nearest cluster center     for(i =1; I <= N; i++) {//Traversal of all samplesvector<int> v1;DoubleD//Distancet =1<< -;//min distance        intTempno;//Temporary grouping         for(j =1; J <= Zn; J + +) {//Traverse All CentersD = Distan (A[z[j]], a[i]);if(D < T) {//Distance closer, update groupt = D;            Tempno = j; }        }//cout << "Push" << tempno << Endl;V[tempno].push_back (i);//Add sample I to section Tempno group}//for (Auto x = V[3].begin (); X! = V[3].end ();        //cout << *x;    //cout << Endl;    //Output grouping information     for(i =1; I <= Zn; i++) {cout <<"group Center:"<< Z[i] << Endl; cout <<"group members:"; for(Autox = V[i].begin (); X! = V[i].end (); x + +) cout << *x <<' ';    cout << Endl; }//Calculate distance between classescout <<"Distance between classes:\ n"; for(i =1; I <= Zn; i++) { for(j = i +1; J <= Zn; J + +) {cout <<"   "<< I <<" --- "<< J << Endl; cout <<"Nearest Distance:"<< Clustermin (i, J) << Endl; cout <<"Farthest Distance:"<< Clustermax (i, J) << Endl;//cout << "middle distance:" << Clustermedian (i, J) << Endl;cout <<"Center of gravity Distance:"<< Clustercentroid (i, J) << Endl; cout <<"Average distance:"<< Clusteraverage (i, J) << Endl; }    }//class within distance    //defined as the distance from the sample within the class to the center of the class andcout <<"\ nintra-class distance:\ n"; for(i =1; I <= Zn; i++) {cout <<"section"<< I <<"class:";    cout << clusterindistance (i) << Endl; }return 0;}

Test input File
In.txt

0.510 20 03 82 21 15 34 86 35 46 47 51

Maximum minimum distance algorithm

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.