Geeks interview question: closest Pair of Points

Source: Internet
Author: User

Divide and Conquer | Set 2 (closest Pair of Points)

We are given an array of n points in the plane, and the problem are to find out the closest pair of points in the. This is problem arises in a number of applications. For example, in Air-traffic control, and you'll want to monitor planes which come too close together, since this may indicate A possible collision. Recall the following formula for distance between two points P and Q.

The brute force solution is O (n^2), compute the distance between all pair and return the smallest. We can calculate the smallest distance in O (NLOGN) time using Divide and conquer strategy. In this post, the A O (n x (LOGN) ^2) approach is discussed. We are discussing a O (NLOGN) approach in a separate post.

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, the input array is sorted according to x coordinates.

1 Find the middle-sorted array, we can take P[N/2] as Middle point.

2) Divide the given array in two halves. The 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 is DL and Dr Find the minimum of DL and Dr. Let the minimum be d.

4) from above 3 steps, we have a upper bound d of minimum distance. Now we are need to consider the pairs such this one point in pair are from left half and other are 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 a array strip[] of all such points.

5) Sort the array strip[] according to Y coordinates. This 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 the seems to being a O (n^2) step, but it is actually O (n). It can be proved geometrically this for every point in the strip, we only need to check at most 7 points after it The trip are sorted according to Y coordinate). This is for more analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.