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.
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.
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 =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With