Q) Find the closest pair of points in a given set of points.
Expected time complexity: O(nlog2(n)) / O(nlog(n))
i was reading up this problem from here. However i am unable to understand a few things:
what do we achieve by sorting points by Y which are already sorted by X?
when we take a point as mid from one of the halves, dont we miss out on a few cases? i.e. how does taking a mid point ensure that the strip formed includes all the points that are candidates for a distance less than the distance returned by the two recursive calls?
even if the above point is proved, shouldnt we take the mid point of both halves i.e. m of left half and (m+1) of right half?
2) You use the x coordinate of the mid point to find a line that splits the points in half. The recursive calls compare all pairs that don't cross that line, so all that remains is to compare pairs that do cross the line. If two points are less than d apart, and they're on either side of the line, then they must both be less than d away from the line.
3) Well, that will be done in the recursive calls. I don't know what else you could mean.
1) The points in the strip are separated out and sorted. Then, given one point in the strip, all the points with y coordinate within d are in a contiguous region around it. These are the only other points you need to compare it with, and the sorting makes it quick and easy to find these points. The magic of this algorithm is that the number of possible points in any such region is strictly limited to a constant value. This follows from the fact (established by the recursive calls) that no pair of points on either side of the line are closer than d.