题目:平面中有若干个点,寻找距离最近的两个点。
分析:
方法1:两两点比较,寻找最近的两个点对,复杂度O(N^2),优点代码简单不容易出错
方法2:观察两两比较的方法,发现有很多无用的比较,对于每一个点只要计算到它最近的点的距离就可以了,枚举所有的点,最后得出距离最近的一对点,但对于一个给定的点,如何找到距离它最近的点呢?可以使用一些启发式规则,减少比较的次数,例如:对于(x,y)取x轴投影上最近的点p1(x1,y1)和取y轴上投影最近的点p2(x2,y2),以(x,y)为一个定点,取x1,x2中较大者x',y1,y2中较大者y',构成(x‘,y’)为矩形的另一个顶点,计算矩形内所有的点与(x,y)的距离,从而减少了比较的次数。再以(x,y)为中心,判断4个项限内的所有点,最后得出距离(x,y)最小的点。复杂度是O(N*K),K为要判断的点的平均个数
方法3:采用分治法,即从x轴将数据不断分成两部分,两部分最近的两点取其小,在进行两部分中间比较,通过两部分的最小距离,缩小了中间带状的点数量,详见编程之美的分析。
算法
核心是分治算法
1. 分别根据点的 x,y 值进行排序
2. 在 x 轴上划一道垂线, 将点均分成两半
3. 假设最近点对都在左/右部分, 递归计算左/右半部分的最短距离并返回较小值 dis
4. 假设最近点对分别在左右两个部分, 横跨中心的竖线. 中心线为中心, 2*dis 为宽度画一个矩形, 横跨中心线的最近点对 candidate 都在这个矩形内. 将这些点按照 y 值的大小加入到数组中. 遍历数组中的点, 将该点与其后的 7 个点计算距离, 返回最小距离
5. 为什么仅和 7 个点作对比呢. 因为已经假设 dis 是左右不分最近点对的最小值, 这就说明在一个长(宽)为 dis 的正方形内, 至多有 4 个点. 长为 dis*2, 宽为 dis 的长方形至多 8 个.
按照方法3的程序如下:
- #include <stdio.h>
- #include <algorithm>
- #include <vector>
- #include <math.h>
- class Point {
- public:
- Point(int x, int y) : x_(x), y_(y) {}
- Point() : x_(0), y_(0) {}
- static bool OrderByX(const Point& left, const Point& right) {
- return left.x_ < right.x_;
- }
- static bool OrderByY(const Point& left, const Point& right) {
- return left.y_ < right.y_;
- }
- int x_;
- int y_;
- };
- float Distance(const Point& left, const Point& right) {
- return sqrt(pow(left.x_ - right.x_, 2) + pow(left.y_ - right.y_, 2));
- }
- int NearestPoints(const std::vector<Point>& points, int start, int end, Point* point1, Point* point2) {
- if (end > start) {
- int middle = (start + end) / 2;
- int left_min_distance = NearestPoints(points, start, middle, point1, point2);
- int right_min_distance = NearestPoints(points, middle + 1, end, point1, point2);
- int min_distance = left_min_distance > right_min_distance ? right_min_distance : left_min_distance;
- std::vector<Point> left_part_points;
- for (int i = start; i <= middle; ++i) {
- if (points[middle].x_ - points[i].x_ <= min_distance) {
- left_part_points.push_back(points[i]);
- }
- }
- sort(left_part_points.begin(), left_part_points.end(), Point::OrderByY);
- std::vector<Point> right_part_points;
- for (int i = middle + 1; i <= end; ++i) {
- if (points[i].x_ - points[middle].x_ <= min_distance) {
- right_part_points.push_back(points[i]);
- }
- }
- sort(right_part_points.begin(), right_part_points.end(), Point::OrderByY);
- int distance_y = 0;
- int point_distance = 0;
- for(int i = 0; i < left_part_points.size(); ++i) {
- for(int j = 0; j < right_part_points.size(); ++j) {
- distance_y = left_part_points[i].y_ > right_part_points[j].y_ ? left_part_points[i].y_ - right_part_points[j].y_ :
- right_part_points[j].y_ - left_part_points[i].y_;
- if (distance_y <= min_distance) {
- point_distance = Distance(left_part_points[i], right_part_points[j]);
- if (point_distance < min_distance) {
- min_distance = point_distance;
- *point1 = left_part_points[i];
- *point2 = right_part_points[j];
- }
- }
- }
- }
- return min_distance;
- } else {
- return 0x7FFFFFFF;
- }
- }
- int main(int argc, char** argv) {
- std::vector<Point> points;
- points.push_back(Point(2,3));
- points.push_back(Point(1,4));
- points.push_back(Point(3,0));
- points.push_back(Point(5,0));
- points.push_back(Point(5,1));
- sort(points.begin(), points.end(), Point::OrderByX);
- Point point1;
- Point point2;
- NearestPoints(points, 0, points.size() - 1, &point1, &point2);
- printf("Point1: (%d, %d) <--> Point2: (%d, %d)\n", point1.x_, point1.y_, point2.x_, point2.y_);
- }
参考文献:
编程之美 2.11