简体   繁体   English

找到(x,y)坐标之间的最大距离

[英]Finding maximum distance between (x,y) coordinates

Im trying to calculate the maximum manhattan distance of a large 2D input , the inputs are consisting of (x, y)s and what I want to do is to calculate the maximum distance between those coordinates In less than O(n^2) time , I can do it in O(n^2) by going through all of elements sth like : 我试图计算一个大的2D输入的最大曼哈顿距离,输入由(x,y)s组成,我想要做的是计算这些坐标之间的最大距离小于O(n ^ 2)时间,我可以通过遍历所有元素来在O(n ^ 2)中完成它,例如:
*(Manhattan distance between two points (X1,Y1) and (X2,Y2) is : |X1-X2| + |Y1-Y2|) *(两点(X1,Y1)和(X2,Y2)之间的曼哈顿距离为:| X1-X2 | + | Y1-Y2 |)

for ( 0 -> n )  
   for ( 0-> n )   
       { // here i calculate |Xi - Xj| + |Yi - Yj| which is maximum }  

but It won't work efficiently for very large inputs :( 但对于非常大的输入它不会有效:(
anyone has any idea for a better algorithm ? 谁有任何想法更好的算法?

There are only two cases to consider, if we only consider results such that Xi <= Xj . 如果我们只考虑Xi <= Xj结果,则只有两种情况需要考虑。

  • If Yi <= Yj , then the distance is (Xj + Yj) - (Xi + Yi) 如果Yi <= Yj ,那么距离是(Xj + Yj) - (Xi + Yi)
  • Otherwise, the distance is (Xj - Yj) - (Xi - Yi) 否则,距离为(Xj - Yj) - (Xi - Yi)

By breaking it down into these cases, I have gotten rid of the absolute value function making it much easier to reason about the distances. 通过将其分解为这些情况,我已经摆脱了绝对值函数,使得更容易推断距离。

So, we simply pick points with minimum and maximum x+y , and compute the distance. 因此,我们只需选择具有最小和最大x+y ,并计算距离。 Then pick points with minimum and maximum xy , and compute the distance. 然后选择具有最小和最大xy ,并计算距离。 One of those two distances is your maximum. 这两个距离中的一个是你的最大距离。

This can be done in O(n) , which is asymptotically optimal. 这可以在O(n) ,这是渐近最优的。

It is fairly simple and can be calculated in O(n) 它非常简单,可以用O(n)计算

Let x1>x2 and y1>y2 x1>x2y1>y2

max(|x1-x2|+|y1-y2|) = max(x1-x2+y1-y2) = max(x1+y1) - min(x2+y2)

Let x1>x2 and y1<y2 x1>x2y1<y2

max(|x1-x2|+|y1-y2|) = max(x1-x2-y1+y2) = max(x1-y1) - min(x2-y2)

Now change x1 with x2 and you take the same results. 现在用x2更改x1并获得相同的结果。

So in general your solution is 所以一般来说你的解决方案是

max ( (max(xi+yi)-min(xi+yi)), (max(xi-yi) - min(xi-yi)) ) 

The best thing to do with questions like this is try to establish some small results that will help you with the overall problem. 对这样的问题做的最好的事情就是尝试建立一些可以帮助解决整体问题的小结果。

For example, it is not too hard to determine that for any three points, A, B and C, which have the condition that B is between (more on this in a second) A and C, B will never be further from a fourth point D than one of A and C. With the standard Euclidean metric of distance, a point is between two other points if it lies on the segment joining them. 例如,确定对于任何三个点A,B和C来说并不难,这些点具有B 介于其间的条件(更多关于此秒)A和C,B将永远不会超过第四个点点D大于A和C中的一个。对于距离的标准欧几里德度量,如果点位于连接它们的段上,则点在两个其他点之间。 For Manhattan measurements it is not so simple - partly because the concept of a segment is not as well understood. 对于曼哈顿测量而言,它并非如此简单 - 部分原因是因为段的概念不是很清楚。

A more general way of describing 'between' is this (using the notation that the distance from A to B is |AB|): A point B is between two points A, C if |AB| 描述'之间'的更一般方式是这样(使用A到B的距离为| AB |的符号):如果| AB |,则点B在两个点A,C之间。 + |BC| + | BC | = |AC| = | AC |

You can see that in Euclidean distance this means that B lies on the segment joining A and C. 你可以看到,在欧几里德距离中,这意味着B位于连接A和C的段上。

In Manhattan distance, this means that the point B is contained in the rectangle defined by A and C (which of course could be a straight segment if AC is parallel one of the axis). 在曼哈顿距离中,这意味着点B包含在由A和C定义的矩形中(如果AC与轴平行,则当然可以是直线段)。

This result means that for any point, if it lies between two existing points, it can be no further from any new points that are added to the set than the two which surround it. 这个结果意味着对于任何一点,如果它位于两个现有点之间,那么添加到集合中的任何新点都不会比围绕它的两个点更远。

Now, this information does not solve the issue for you, but it does let you throw away many potential future calculations. 现在,这些信息并没有为您解决问题,但它确实让您放弃了许多潜在的未来计算。 Once you have determined that a point is between two others, there is no point in tracking it. 一旦确定某个点位于另外两个点之间,就没有必要跟踪它。

So, you can solve this problem by only tracking the outermost points, and disregarding any that fall within. 因此,您可以通过仅跟踪最外面的点,并忽略其中的任何内容来解决此问题。

An interesting exercise for the casual observer 对于不经意的观察者来说,这是一项有趣

Prove that you can have no more than 4 distinct points such that none of the points are between two of the others, in the Manhattan sense. 证明你可以拥有不超过4个不同点,这样在曼哈顿意义上,其他两个点之间没有任何一点。

With this second result it becomes clear that you will only ever need to track up to 4 points. 通过第二个结果,您可以清楚地了解到最多只需要跟踪4个点。

Some of the other methods already presented are probably faster, but this way is more fun! 已经呈现的一些其他方法可能更快,但这种方式更有趣!

Extra Credit 额外信用

Generalise these ideas to n dimensions 将这些想法概括为n

the first big improvement would be: 第一个重大改进是:

for ( X: 0 -> n )
    for ( Y: X -> n )
        { compute the distance between X and Y }

since the distance between X and Y is the same as the distance between Y and X. that would cut your computation by a half... 因为X和Y之间的距离与Y和X之间的距离相同,这会使你的计算减少一半......

Maximum distance will be between most far from each other points. 最大距离将介于最远离彼此的点之间。 So you just have find dot with maximum X and maximum Y and then find dot with minimum X and minimum Y and calculate distance between them. 因此,您只需找到具有最大X和最大Y的点,然后找到具有最小X和最小Y的点并计算它们之间的距离。 There can be a lot of dots which will match criteria.. but at least yu'll have a much less amount of points to check 可能有很多点符合标准..但至少你会有更少的点来检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM