views:

574

answers:

5

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.

+4  A: 

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.

Marcelo Cantos
Now assume all given N points are on the convex hull. Still O(N)?
Pavel Shved
Yes, because you 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 vertices, you will have to advance either one walker or the other, but it will always take O(N) to circumnavigate the hull.
Marcelo Cantos
Please, post a complete algorithm for checking what points are furthest, given the convex hull (a set of nodes and their order).
Pavel Shved
Sorry, that's more work than I can be bothered with right now.
Marcelo Cantos
Well, your comment anyway is the best explanation of a correct algorithm on this page, so I'll put it into answer text and upvote then.
Pavel Shved
Thanks Pavel! I was not trying to be dismissive, by the way (my last comment reads worse than I intended). It's just a fair bit of fiddly geometry and edge-cases to get it right.
Marcelo Cantos
@Marcelo, oh, saying "complete algorithm" in computational geometry usually means "as complete as possible before it gets boring" :-)
Pavel Shved
This has a name: http://en.wikipedia.org/wiki/Rotating_calipers
Rafał Dowgird
Very cool! Thanks Rafal (And here was I, thinking I made it up ;-).
Marcelo Cantos
A: 

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.

Adrien
+4  A: 

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: http://stackoverflow.com/questions/477591/algorithm-to-find-two-points-furthest-away-from-each-other

Also:

zaf
That questions is about graph algorithms, not computational geometry, isn't it?
Pavel Shved
you can model the problem as a complete weighted graph
jk
good answer, I like it.
ldog
A: 

A starting point could be looking at closest-point problems, which have been examined. Wikipedia lists some options:

http://en.wikipedia.org/wiki/Closest_point_query

Joel Goodwin
Or http://en.wikipedia.org/wiki/Closest_pair_problem
zaf
Well, the solutions for closest base on divide and conquer, and I have no idea how to apply it to finding the furthest apart points.
depesz
A: 

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. =)

Jens
Consider this, and then correct or delete your answer so you don't get downvotes: imagine you have an almost-square set of points, `{A=(0, 0), B=(10, 10), C=(10, 0), D=(0, 11)}`; most distant points are BD (distance = 14.86); but if you try to start at A or B you'll feel tempted to remove C (AC = BC = 10) or D (AD = 11, BD = 10.05) since they're closer to the chosen vertex than the opposite vertex (AC = 14.14), **while in reality the longest distance is really 14.86** .
ANeves
@sr pt: In your example, CD are the most distant points, not BD. Starting in A or B one would always choose the other in step 2, so only these two get removed in step 4.
Jens