We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic control, you may want to monitor planes that come too close together, since this may indicate a possible collision. Recall the following formula for distance between two points p and q.
We have discussed a divide and conquer solution for this problem. The time complexity of the implementation provided in the previous post is O(n (Logn)^2). In this post, we discuss implementation with time complexity as O(nLogn).
Following is a recap of the algorithm discussed in the previous post.
1) We sort all points according to x coordinates.
2) Divide all points in two halves.
3) Recursively find the smallest distances in both subarrays.
4) Take the minimum of two smallest distances. Let the minimum be d.
5) Create an array strip that stores all points which are at most d distance away from the middle line dividing the two sets.
6) Find the smallest distance in strip.
7) Return the minimum of d and the smallest distance calculated in above step 6.
The great thing about the above approach is, if the array strip is sorted according to y coordinate, then we can find the smallest distance in strip in O(n) time. In the implementation discussed in the previous post, strip was explicitly sorted in every recursive call that made the time complexity O(n (Logn)^2), assuming that the sorting step takes O(nLogn) time.
In this post, we discuss an implementation where the time complexity is O(nLogn). The idea is to presort all points according to y coordinates. Let the sorted array be Py. When we make recursive calls, we need to divide points of Py also according to the vertical line. We can do that by simply processing every point and comparing its x coordinate with x coordinate of the middle line.
Following is C++ implementation of O(nLogn) approach.
The smallest distance is 1.41421
Time Complexity:Let Time complexity of above algorithm be T(n). Let us assume that we use a O(nLogn) sorting algorithm. The above algorithm divides all points in two sets and recursively calls for two sets. After dividing, it finds the strip in O(n) time. Also, it takes O(n) time to divide the Py array around the mid vertical line. Finally finds the closest points in strip in O(n) time. So T(n) can be expressed as follows
T(n) = 2T(n/2) + O(n) + O(n) + O(n)
T(n) = 2T(n/2) + O(n)
T(n) = T(nLogn)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Closest Pair of Points using Divide and Conquer algorithm
- Find the K closest points to origin using Priority Queue
- Find K Closest Points to the Origin
- Number of Integral Points between Two Points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Minimum number of points to be removed to get remaining points on one side of axis
- Ways to choose three points with distance between the most distant points <= L
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Number of ordered points pair satisfying line equation
- Find closest number in array
- Print X array elements closest to the Kth smallest element in the array
- LS3/NS3 sphere generation algorithm and its implementation
- Window to Viewport Transformation in Computer Graphics with Implementation
- Find Simple Closed Path for a given set of points
- Orientation of 3 ordered points
- Count Integral points inside a Triangle
- Non-crossing lines to connect points in a circle
- Count maximum points on same line
- Circle and Lattice Points
- Queries on count of points lie inside a circle