Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 给定二维点列表,查找距离所有其他点最近的点_Algorithm_Language Agnostic - Fatal编程技术网

Algorithm 给定二维点列表,查找距离所有其他点最近的点

Algorithm 给定二维点列表,查找距离所有其他点最近的点,algorithm,language-agnostic,Algorithm,Language Agnostic,找到最接近所有其他点的点的有效算法是什么 我只能想到一个暴力O(n^2)解决方案: Input: list of 2d points (x,y) where x and y are integers. Distance: distance is defined as the Manhattan distance. ie: def dist(p1,p2) return abs(p1.x-p2.x) + abs(p1.y - p2.y) 基本上看每一对点。你

找到最接近所有其他点的点的有效算法是什么

我只能想到一个暴力O(n^2)解决方案:

Input: list of 2d points (x,y) where x and y are integers.

Distance: distance is defined as the Manhattan distance. 
    ie:
    def dist(p1,p2) 
         return abs(p1.x-p2.x) + abs(p1.y - p2.y)

基本上看每一对点。

你要找的是

你基本上把所有的x'和y'加在一起,然后除以整个系统的质量。 现在,你有了均匀的质量更小的粒子,让它们的质量为1

然后你要做的就是求和粒子的位置,除以粒子的数量

假设我们有p1(1,2)p2(1,1)p3(1,0)

因此p2是最接近的


在O(n)

中求解从初始算法开始,有一种可能的优化:

// we sum the x's 

    bestXcord = (1+1+1)/3 = 1

//we sum the y's 

    bestYcord = (2+1)/3 = 1 
我们的想法是,尽可能快地剔除异常值。这可以进一步改进:

  • 以一个启发式的“内部”点开始p1循环(这会首先创建一个好的
    思维者,所以更坏的点会更快地被丢弃)
  • 使用启发式“外部”点启动p2循环(这会使距离快速上升,可能会更快地触发退出条件
如果你以尺寸换取速度,你可以走另一条路线:

minDist=inf
bestPoint = null
for p1 in points:
    dist = 0
    for p2 in points:
        dist+=distance(p1,p2)
        //This will weed out bad points rather fast
        if dist>=minDist then continue(p1)
    /*
    //Unnecessary because of new line above
    minDist = min(dist,minDist)
    bestPoint = argmin(p1, bestPoint)
    */
    bestPoint = p1
//假设点编号为0..n
dist[]=int[n+1];//初始化为0

对于(i=0;i注意,在1-D中,使到所有点的距离之和最小的点是中值

在二维中,问题可以在
O(n log n)
中解决,如下所示:

创建一个x坐标排序数组,并为数组中的每个元素计算选择该坐标的“水平”成本。元素的水平成本是到投影到x轴上的所有点的距离之和。这可以通过扫描数组两次以线性时间计算(一次从左向右,一次反向)。同样地,创建y坐标的排序数组,并为数组中的每个元素计算选择该坐标的“垂直”成本


现在,对于原始数组中的每个点,我们可以通过添加水平和垂直成本来计算
O(1)
时间中所有其他点的总成本。因此,我们可以计算
O(n)
中的最佳点。因此,总运行时间是
O(n log n)

这不是欧几里德距离,需要对每个点进行平方(p1.x-p2.x)和(p1.y-p2.y),求和的平方根。方法
dist()
是曼哈顿距离。哎呀,我不是指欧几里德距离。我会编辑它。不完全是。质心不一定是集合中的一个点,而要求你寻找的点必须是集合中的点。例如,
(0,2),(0,0)
-你不能选择
(0,1)
作为答案。我怀疑离质心最近的点可以-但你能证明它吗?我同意,它并不完美。这种关联如此美妙,我不得不把它作为一个答案。当然,对这组点进行另一次传递,以找到离“质心”最近的点从第二个thaught开始,仍然是O(n),不-它肯定不行。想想:
(0,0),(0,0),(0,0),(5000,0),(10000,0)
15000/5=3000
。最近的是
(5000,0)
,距离之和是
20000
。如果选择(0,0)求和将为15000只有当你有一个近似平坦的分布时,质心参数才起作用-它会断裂,例如在环上(质量分布的微小变化使许多内点中的一个成为最佳点)。我确信的部分是最初的断言:“在1d中,使到所有点的距离之和最小的点是中位数”。如果这是真的,我们可以使用线性时间中位数算法在O(n)中解决这个问题。是的,在一维中,它可以在线性时间中解决。但在二维中,问题是点:(,)可能不是输入点。这是“质心”再次论证:它需要一个平坦(且密集)的空间分布points@EugenRieck我不明白你的意思。我没有计算重心。没有看到你的算法是如何在O(nlogn)中完成的。根据你的描述,你计算数组中每个元素的水平/垂直成本,这个成本是O(n).有n个这样的元素,表示O(n^2)。我误解了什么吗?
minDist=inf
bestPoint = null
for p1 in points:
    dist = 0
    for p2 in points:
        dist+=distance(p1,p2)
        //This will weed out bad points rather fast
        if dist>=minDist then continue(p1)
    /*
    //Unnecessary because of new line above
    minDist = min(dist,minDist)
    bestPoint = argmin(p1, bestPoint)
    */
    bestPoint = p1
//assumes points are numbered 0..n
dist[]=int[n+1]; //initialized to 0
for (i=0;i<n;i++)
  for (j=i+1;j<=n;j++) {
    dist=dist(p[i], p[j]);
    dist[i]+=dist;
    dist[j]+=dist;
  }
minDist=min(dist);
bestPoint=p[argmin(dist)];