What is the most efficient way to calculate the maximum distance of two points in a list?

后端 未结 2 430
再見小時候
再見小時候 2020-12-17 14:26

I have a list L of points (x, y) and the usual euclidean distance measure

\"enter

相关标签:
2条回答
  • 2020-12-17 15:11

    You should look up on Rotating calipers(http://en.wikipedia.org/wiki/Rotating_calipers) - they're widely used for that kind of problems. Also, your assumption is wrong. For a fixed point p on the convex polygon: the diagonal can first increase, then decrease, then increase, and then decrease again. At least, I've got a case where this happens.

    A heuristic approach also: select a point x. Find the furthest point y from it. Find the furthest point z from y. d(z,y) is a good estimation.

    The image that illustrates the diagonals:

    enter image description here

    1->2: increasing; 2->3 decreasing; 3->4 increasing; 4->5 decreasing. The figure may not be precise, move the points that 3 and 4 point to a bit further away from p (on the same line).

    0 讨论(0)
  • 2020-12-17 15:27

    Assuming you have a uniform distribution of the points you can do the following thing:

    Find max_x and min_x being the maximum and minimum X coordinates - (O(n)). Those value should help you pick a constant k as the "best" one for the current set of points. Different value of k will influence only the complexity of the algorithm.

    Consider a new data structure which is matrix like and is a vector of vectors or vector of linked lists, lets name it structure where structure[i] is the corresponding vector/linked lists (as described above). Populate this data structure as follows: structure[i] should contain points that have their x coordinate being in the range of [max_x+ik,max_x+(i+1)k] this will take another O(n) time and O(n) extra space. Now you sort every entry of structure[i] by y coordinate. Having this done it is enough to compute the distances (brute force) between the following set of points: structure[0], structure[structure.length()-1], the extremes (entry at first and last index) of every other structure[i].

    Basically this is almost the same as doing the convex hull and starting to compute the distances of the points that are on the hull, the difference is that picking the right k might either make it faster or slower. Having worst case complexity O(n^2) and best case complexity O(nLg(n)). Where k will influence the trade of either sorting bigger groups of points or having more points to compute the distances between.

    0 讨论(0)
提交回复
热议问题