Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档 > MATLAB技术文章
MATLAB技术文章 MATLAB Technical Articles From Mathworks
回复
 
主题工具 显示模式
旧 2024-03-29, 06:55   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
默认 Closest Pair of Points Problem - Cleve Moler on Mathematics and Computing

The Closest Pair of Points problem is a standard topic in an algorithms course today, but when I taught such a course fifty years ago, the algorithm was not yet known.

Contents
California Dreaming

Imagine you are driving a car on the Harbor Freeway in southern California with typical Los Angeles traffic conditions. Among the many things you might want to know is which pair of vehicles is nearest each other.

This is an instance of the Closest Pair of Points problem:
  • Given the location of n points in the plane, which pair of points is closest to each other?


Closest Pair of Points

It is convenient to represent the points by a vector of complex values. The distance between points z(k) and z(j) is then

d = abs(z(k) - z(j))Here are a few points in the unit square. The closest pair is highlighted.



Pairs

The first algorithm you might think of computes the distance between all possible pairs of points and finds the minimum. This is a brute force approach that requires only a few lines of code.

function d = Pairs(z) % Pairs. % d = Pairs(z) is the minimum distance between any two elements % of the complex vector z. n = length(z); d = Inf; for k = 1:n for j = k+1:n if abs(z(k) - z(j)) < d d = abs(z(k) - z(j)); end end endendDivCon

DivCon stands for Divide and Conquer. In outline, the steps are:
  • Divide the set of points into two halves.
  • Recursively, find the closest pair in each half.
  • Consider the case when the closest pair has one point in each half.
  • Terminate the recursion with sets of two or three points.
function d = DivCon(z,sorted) % DivCon. % d = DivCon(z) is the minimum distance between any two elements % of the complex vector z. % % d = DivCon(z,true) is a recursive call with ascending real(z). n = length(z); if n

More...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 03:06


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.