最近的垂直扫描对

时间:2014-12-30 03:23:01

标签: c++ algorithm computational-geometry

最近对的标准扫描线算法是众所周知的,如here所述,它使用扫描线水平扫过点集,仅维持当前点当前最佳距离内的扫描线。

通常,这些点最初必须按x坐标排序,并且边界框(在c ++实现的情况下为std :: set)必须按y坐标排序,如{{3}中所示。 }。

但是,在尝试实现时,我偶然忘记按x坐标对点进行排序,而是按y坐标对它们进行排序。令人惊讶的是,这似乎仍然有效。 您可以看到我的实现this c++ implementation,它基本上遵循标准lineweep最近对算法的略微修改版本:

#include <iostream>
#include <set>
#include <algorithm>
#include <math.h>
#include <vector>

using namespace std;

#define x second
#define y first

typedef pair<long long, long long> pll;

inline double dist(pll p1, pll p2)
{
    return sqrt((double) (p2.y - p1.y)*(p2.y - p1.y) + (p2.x - p1.x)*(p2.x - p1.x));
}


int main(int argc, const char * argv[])
{
    int numPoints;
    cin >> numPoints;

    vector <pll> points;
    points.resize(numPoints);

    for (int i = 0; i < numPoints; i++)
    {
        cin >> points[i].x >> points[i].y;
    }

    //Sorts the points by y coordinate (because y is first)
    sort(points.begin(), points.end());

    double shortestDistSoFar = INFINITY;
    set <pll> boundingBox; //Bounding box maintained by y-coordinate
    boundingBox.insert(points[0]);

    int left = 0;

    pll best1, best2;

    for (int i = 1; i < numPoints; i++)
    {
        //Maintain only points to the left of the current point whose distance is less than bestDist
        while ((left < i) && (points[i].x - points[left].x > shortestDistSoFar))
        {
            boundingBox.erase(points[left]);
            left++;
        }

        //Consider only points within bestDist of the current point
        for (auto it = boundingBox.lower_bound(pll(points[i].y - shortestDistSoFar, points[i].x - shortestDistSoFar));
             it != boundingBox.end() && it->y <= points[i].y + shortestDistSoFar; it++)
        {
            if (dist(*it, points[i]) < shortestDistSoFar)
            {
                shortestDistSoFar = dist(*it, points[i]);
                best1 = *it;
                best2 = points[i];
            }
        }

        boundingBox.insert(points[i]);
    }

    return 0;
}

按照增加y坐标的顺序访问每个点,并且对于每个点,检查从y-bestDist到y + bestDist的所有点,在找到新的最短距离时更新bestDist并从集合中移除与x坐标距离的点。当前点大于bestDist。

这个修改后的算法是否仍然有用(我只测试了几个案例),运行时间是否仍为O(N lgN)?

1 个答案:

答案 0 :(得分:4)

它确实可以正常工作(因为只有在可以安全删除它们时才会从集中删除这些点)。但是,它的时间复杂度为O(n ^ 2),因为这些点在应用时并不总是被删除。

这个简单的生成器(用python3编写):

from sys import argv

n = int(argv[1])
dy = 1
dx = -n
print(n)
for i in range(n):
  print(dx * i, dy * i)

创建一个测试用例,使您的代码对任何O(n ^ 2)执行n操作。

这个测试用例的想法非常简单:如果点(按y坐标排序后)按x坐标的递减顺序迭代,则永远不会从集合中删除它们。因此,如果它们通过y坐标和远x坐标彼此靠近,则每次遍历整个集合。这就是检查所有点对的原因(并且确实有n * (n - 1) / 2对)。

现在让我们看看它在实践中的运作方式:
首先,我用g++ -std=c++11 -O2 closest_pair.cpp编译了你的代码。 之后我跑了一堆测试:

temp$ python3 gen.py 10000 > input
temp$ time ./a.out < input

real    0m0.805s
user    0m0.797s
sys     0m0.008s

temp$ python3 gen.py 30000 > input
temp$ time ./a.out < input

real    0m7.195s
user    0m7.198s
sys     0m0.004s

temp$ python3 gen.py 50000 > input
temp$ time ./a.out < input

real    0m23.711s
user    0m23.725s
sys     0m0.004s

正如您所看到的,它的效果非常慢。

相关问题