Find two points with the Closest Euclidean distance.

Please help me to solve this :
S is a Set of n points on a 2D plane , find two points with the closest Euclidean distance?

1 Like

have a look at line sweep algorithm. it might help. for details go to this link

@rashedcs A naive solution to find closest pair would be iterate through all points and for every point find its distances from every other point, but this will be an O(n$^2$) approach and not useful but it can be optimized to run in O(n$logn$) by using divide and conquer strategy. You can refer to this link for deatiled description of this optimized approach.

Here’s a (Python) code.I hope the approach is easy to understand.

import math
n=int(input('Enter the no.of points: '))
l=[];d=0;ll=[0];s=[]
for i in range(n):
    x=int(input('Enter x co-ordinate '))
    y=int(input('Enter y co-ordinate '))
    tp=(x,y) #The tuple containing the co-ordinates
    l.append(tp)
for t in l:
    for r in l:
        if t!=r:
            d=math.hypot((r[0]-t[0]),(r[1]-t[1]))
            ll.append(d);
            ll.sort()
            if d<ll[1]:
                s=[t,r]    #solution list
print('The points with the shortest distance between them are: ',s[0],s[1])

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.
  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.
  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).
  7. Finally return the minimum of d and distance calculated in above step (step 6).
    //source:-geeks for geeks
//