(1.5.2.10)寻找最近点对

题目:平面中有若干个点,寻找距离最近的两个点。

分析:

方法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的程序如下:


[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <algorithm>  
  3. #include <vector>  
  4. #include <math.h>  
  5. class Point {  
  6.  public:  
  7.   Point(int x, int y) : x_(x), y_(y) {}  
  8.   Point() : x_(0), y_(0) {}  
  9.   static bool OrderByX(const Point& left, const Point& right) {  
  10.     return left.x_ < right.x_;  
  11.   }  
  12.   static bool OrderByY(const Point& left, const Point& right) {  
  13.     return left.y_ < right.y_;  
  14.   }  
  15.   int x_;  
  16.   int y_;  
  17. };  
  18. float Distance(const Point& left, const Point& right) {  
  19.   return sqrt(pow(left.x_ - right.x_, 2) + pow(left.y_ - right.y_, 2));  
  20. }  
  21. int NearestPoints(const std::vector<Point>& points, int start, int end, Point* point1, Point* point2) {  
  22.   if (end > start) {  
  23.   int middle = (start + end) / 2;  
  24.   int left_min_distance = NearestPoints(points, start, middle, point1, point2);  
  25.   int right_min_distance = NearestPoints(points, middle + 1, end, point1, point2);  
  26.   int min_distance = left_min_distance > right_min_distance ? right_min_distance : left_min_distance;  
  27.   std::vector<Point> left_part_points;  
  28.   for (int i = start; i <= middle; ++i) {  
  29.     if (points[middle].x_ - points[i].x_ <= min_distance) {  
  30.       left_part_points.push_back(points[i]);  
  31.     }  
  32.   }  
  33.   sort(left_part_points.begin(), left_part_points.end(), Point::OrderByY);  
  34.   std::vector<Point> right_part_points;  
  35.   for (int i = middle + 1; i <= end; ++i) {  
  36.     if (points[i].x_ - points[middle].x_ <= min_distance) {  
  37.       right_part_points.push_back(points[i]);  
  38.     }  
  39.   }  
  40.   sort(right_part_points.begin(), right_part_points.end(), Point::OrderByY);  
  41.   int distance_y = 0;  
  42.   int point_distance = 0;  
  43.   for(int i = 0; i < left_part_points.size(); ++i) {  
  44.     for(int j = 0; j < right_part_points.size(); ++j) {  
  45.       distance_y = left_part_points[i].y_ > right_part_points[j].y_ ? left_part_points[i].y_ - right_part_points[j].y_ :  
  46.                    right_part_points[j].y_ - left_part_points[i].y_;  
  47.       if (distance_y <= min_distance) {  
  48.         point_distance = Distance(left_part_points[i], right_part_points[j]);  
  49.         if (point_distance < min_distance) {  
  50.           min_distance = point_distance;  
  51.           *point1 = left_part_points[i];  
  52.           *point2 = right_part_points[j];  
  53.         }  
  54.       }  
  55.     }  
  56.   }  
  57.   return min_distance;  
  58.   } else {  
  59.     return 0x7FFFFFFF;  
  60.   }  
  61. }  
  62.     
  63.       
  64. int main(int argc, char** argv) {  
  65.   std::vector<Point> points;  
  66.   points.push_back(Point(2,3));  
  67.   points.push_back(Point(1,4));  
  68.   points.push_back(Point(3,0));  
  69.   points.push_back(Point(5,0));  
  70.   points.push_back(Point(5,1));  
  71.   sort(points.begin(), points.end(), Point::OrderByX);  
  72.   Point point1;  
  73.   Point point2;  
  74.   NearestPoints(points, 0, points.size() - 1, &point1, &point2);  
  75.   printf("Point1: (%d, %d) <--> Point2: (%d, %d)\n", point1.x_, point1.y_, point2.x_, point2.y_);  
  76. }  


其中,寻找矩形带内的点可以通过二分搜索来寻找,为了简单,用了循环来寻找。


参考文献:

编程之美 2.11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值