KNN algorithm implementation in C++ with graphics.h. And also learn the concept and working of K nearest neighbor algorithm.
This program is implemented in C++ using Turbo C. Use Graphics.h header file for visual understanding of the algorithm. Graphics.h is the C library which helps to create graphical shapes, like Circle, Rectangles etc.
This knn algorithm program has two classes A and B. Each class has 4 members. This program Get x and y coordinates of the class members in input.
When all members of class A & B are located than enter new member location. Program automatically calculates the distance of new member from the member of both classes.
After calculating and sorting the distances between the members of classes. Put the value of k (which is less than 9 for this program). New member goes into any of these classes with respect to the value of k. For download this knn algorithm program .cpp file Click below.
How this knn algorithm Program component work
You need to have installed Turbo C which have Graphics.h library in working condition. I am sure you clearly understood about the operating system on which it work.
- These for loops take input for class A and Class B members locations. And show circles on that locations.
cout<<"Enter the Class A memebers location"<<endl; for (i=0 ; i>A[0][i]; cout<<"ty-cordinates of member "<<++i<<" ="; i--; cin>>A[1][i]; x1=A[0][i]; y1=A[1][i]; circle(getmaxx()/2+x1, getmaxy()/2-y1, radius); clas[j]='A'; } cout<<"Enter the Class B memebers location"<<endl; setcolor(5); for (h=0, j=4 ; h>B[0][h]; cout<<"ty-cordinates of member "<<++h<<" ="; h--; cin>>B[1][h]; x1=B[0][h]; y1=B[1][h]; clas[j]='B'; circle(getmaxx()/2+x1, getmaxy()/2-y1, radius); }
- Take the location of New member which point in the form of circle. After that for loop calculate the distance of New member from the members of other classes (A & B).
cout<<"Enter x-cordinate of New member = "; cin>>x2; cout<<"Enter y_cordinate of New member = " ; cin>>y2; circle(getmaxx()/2+x2, getmaxy()/2-y2, 5); for(j=0, h=0; j<4;h++, j++) { x1=A[0][h]; y1=A[1][h]; distance[j]=sqrt((pow((x2-x1), 2.0 ) + pow((y2-y1) , 2.0)) ); } for(j=4, h=0; j<8;h++, j++) { x1=B[0][h]; y1=B[1][h]; distance[j]=sqrt((pow((x2-x1), 2.0 ) + pow((y2-y1) , 2.0)) ); }
- Nested for loop for sorting the distances; which calculated between new member and class members.
int m, l,n; float key; for( m=0; m<8; m++) for(n=7 ; n>=m+1 ; n--) if(distance[n] < distance[n-1]) { int t = distance[n]; distance[n]= distance[n-1]; distance[n-1] = t; char temp = clas[n]; clas[n]=clas[n-1]; clas[n-1]=temp; }
- Get the value of K and make decision on the base of its value. Either New member goes into Class A or in Class B. And finally knn algorithm work is done.
cout<< "nEnter value of K = "; cin>>k; int chk ,a=0,b=0; for (chk=0; chk<k; chk++) { cout<<clas[chk]<<" "; if(clas[chk]=='A') { a++; } else if(clas[chk]=='B') { b++; } } cout<<""<<endl; if(a>b) { setcolor(3); outtextxy(getmaxx()/2,getmaxy()/2+50,"New Member lies in Class A"); } else if(b>a) { setcolor(5); outtextxy(getmaxx()/2,getmaxy()/2+50,"New Member lies in Class B"); }
Hope! this program is really useful for you. Feel free to contact us! If you face any problem about this KNN Algorithm program. This is my way of implementation of the algorithm. Below is the place (in Comments) where you can share yours 🙂 .