How to find two most distant points?

2020-01-27 01:12发布

This is a question that I was asked on a job interview some time ago. And I still can't figure out sensible answer.

Question is:

you are given set of points (x,y). Find 2 most distant points. Distant from each other.

For example, for points: (0,0), (1,1), (-8, 5) - the most distant are: (1,1) and (-8,5) because the distance between them is larger from both (0,0)-(1,1) and (0,0)-(-8,5).

The obvious approach is to calculate all distances between all points, and find maximum. The problem is that it is O(n^2), which makes it prohibitively expensive for large datasets.

There is approach with first tracking points that are on the boundary, and then calculating distances for them, on the premise that there will be less points on boundary than "inside", but it's still expensive, and will fail in worst case scenario.

Tried to search the web, but didn't find any sensible answer - although this might be simply my lack of search skills.

11条回答
对你真心纯属浪费
2楼-- · 2020-01-27 01:29

A stochastic algorithm to find the most distant pair would be

  • Choose a random point
  • Get the point most distant to it
  • Repeat a few times
  • Remove all visited points
  • Choose another random point and repeat a few times.

You are in O(n) as long as you predetermine "a few times", but are not guaranteed to actually find the most distant pair. But depending on your set of points the result should be pretty good. =)

查看更多
做个烂人
3楼-- · 2020-01-27 01:30

Just a few thoughts:

You might look at only the points that define the convex hull of your set of points to reduce the number,... but it still looks a bit "not optimal".

Otherwise there might be a recursive quad/oct-tree approach to rapidly bound some distances between sets of points and eliminate large parts of your data.

查看更多
再贱就再见
4楼-- · 2020-01-27 01:31

look at right angle triangles A-(x1, y1), B-(x2, y2) and the distance b/w A and B is = sqrt[(|x1|+|x2|)^2 + (|y1|+|y2|)^2]

查看更多
混吃等死
5楼-- · 2020-01-27 01:35

Boundary point algorithms abound (look for convex hull algorithms). From there, it should take O(N) time to find the most-distant opposite points.

From the author's comment: first find any pair of opposite points on the hull, and then walk around it in semi-lock-step fashion. Depending on the angles between edges, you will have to advance either one walker or the other, but it will always take O(N) to circumnavigate the hull.

查看更多
倾城 Initia
6楼-- · 2020-01-27 01:37

EDIT: One way is to find the convex hull http://en.wikipedia.org/wiki/Convex_hull of the set of points and then the two distant points are vertices of this.

Possibly answered here: Algorithm to find two points furthest away from each other

Also:

查看更多
淡お忘
7楼-- · 2020-01-27 01:38

Find the mean of all the points, measure the difference between all points and the mean, take the point the largest distance from the mean and find the point farthest from it. Those points will be the absolute corners of the convex hull and the two most distant points. I recently did this for a project that needed convex hulls confined to randomly directed infinite planes. It worked great.

查看更多
登录 后发表回答