views:

66

answers:

2

Given n points on the outline of the unit circle, I want to calculate the closest 2 points.

The points are not ordered, and I need to do it in O(n) (so I cannot sort them clockwise...)

I once knew the solution for this, but forgot it... the solution includes hashing, and splitting the circle to n or more slices.

If you found an algorithm to calculate only the distance, and not the specific points, it will be good enough..

A: 

Sort them by angle by placing them in bins (binsort, O(n)); choose a number of bins of the same order as the number of points. Then walk through and find the closest pair, repeating the process within a bin if more than one point falls in it.

Rex Kerr
binsort is based on the assumption that the points were picked randomly, right? I'm not sure that I can assume that, but I'll ask tommorrow.
Oren
@Oren - You are correct about the assumption. I could give you a set of points that are spaced apart with exponentially decreasing distance, and you're not going to do better than `O(N log N)` with that. My suggestion is the same as the one @Jim Lewis linked to; I don't know how they get `O(N log log N)`; in the random case, you expect a reduction of the number of points to examine by (e-2)/e per `O(N)` step (if you choose # bins = # points (property of Poisson distribution)), and the geometric series of that is just 1/(1-e/(e-2)) = (e-2)/2, which is just a constant factor.
Rex Kerr
+1  A: 

Here's a solution that purports to be O(n log log n) for finding the closest pair of points on a line. This is a trivial transformation from your problem -- each point (x,y) on the unit circle maps to a linear coordinate x' in the line segment [0,2pi), where x' = atan2(y,x). The idea, once you've converted it to a 1-D problem, is roughly to start hashing the x' coordinates into buckets, repartitioning big buckets into smaller buckets until there's at most one point per bucket, then walk through the list and find the closest pair. (And you'd have one additional check to see if the points with the min and max x' coordinate form an even closer pair than the linear minimum.)

Jim Lewis
I didn't fully understand the O(n log log n) calculation, but I trust you on this. Anyway, it's not a O(n) solution. Can you make the fact that we're looking on a circle into an advantage instead of disadvantage?
Oren
On the other hand, if there's an algorithm that solves my problem in O(n), then I could map any "closest pair problem on a line" into half a circle, and solve it in O(n) instead of O(n log log n)...
Oren