Closest Pair
Last updated
Was this helpful?
Last updated
Was this helpful?
A set of points (x,y) we'll call P;
Return the two points that are closest to each other.
We define closest by euclidean distance
No duplicate points
Double for loop, calculate the distance between every possible point, maintains the minimum along the way.
It is worth noting that the upper bound is n^2, but really we are calculating nC2 combination pairs. For a visual of this, see Combinatorics chapter in ADT gitbook.
While a brute force solution still exists, we can do better
If we limited our points in d-1 we can solve this in nlogn by:
Sorting -> nlogn
Scanning through it again, and then finding the minimum distance between two pairs of points. -> O(N)
Motivated by our 1-d case, we may want to try and sort both the x and y coordinate sets by their dimension and then go through there.
But, we can easily derive an example where although 2 coordinates appear very per 1 axis, they are in reality a lot further in the other axis.
We find that our 1-dimensional approach will not work.
Split the pairs of points vertically, such that there are the same amount of points on each side (if odd, differ by 1).
We accomplish this by sorting the points respectfully by x-cord, and y-cord - and finding the median.
Solve the closest pairs of points on 1st half, and then in the 2nd half.
The idea is to continue dividing our data in half until we reach the point where we have 2 or 3 points. Our return condition would be to then use brute force to find the closest pair of points for that call.
We do the same thing with the right hand side.
Then we find the minimum of the two sides, lets call this min_d
Finally, we only consider points from the vertical line + mid_d distance away. We can be assured that points further than mid_d from the median line are not the closest.
Here we find the distance between crossing points.
Sort points by x-cord <- Px
Sort points by y-cord <- Py
Split Px in half (median; size/2)
At this point if we calculate both sides, then it would take each for each. Which is still of order . We have also yet to consider the closest pair of points that are considered between these two discrete sets.