在此问题中,在2D平面上给出了一组n点。在这个问题中,我们必须找到距离最小的一对点。
为了解决这个问题,我们必须将点分为两半,然后以递归的方式计算出两点之间的最小距离。使用到中线的距离,将点分成几条。我们将发现与条形阵列的最小距离。首先创建两个带有数据点的列表,一个列表将保存按x值排序的点,另一个列表将保存按y值排序的数据点。
该算法的时间复杂度将为O(n log n)。
Input: A set of different points are given. (2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4) Output: Find the minimum distance from each pair of given points. Here the minimum distance is: 1.41421 unit
findMinDist(pointsList,n)
输入: 给定点列表和列表中的点数。
输出- 查找两点之间的最小距离。
Begin min := ∞ for all items i in the pointsList, do for j := i+1 to n-1, do if distance between pointList[i] and pointList[j] < min, then min = distance of pointList[i] and pointList[j] done done return min End
stripClose(带,大小,距离)
输入-条带中的不同点,点数,与中线的距离。
输出- 距条中两个点的最近距离。
Begin for all items i in the strip, do for j := i+1 to size-1 and (y difference of ithand jth points) <min, do if distance between strip[i] and strip [j] < min, then min = distance of strip [i] and strip [j] done done return min End
findClosest(xSorted,ySorted,n)
输入-按x值排序的点,按y值排序的点,点数。
输出-从总点中找到最小距离。
Begin if n <= 3, then call findMinDist(xSorted, n) return the result mid := n/2 midpoint := xSorted[mid] define two sub lists of points to separate points along vertical line. the sub lists are, ySortedLeft and ySortedRight leftDist := findClosest(xSorted, ySortedLeft, mid) //find left distance rightDist := findClosest(xSorted, ySortedRight, n - mid) //find right distance dist := minimum of leftDist and rightDist make strip of points j := 0 for i := 0 to n-1, do if difference of ySorted[i].x and midPoint.x<dist, then strip[j] := ySorted[i] j := j+1 done close := stripClose(strip, j, dist) return minimum of close and dist End
#include <iostream> #include<cmath> #include<algorithm> using namespace std; struct point { int x, y; }; intcmpX(point p1, point p2) { //to sort according to x value return (p1.x < p2.x); } intcmpY(point p1, point p2) { //to sort according to y value return (p1.y < p2.y); } float dist(point p1, point p2) { //find distance between p1 and p2 return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } float findMinDist(point pts[], int n) { //find minimum distance between two points in a set float min = 9999; for (int i = 0; i < n; ++i) for (int j = i+1; j < n; ++j) if (dist(pts[i], pts[j]) < min) min = dist(pts[i], pts[j]); return min; } float min(float a, float b) { return (a < b)? a : b; } float stripClose(point strip[], int size, float d) { //find closest distance of two points in a strip float min = d; for (int i = 0; i < size; ++i) for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j) if (dist(strip[i],strip[j]) < min) min = dist(strip[i], strip[j]); return min; } float findClosest(point xSorted[], point ySorted[], int n){ if (n <= 3) return findMinDist(xSorted, n); int mid = n/2; point midPoint = xSorted[mid]; point ySortedLeft[mid+1]; // y sorted points in the left side point ySortedRight[n-mid-1]; // y sorted points in the right side intleftIndex = 0, rightIndex = 0; for (int i = 0; i < n; i++) { //separate y sorted points to left and right if (ySorted[i].x <= midPoint.x) ySortedLeft[leftIndex++] = ySorted[i]; else ySortedRight[rightIndex++] = ySorted[i]; } float leftDist = findClosest(xSorted, ySortedLeft, mid); float rightDist = findClosest(ySorted + mid, ySortedRight, n-mid); float dist = min(leftDist, rightDist); point strip[n]; //hold points closer to the vertical line int j = 0; for (int i = 0; i < n; i++) if (abs(ySorted[i].x - midPoint.x) <dist) { strip[j] = ySorted[i]; j++; } return min(dist, stripClose(strip, j, dist)); //find minimum using dist and closest pair in strip } float closestPair(point pts[], int n) { //find distance of closest pair in a set of points point xSorted[n]; point ySorted[n]; for (int i = 0; i < n; i++) { xSorted[i] = pts[i]; ySorted[i] = pts[i]; } sort(xSorted, xSorted+n, cmpX); sort(ySorted, ySorted+n, cmpY); return findClosest(xSorted, ySorted, n); } int main() { point P[] ={{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}}; int n = 6; cout<< "The minimum distance is " <<closestPair(P, n); }
输出结果
The minimum distance is 1.41421