Geeks interview questions: closest Pair of points and O (NLOGN) Implementati

Source: Internet
Author: User
Tags sort

Closest Pair of Points | O (NLOGN) implementation

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.

We have discussed a divide and conquer solution for this problem. The time complexity of the "implementation provided in" previous post is O (n (logn) ^2). In this post, we discuss a implementation with time complexity as O (NLOGN).

Following is a recap to 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 to 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 at above step 6.

The great thing about the above approach is, if the array strip[] are sorted according to Y coordinate, then we can find th e smallest distance in strip[] in O (n) time. In the implementation discussed in previous post, strip[] is explicitly sorted in every recursive call this made the time Complexity O (n (logn) ^2), assuming that sorting step takes O (NLOGN) time.

In this post, we discuss a implementation where the time complexity is O (NLOGN). The idea are to presort all points according to Y coordinates. Let the sorted array is py[]. When we do recursive calls, we need to divide points of py[] also according to the vertical line. We can do so by simply processing every point and comparing its X coordinate with x coordinate of middle.

The first question is the improvement of the binary method, the efficiency of the previous algorithm is O (NLGNLGN)

The first time the second Division method: http://blog.csdn.net/kenden23/article/details/20737523

Why is this binary O (NLGNLGN)? In addition to using formula calculations, you can consider the worst-case scenario, when the midpoint of the distance is less than d distance, and all the points are concentrated, then each recursion requires a sort of all the points, the efficiency is O (NLGN), then each of the two points need to be so efficient, multiplied by the LGN efficiency of the Second Division, Then total efficiency is O (NLGNLGN).

Oh, not very formal analysis, but I think this analysis is more "smart", and will not be wrong.

So why do so many books say that this algorithm is O (NLGN)? Because most books do not analyze the minimum distance from the middle point will appear the worst case.

and Geeks this website to consider very carefully, this is also I very highly esteem this website one of the reasons.

This algorithm improves efficiency to O (NLGN), which eliminates the need for repetitive sorting in the middle. If you are familiar with the previous binary algorithm, then it is easy to write this program. Do not understand, first understand the previous algorithm, in order to improve the algorithm.

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.