分治法找最近点对,可以把时间复杂度从暴力的O(n^2)降低到O(nlogn)
原理就是把图中的所有点分为左右两半,接着递归再二分左右两半。直到区间内只有两个点,易得两点间长度。
之后是合并的过程,从左右区间中取出更小的那一个。接着,比较中间区域strip内的点对,看看有没有更短的点对。(这个过程经过证明最多只要比较7次!)
最后得到最小的那个长度。
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.
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.
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) <