In the closest pair of points divide and conquer algorithm, what is the significance of sorting the "strip" by the points' y values?

StackOverflow https://stackoverflow.com/questions/19781518

Question

I believe I understand the algorithm quite clearly, except for the step where you look to see if there's any points that are close by looking across the division and create a strip where points within the strip are candidates.

But then the algorithm states to sort the points by their y coordinates and then check each other point in the strip to find if there is a smaller distance than the one previously found. It basically sounds like you brute force within the strip.

For example, here's what Introduction to Algorithms states:

enter image description here

So it seems you just take each point and compare it against all the others to find the closest points? Why is it necessary to sort by y value then? You already have them sorted by x, why not brute force with that?

Was it helpful?

Solution

You don't brute force compare against all points in Y' but only against the one next to p. If that one is already too far away you can just stop, because all other points will be even further away. You only continue evaluating the next closest neighbor if the last one was still within your search distance.

The text explains it in the As we will see shortly section.

Sorting is an optimization here that allows you to iterate nearest neighbors in O(1) after paying the sorting costs of O(n log n) once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top