Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a set of points, how do I find the two points that are farthest from each other? [duplicate]

Possible Duplicate:
Greatest linear dimension 2d set of points

I could compute the distance between each point and take the largest but that doesn't sound like a very efficient way to do it when there are a large (> 1000) number of points.

Note: This is for iPhone so I don't have a ton of processing power.

like image 392
willc2 Avatar asked Oct 24 '09 16:10

willc2


2 Answers

Why not just compute the convex hull of the points? Depending on the algorithm you use, it takes either O(n) or O(n log n) time and eliminates all the inner points from consideration. Then, only check these outermost points to find the two that are the farthest away.

like image 101
Chris Bunch Avatar answered Nov 19 '22 18:11

Chris Bunch


You're asking to compute the diameter of the set. The standard technique is to first compute the convex hull, which reduces the problem to finding the diameter of a convex polygon. Even in the case where you don't eliminate any points, this added information is exactly what's needed to solve the problem efficiently. However, finding the diameter of a convex polygon is not entirely trivial; several published papers with algorithms for this task turn out to be incorrect.

Here's a fairly readable discussion of a correct O(n) algorithm for the task (where n is the number of points in the convex hull).

Also, note the the iphone isn't that limited; a carefully written implementation of even the completely naive algorithm can handle 1000 points in less than a tenth of a second. Of course, using the correct algorithm will let you go much faster =)

like image 29
Stephen Canon Avatar answered Nov 19 '22 19:11

Stephen Canon