C++ Program to Find Closest Pair of Points in an Array

This C++ Program Finds the Closest Pair of Points in an Array.

Here is source code of the C++ Program to Find the Closest Pair of Points in an Array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Find Closest Pair of Points in an Array
  3.  */
  4. #include <iostream>
  5. #include <cfloat>
  6. #include <cstdlib>
  7. #include <cmath>
  8. using namespace std;
  9.  
  10. /* 
  11.  * Point Declaration
  12.  */
  13. struct Point
  14. {
  15.     int x, y;
  16. };
  17.  
  18. /* 
  19.  * sort array of points according to X coordinate
  20.  */
  21. int compareX(const void* a, const void* b)
  22. {
  23.     Point *p1 = (Point *)a, *p2 = (Point *)b;
  24.     return (p1->x - p2->x);
  25. }
  26. /* 
  27.  * sort array of points according to Y coordinate
  28.  */
  29. int compareY(const void* a, const void* b)
  30. {
  31.     Point *p1 = (Point *)a, *p2 = (Point *)b;
  32.     return (p1->y - p2->y);
  33. }
  34. /* 
  35.  * find the distance between two points
  36.  */
  37. float dist(Point p1, Point p2)
  38. {
  39.     return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
  40. }
  41. /* 
  42.  * return the smallest distance between two points
  43.  */ 
  44. float small_dist(Point P[], int n)
  45. {
  46.     float min = FLT_MAX;
  47.     for (int i = 0; i < n; ++i)
  48.     {
  49.         for (int j = i + 1; j < n; ++j)
  50.         {
  51.             if (dist(P[i], P[j]) < min)
  52.                 min = dist(P[i], P[j]);
  53.         }
  54.     }
  55.     return min;
  56. }
  57. /* 
  58.  * find the distance beween the closest points of strip of given size
  59.  */
  60. float stripClosest(Point strip[], int size, float d)
  61. {
  62.     float min = d;
  63.     for (int i = 0; i < size; ++i)
  64.     {
  65.         for (int j = i + 1; j < size && (strip[j].y - strip[i].y) < min; ++j)
  66.         {
  67.             if (dist(strip[i],strip[j]) < min)
  68.                 min = dist(strip[i], strip[j]);
  69.         }
  70.     }
  71.     return min;
  72. }
  73. /* 
  74.  * find the smallest distance.
  75.  */
  76. float closestUtil(Point Px[], Point Py[], int n)
  77. {
  78.     if (n <= 3)
  79.         return small_dist(Px, n);
  80.     int mid = n / 2;
  81.     Point midPoint = Px[mid];
  82.     Point Pyl[mid + 1];
  83.     Point Pyr[n - mid - 1];
  84.     int li = 0, ri = 0;
  85.     for (int i = 0; i < n; i++)
  86.     {
  87.         if (Py[i].x <= midPoint.x)
  88.             Pyl[li++] = Py[i];
  89.         else
  90.             Pyr[ri++] = Py[i];
  91.     }
  92.     float dl = closestUtil(Px, Pyl, mid);
  93.     float dr = closestUtil(Px + mid, Pyr, n-mid);
  94.     float d = min(dl, dr);
  95.     Point strip[n];
  96.     int j = 0;
  97.     for (int i = 0; i < n; i++)
  98.     {
  99.         if (abs(Py[i].x - midPoint.x) < d)
  100.             strip[j] = Py[i], j++;
  101.     }
  102.     return min(d, stripClosest(strip, j, d));
  103. }
  104. /* 
  105.  * finds the smallest distance
  106.  */
  107. float closest(Point P[], int n)
  108. {
  109.     Point Px[n];
  110.     Point Py[n];
  111.     for (int i = 0; i < n; i++)
  112.     {
  113.         Px[i] = P[i];
  114.         Py[i] = P[i];
  115.     }
  116.     qsort(Px, n, sizeof(Point), compareX);
  117.     qsort(Py, n, sizeof(Point), compareY);
  118.     return closestUtil(Px, Py, n);
  119. }
  120.  
  121. /*
  122.  * Main
  123.  */
  124. int main()
  125. {
  126.     Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
  127.     int n = sizeof(P) / sizeof(P[0]);
  128.     cout << "The smallest distance is " << closest(P, n);
  129.     return 0;
  130. }

$ g++ closest_pair.cpp
$ a.out
 
The smallest distance is 1.41421
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.