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.

Closest Pair of Points

The Brute force solution is O(n^2), compute the distance between each pair and return the smallest. We can calculate the smallest distance in O(nLogn) time using Divide and Conquer strategy. In this post, a O(n x (Logn)^2) approach is discussed. We will be discussing a O(nLogn) approach in a separate post.

[ad type=”banner”]

Algorithm

Following are the detailed steps of a O(n (Logn)^2) algortihm.
Input: An array of n points P[] Output: The smallest distance between two points in the given array.

As a pre-processing step, input array is sorted according to x coordinates.

1) Find the middle point in the sorted array, we can take P[n/2] as middle point.

2) Divide the given array in two halves. The first subarray contains points from P[0] to P[n/2]. The second subarray contains points from P[n/2+1] to P[n-1].

3) Recursively find the smallest distances in both subarrays. Let the distances be dl and dr. Find the minimum of dl and dr. Let the minimum be d.

Divide and Conquer

4) From above 3 steps, we have an upper bound d of minimum distance. Now we need to consider the pairs such that one point in pair is from left half and other is from right half. Consider the vertical line passing through passing through P[n/2] and find all points whose x coordinate is closer than d to the middle vertical line. Build an array strip[] of all such points.

[ad type=”banner”]

5) Sort the array strip[] according to y coordinates. This step is O(nLogn). It can be optimized to O(n) by recursively sorting and merging.

6) Find the smallest distance in strip[]. This is tricky. From first look, it seems to be a O(n^2) step, but it is actually O(n). It can be proved geometrically that for every point in strip, we only need to check at most 7 points after it (note that strip is sorted according to Y coordinate). See this for more analysis.

7) Finally return the minimum of d and distance calculated in above step (step 6)

Implementation

Following is C/C++ implementation of the above algorithm.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20divide%20and%20conquer%20program%20in%20C%2FC%2B%2B%20to%20find%20the%20smallest%20distance%20from%20a%0A%2F%2F%20given%20set%20of%20points.%0A%20%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cfloat.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Cmath.h%3E%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%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%20qsort(strip%2C%20size%2C%20sizeof(Point)%2C%20compareY)%3B%20%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%20P%20contains%0A%2F%2F%20all%20points%20sorted%20according%20to%20x%20coordinate%0Afloat%20closestUtil(Point%20P%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(P%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%20P%5Bmid%5D%3B%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(P%2C%20mid)%3B%0A%20%20%20%20float%20dr%20%3D%20closestUtil(P%20%2B%20mid%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(P%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%20P%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%20qsort(P%2C%20n%2C%20sizeof(Point)%2C%20compareX)%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(P%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%20printf(%22The%20smallest%20distance%20is%20%25f%20%22%2C%20closest(P%2C%20n))%3B%0A%20%20%20%20return%200%3B” message=”” highlight=”” provider=”manual”/]

Output:

The smallest distance is 1.414214
[ad type=”banner”]

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, sorts the strip in O(nLogn) time and 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(nLogn) + O(n)
T(n) = 2T(n/2) + O(nLogn)
T(n) = T(n x Logn x Logn)

 

Categorized in: