我对分而治之算法来寻找最近点的问题。我查了一下这个页面https://www.geeksforgeeks.org/closest-pair-of-points-onlogn-implementation/ C ++实现,但没有与此代码的问题。它的工作原理在大多数情况下罚款,但对于一些数据,实现返回等结果,比穷举法。例如,让我们取十个点(X,Y):
(795 981)
(1905 4574)
(8891 665)
(6370 1396)
(93 8603)
(302 7099)
(326 5318)
(4493 3977)
(429 8687)
(9198 1558)
对于这个数据,O(n log n)
算法返回944.298,而不是346.341蛮力给出。这究竟是为什么?
这正是geeksforgeeks实现我的示例数据:
#include <iostream>
#include <float.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
struct Point
{
int x, y;
};
int compareX(const void* a, const void* b)
{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->x - p2->x);
}
int compareY(const void* a, const void* b)
{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->y - p2->y);
}
float dist(Point p1, Point p2)
{
return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y)
);
}
float bruteForce(Point P[], int n)
{
float min = FLT_MAX;
for (int i = 0; i < n; ++i)
for (int j = i+1; j < n; ++j)
if (dist(P[i], P[j]) < min)
min = dist(P[i], P[j]);
return min;
}
float min(float x, float y)
{
return (x < y)? x : y;
}
float stripClosest(Point strip[], int size, float d)
{
float min = d; // Initialize the minimum distance as 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 closestUtil(Point Px[], Point Py[], int n)
{
// If there are 2 or 3 points, then use brute force
if (n <= 3)
return bruteForce(Px, n);
// Find the middle point
int mid = n/2;
Point midPoint = Px[mid];
Point Pyl[mid+1]; // y sorted points on left of vertical line
Point Pyr[n-mid-1]; // y sorted points on right of vertical line
int li = 0, ri = 0; // indexes of left and right subarrays
for (int i = 0; i < n; i++)
{
if (Py[i].x <= midPoint.x)
Pyl[li++] = Py[i];
else
Pyr[ri++] = Py[i];
}
float dl = closestUtil(Px, Pyl, mid);
float dr = closestUtil(Px + mid, Pyr, n-mid);
// Find the smaller of two distances
float d = min(dl, dr);
Point strip[n];
int j = 0;
for (int i = 0; i < n; i++)
if (abs(Py[i].x - midPoint.x) < d)
strip[j] = Py[i], j++;
return min(d, stripClosest(strip, j, d) );
}
float closest(Point P[], int n)
{
Point Px[n];
Point Py[n];
for (int i = 0; i < n; i++)
{
Px[i] = P[i];
Py[i] = P[i];
}
qsort(Px, n, sizeof(Point), compareX);
qsort(Py, n, sizeof(Point), compareY);
// Use recursive function closestUtil() to find the smallest distance
return closestUtil(Px, Py, n);
}
// Driver program to test above functions
int main()
{
Point P[] = {{795, 981}, {1905, 4574}, {8891, 665}, {6370, 1396}, {93, 8603}, {302, 7099},
{326, 5318}, {4493, 3977}, {429, 8687}, {9198, 1558}};
int n = sizeof(P) / sizeof(P[0]);
cout << closest(P, n) << std::endl;
cout << bruteForce(P, n) << std::endl;
return 0;
}
有没有人知道什么是错在这里?我一直在试图修复它几个小时,但我真的不明白,为什么这个问题会发生。
由于Pyl
和Pyr
具有mid+1
和n-mid-1
的大小分别以下两行
float dl = closestUtil(Px, Pyl, mid );
float dr = closestUtil(Px+mid, Pyr, n-mid);
应改写如下:
float dl = closestUtil(Px, Pyl, mid+1 );
float dr = closestUtil(Px+mid+1, Pyr, n-mid-1);
此外,如在this site的源代码评论的,在上面的代码,假定所有的x坐标是不同的。例如,如果所有的x坐标相同,li
从0
增加到n
和发生在Pyl[li++] = Py[i];
意外的行为li >= mid+1
时。
顺便说一句,VLA(可变Vength阵列)根本不会在C ++规范存在。因为数组Px
,Py
,Pyl
和Pyr
可在具有自动存储持续时间堆叠产生,它们的尺寸应在编译时被确定。但是,一些C ++编译器包括GNU编译器支持VLA作为编译器扩展并允许声明C风格阵列具有动态长度。 (内存是如何分配的VLA是具体实施。)但是,C ++提供了std::vector
动态数组功能,这可能使我们的代码更易读,便携和坚固。