分治法求距离最近的点对

v_olcano 2005-09-14 05:30:28
哪位高手能解释一下这个算法.讲一下大概.
我在网上找到有关的,因为没有图例,没怎么看明白.
这个地方的.http://www.zahui.com/html/13/29684.htm

...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangc_jerry 2005-09-17
  • 打赏
  • 举报
回复
复杂度为O(nlogn)
wangc_jerry 2005-09-17
  • 打赏
  • 举报
回复
pair< double, pair> dq_closest_pair(
vector& sorted_points,
vector& y_sorted_points )
{
// if the size of sorted_points is less than BASE_CASE_NUM_POINTS,
// call naive_closest_pair( sorted_points ) instead.
// otherwise,
// find median point in sorted_points (point in the middle of the vector)
// let median point be p = (xp, yp). If all points have the
// same x coordinate, we must still split list in half, so we
// must sort points with the same x coordinate by their y
// coordinate. Implement operator< on points to do this.
//
// copy first half of sorted_points into left_sorted_points
// copy second half of sorted_points into right_sorted_points
/* Now select the points from y_sorted_points which are in
* left_sorted_points, by checking if their x coordinate in
* less than xp, or, if equal to xp, if their y coordinate is less.
* In other words, if a < p, using operator<. Select these points
* into a new vector, keeping the same order. Thus, these points
* remain sorted by their y coordinates. Call this new vector
* left_y_sorted_points.
*/
// Similarly, create right_y_sorted_points.
// Call dq_closest_pair( left_sorted_points, left_y_sorted_points ),
// and remember the return value.
// similarly, call dq_closest_pair() for the right half.

// Select the minimum distance from the calls to dq_closest_pair(),
// call it d.

// Create two new vectors from left_y_sorted_points and right_y_sorted_points,
// by selecting only those points with x coordinate > xp-d or < xp+d,
// respectively. Call these list_a and list_b. Iterate through list_a,
// keeping iterators
// pointing to points in list_b that are the beginning and end of the range
// in list_b that must be compared to the current point in list_a.
// These iterators in list_b will only move forward, as we move through
// list_a. Compare each point a = (xa, ya) in list_a to those points
// b = (xb,yb) in list_b such that ya - d < yb < ya+d. Find the
// distance between all these pairs, and if a distance is less than
// d, update d and the current best pair of points appropriately.
// If d changes, we don't need to go back and reselect list_a and list_b.
// Just keep searching the current lists until the end.

// Then return the shortest distance d and the best pair of points.
}
gauss 2005-09-14
  • 打赏
  • 举报
回复
我记得算法导论(也就是Introduction To Algorithm)有专门讲到这个问题。

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧