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.a

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 an 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.

[ad type=”banner”]

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 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 middle line.

Following is C++ implementation of O(nLogn) approach

// A divide and conquer program in C++ to find the smallest distance from a
// given set of points.

[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0A%23include%20%3Cfloat.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Cmath.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20structure%20to%20represent%20a%20Point%20in%202D%20plane%0Astruct%20Point%0A%7B%0A%20%20%20%20int%20x%2C%20y%3B%0A%7D%3B%0A%20%0A%20%0A%2F*%20Following%20two%20functions%20are%20needed%20for%20library%20function%20qsort().%0A%20%20%20Refer%3A%20http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fclibrary%2Fcstdlib%2Fqsort%2F%20*%2F%0A%20%0A%2F%2F%20Needed%20to%20sort%20array%20of%20points%20according%20to%20X%20coordinate%0Aint%20compareX(const%20void*%20a%2C%20const%20void*%20b)%0A%7B%0A%20%20%20%20Point%20*p1%20%3D%20(Point%20*)a%2C%20%20*p2%20%3D%20(Point%20*)b%3B%0A%20%20%20%20return%20(p1-%3Ex%20-%20p2-%3Ex)%3B%0A%7D%0A%2F%2F%20Needed%20to%20sort%20array%20of%20points%20according%20to%20Y%20coordinate%0Aint%20compareY(const%20void*%20a%2C%20const%20void*%20b)%0A%7B%0A%20%20%20%20Point%20*p1%20%3D%20(Point%20*)a%2C%20%20%20*p2%20%3D%20(Point%20*)b%3B%0A%20%20%20%20return%20(p1-%3Ey%20-%20p2-%3Ey)%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20the%20distance%20between%20two%20points%0Afloat%20dist(Point%20p1%2C%20Point%20p2)%0A%7B%0A%20%20%20%20return%20sqrt(%20(p1.x%20-%20p2.x)*(p1.x%20-%20p2.x)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(p1.y%20-%20p2.y)*(p1.y%20-%20p2.y)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%3B%0A%7D%0A%20%0A%2F%2F%20A%20Brute%20Force%20method%20to%20return%20the%20smallest%20distance%20between%20two%20points%0A%2F%2F%20in%20P%5B%5D%20of%20size%20n%0Afloat%20bruteForce(Point%20P%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20float%20min%20%3D%20FLT_MAX%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist(P%5Bi%5D%2C%20P%5Bj%5D)%20%3C%20min)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min%20%3D%20dist(P%5Bi%5D%2C%20P%5Bj%5D)%3B%0A%20%20%20%20return%20min%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20minimum%20of%20two%20float%20values%0Afloat%20min(float%20x%2C%20float%20y)%0A%7B%0A%20%20%20%20return%20(x%20%3C%20y)%3F%20x%20%3A%20y%3B%0A%7D%0A%20%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20the%20distance%20beween%20the%20closest%20points%20of%0A%2F%2F%20strip%20of%20given%20size.%20All%20points%20in%20strip%5B%5D%20are%20sorted%20accordint%20to%0A%2F%2F%20y%20coordinate.%20They%20all%20have%20an%20upper%20bound%20on%20minimum%20distance%20as%20d.%0A%2F%2F%20Note%20that%20this%20method%20seems%20to%20be%20a%20O(n%5E2)%20method%2C%20but%20it’s%20a%20O(n)%0A%2F%2F%20method%20as%20the%20inner%20loop%20runs%20at%20most%206%20times%0Afloat%20stripClosest(Point%20strip%5B%5D%2C%20int%20size%2C%20float%20d)%0A%7B%0A%20%20%20%20float%20min%20%3D%20d%3B%20%20%2F%2F%20Initialize%20the%20minimum%20distance%20as%20d%0A%20%0A%20%20%20%20%2F%2F%20Pick%20all%20points%20one%20by%20one%20and%20try%20the%20next%20points%20till%20the%20difference%0A%20%20%20%20%2F%2F%20between%20y%20coordinates%20is%20smaller%20than%20d.%0A%20%20%20%20%2F%2F%20This%20is%20a%20proven%20fact%20that%20this%20loop%20runs%20at%20most%206%20times%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20size%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20size%20%26%26%20(strip%5Bj%5D.y%20-%20strip%5Bi%5D.y)%20%3C%20min%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist(strip%5Bi%5D%2Cstrip%5Bj%5D)%20%3C%20min)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min%20%3D%20dist(strip%5Bi%5D%2C%20strip%5Bj%5D)%3B%0A%20%0A%20%20%20%20return%20min%3B%0A%7D%0A%20%0A%2F%2F%20A%20recursive%20function%20to%20find%20the%20smallest%20distance.%20The%20array%20Px%20contains%0A%2F%2F%20all%20points%20sorted%20according%20to%20x%20coordinates%20and%20Py%20contains%20all%20points%0A%2F%2F%20sorted%20according%20to%20y%20coordinates%0Afloat%20closestUtil(Point%20Px%5B%5D%2C%20Point%20Py%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20If%20there%20are%202%20or%203%20points%2C%20then%20use%20brute%20force%0A%20%20%20%20if%20(n%20%3C%3D%203)%0A%20%20%20%20%20%20%20%20return%20bruteForce(Px%2C%20n)%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20middle%20point%0A%20%20%20%20int%20mid%20%3D%20n%2F2%3B%0A%20%20%20%20Point%20midPoint%20%3D%20Px%5Bmid%5D%3B%0A%20%0A%20%0A%20%20%20%20%2F%2F%20Divide%20points%20in%20y%20sorted%20array%20around%20the%20vertical%20line.%0A%20%20%20%20%2F%2F%20Assumption%3A%20All%20x%20coordinates%20are%20distinct.%0A%20%20%20%20Point%20Pyl%5Bmid%2B1%5D%3B%20%20%20%2F%2F%20y%20sorted%20points%20on%20left%20of%20vertical%20line%0A%20%20%20%20Point%20Pyr%5Bn-mid-1%5D%3B%20%20%2F%2F%20y%20sorted%20points%20on%20right%20of%20vertical%20line%0A%20%20%20%20int%20li%20%3D%200%2C%20ri%20%3D%200%3B%20%20%2F%2F%20indexes%20of%20left%20and%20right%20subarrays%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20if%20(Py%5Bi%5D.x%20%3C%3D%20midPoint.x)%0A%20%20%20%20%20%20%20%20%20Pyl%5Bli%2B%2B%5D%20%3D%20Py%5Bi%5D%3B%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20Pyr%5Bri%2B%2B%5D%20%3D%20Py%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Consider%20the%20vertical%20line%20passing%20through%20the%20middle%20point%0A%20%20%20%20%2F%2F%20calculate%20the%20smallest%20distance%20dl%20on%20left%20of%20middle%20point%20and%0A%20%20%20%20%2F%2F%20dr%20on%20right%20side%0A%20%20%20%20float%20dl%20%3D%20closestUtil(Px%2C%20Pyl%2C%20mid)%3B%0A%20%20%20%20float%20dr%20%3D%20closestUtil(Px%20%2B%20mid%2C%20Pyr%2C%20n-mid)%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20smaller%20of%20two%20distances%0A%20%20%20%20float%20d%20%3D%20min(dl%2C%20dr)%3B%0A%20%0A%20%20%20%20%2F%2F%20Build%20an%20array%20strip%5B%5D%20that%20contains%20points%20close%20(closer%20than%20d)%0A%20%20%20%20%2F%2F%20to%20the%20line%20passing%20through%20the%20middle%20point%0A%20%20%20%20Point%20strip%5Bn%5D%3B%0A%20%20%20%20int%20j%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(abs(Py%5Bi%5D.x%20-%20midPoint.x)%20%3C%20d)%0A%20%20%20%20%20%20%20%20%20%20%20%20strip%5Bj%5D%20%3D%20Py%5Bi%5D%2C%20j%2B%2B%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20closest%20points%20in%20strip.%20%20Return%20the%20minimum%20of%20d%20and%20closest%0A%20%20%20%20%2F%2F%20distance%20is%20strip%5B%5D%0A%20%20%20%20return%20min(d%2C%20stripClosest(strip%2C%20j%2C%20d)%20)%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20functin%20that%20finds%20the%20smallest%20distance%0A%2F%2F%20This%20method%20mainly%20uses%20closestUtil()%0Afloat%20closest(Point%20P%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20Point%20Px%5Bn%5D%3B%0A%20%20%20%20Point%20Py%5Bn%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Px%5Bi%5D%20%3D%20P%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20Py%5Bi%5D%20%3D%20P%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20qsort(Px%2C%20n%2C%20sizeof(Point)%2C%20compareX)%3B%0A%20%20%20%20qsort(Py%2C%20n%2C%20sizeof(Point)%2C%20compareY)%3B%0A%20%0A%20%20%20%20%2F%2F%20Use%20recursive%20function%20closestUtil()%20to%20find%20the%20smallest%20distance%0A%20%20%20%20return%20closestUtil(Px%2C%20Py%2C%20n)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20Point%20P%5B%5D%20%3D%20%7B%7B2%2C%203%7D%2C%20%7B12%2C%2030%7D%2C%20%7B40%2C%2050%7D%2C%20%7B5%2C%201%7D%2C%20%7B12%2C%2010%7D%2C%20%7B3%2C%204%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(P)%20%2F%20sizeof(P%5B0%5D)%3B%0A%20%20%20%20cout%20%3C%3C%20%22The%20smallest%20distance%20is%20%22%20%3C%3C%20closest(P%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

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 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)

[ad type=”banner”]