The charm of numbers: Finding the nearest point pair on a two-dimensional plane

Source: Internet
Author: User

In the n points on the two-dimensional plane, how to quickly find the nearest pair of points is the nearest point to the problem.

At first glance, it may feel a bit complicated.
Scheme One: Brute force method. The array contains a total of n numbers, so we can sort all the points in the plane by the x-axis, then calculate the distance from the last coordinate to the left of the previous one, and then use Min and position to record the nearest distance and two coordinates. The scheme is somewhat similar to the distance of two nearest points in one-dimensional space,The time complexity is: O (n*n).

Scenario Two: in a one-dimensional space, we know that if the array is ordered, we can quickly find the two closest points. We can use the time complexity of O (N*logn) to sort the data [fast, heap, and return]. The minimum difference can be obtained by the time complexity of O (N) when the order is completed. However, this method cannot be used in two dimensions, because in two-dimensional space, the distance between the x-axis and the smallest distance between the two points is not necessarily the shortest, as shown in.

Is there any way to do that? Here we have adopted the method of division and treatment. We use the median of the array [the solution details of the median to refer to here], dividing the array into left and right two parts, either from the left or from the left, or from the left and the left.Here we can reduce the complexity of time to O (N*logn).
Here the main complex is in the merging place, and its thought is as follows:
The first step: first find the point set data in the median median, by the x-coordinate to divide, with median to the point set data, the left is data1, the right is data2;
The second step: to find the two parts divided into the data1 and data2 the nearest point pair, recorded as: MinDis1 and MinDis2;
The third step: Find the Data1 and data2 the nearest point to the distance of the smaller value: Mindis = Min{mindis1,mindis2};
Fourth step: Find out the top 6 points of the Y value in data2 [using the pigeon nest principle, because its distance is not likely to be less than min, and we only consider the data within the Min*2*min box],
For points in data1, calculate the distance from each point in the Data2 Mindiso, if Mindiso < Mindis, change the Mindis value, MINDIS=MINDIS0; description at the minimum distance one from the left one comes from the right, Produced at the time of the merger.
The reference code is as follows:
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace St  D  Vertex information struct Point {double m_x, m_y; Point (): m_x (0.0), m_y (0.0) {} point (double x, double y): m_x (x), m_y (y) {} bool operator== (const point& p) Const {Retu  RN m_x==p.m_x && m_y==p.m_y;}  }; ostream& operator<< (ostream& os, const point& p) {return OS << ("<< p.m_x <<", "  << p.m_y << ")"; }//Insert sort Template<class T, class pr> void Insert_sort (vector<t> &vec, int l, int r, Pr pred) {int I  , J;  for (i=l+1; i<=r; i++) {T tmp = vec[i];  for (j=i-1; j>=l && pred (Tmp,vec[j]); j--) vec[j+1]=vec[j];  VEC[J+1] = tmp;  }}//Find where key is located template<class t> int get_position (vector<t> &vec, int l, int r, T key) {for (int I=l; i<=r;  i++) if (key = = Vec[i]) return i;  return-1; }//The VEC is divided by the first element Template<class T, class pr> intPartition (vector<t> &vec, int l, int r, Pr pred) {int I, J;  for (i=l+1,j=l; i<=r; i++) {if (pred (Vec[i],vec[l])) {++j;  Swap (vec[i],vec[j]);  }} swap (Vec[j],vec[l]);  Return J;  }//Order statistics get the value of K element Template<class T, class pr> T Select (vector<t> &vec, int l, int r, int k, Pr pred)  {int n = r-l+1;  if (n==1) {if (k!=0) printf ("Out of boundary!\n");  return vec[l];  }//Find the median median as the split-point int cnt = N/5;  int tcnt = (n+4)/5;  int rem = n%5;  Vector<t> Group (TCNT);  int I, J;  for (i=0,j=l; i<cnt; i++,j+=5) {Insert_sort (VEC, J, J+4, pred);  Group[i] = vec[j+2];  } if (REM) {Insert_sort (VEC, J, J+rem-1, pred);  Group[i] = vec[j+ (rem-1)/2];  } T key = Select (Group, 0, Tcnt-1, (tcnt-1)/2, pred);  Find the location of the split point int key_pos = Get_position (VEC, L, R, key);  Swap (Vec[key_pos], vec[l]);  Divide the array with the split points, small on the left, large on the right int pos = partition (VEC, L, R, Pred);  int x = pos-l;  if (x = = k) return key; else if (x < K) return Select (VEC, Pos+1, R, K-x-1, pred);  else return Select (VEC, L, Pos-1, K, pred);  }//Calculate the distance between points A and B double dist (const point& A, const point& b) {double x = a.m_x-b.m_x;  Double y = a.m_y-b.m_y;  return sqrt (x*x+y*y);  } bool Cmpx (const point& A, const point& b) {return a.m_x < b.m_x;  } bool Cmpy (const point& A, const point& b) {return a.m_y < b.m_y; The number of elements of a double mindifferent (vector<point> p, int l, int r, vector<point> &result) {//Sub-region divided by median is reduced  to 2 or 3, no more to 1 if ((r-l+1) ==2) {result[0] = p[l];  RESULT[1] = P[r];  if (CMPX (p[r],p[l)) swap (P[l], p[r]);  Return Dist (P[l], p[r]);  } if ((r-l+1) ==3) {Insert_sort (P, L, R, CMPX);  Double TMP1 = dist (p[l], p[l+1]);  Double TMP2 = dist (p[l+1], p[l+2]);  Double ret = min (tmp1, TMP2);  if (TMP1 = = ret) {result[0] = p[l];  RESULT[1] = p[l+1];  } else {result[0] = p[l+1];  RESULT[1] = p[l+2];  } return ret;  }//Case greater than 3 dots int mid = (r+l) >>1; Point median = select (P, L, R, Mid-l, CMPX);  Vector<point> res1 (2), Res2 (2);  Double min_l = Mindifferent (P, L, Mid, res1);  Double min_r = Mindifferent (p, mid+1, R, Res2);  Double minum = min (min_l, min_r);  if (Minum = = min_l) {result[0] = res1[0];  RESULT[1] = res1[1];  } else {result[0] = res2[0];  RESULT[1] = res2[1];  }//For [P[mid+1]-minum, P[mid]+minum] The Ribbon area is sorted by y vector<point> Yvec;  int I, J;  For (i=mid+1, i<=r; i++) if (p[i].m_x-p[mid].m_x < Minum) Yvec.push_back (Point (P[i]));  For (I=mid, i>=l; i--) if (p[mid+1].m_x-p[i].m_x < Minum) Yvec.push_back (Point (P[i]));  Sort (Yvec.begin (), Yvec.end (), cmpy); for (i=0; I<yvec.size (); i++) {//At most up to 7 points thereafter will be less than minum for (j=i+1; J<yvec.size () && yvec[j].m_y-yvec[ I].m_y<minum && j<=i+7;  J + +) {Double delta = dist (yvec[i],yvec[j]);  if (Delta < minum) {minum = Delta;  Result[0] = Yvec[i];  RESULT[1] = Yvec[j];  }}} return minum;  } int main () {int n, I, J, X, y; Vector<pOint> result (2);  vector<point> input;  cout<< "Please input the number of your data:" <<endl;cin >> N; cout<< "Please input your data:" <<endl;for (i=0; i<n;  i++) {cin >> x;  Cin >> y;  Input.push_back (Point (x, y));  } Double minum = mindifferent (input, 0, input.size ()-1, result);  cout << Nearest point: << Result[0] << "and" << result[1] << Endl;  cout << "Distance:" << minum << Endl;  System ("pause"); return 0;   }

Operation Result:


The charm of numbers: Finding the nearest point pair on a two-dimensional plane

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.