Geeks面试题:Closest Pair of Points

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

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) 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). See this for more analysis.

时间: 2024-10-28 16:52:36

Geeks面试题:Closest Pair of Points的相关文章

Geeks面试题: Closest Pair of Points及O(nlogn) Implementati

Closest Pair of Points | O(nlogn) Implementation 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 cont

UVa 10245:The Closest Pair Problem

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1186 [原题] Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

Geeks 面试题之Fibonacci numbers优化为lgn效率

The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, --.. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation with seed values Write a fun

Geeks面试题:Box Stacking Problem

Dynamic Programming | Set 22 (Box Stacking Problem) You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as pos

Geeks 面试题之Maximum size square sub-matrix with all 1s

Maximum size square sub-matrix with all 1s Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The maximum square s

Geeks 面试题之Ugly Numbers

Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, - shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 150′th ugly number.

Geeks 面试题之Optimal Binary Search Tree

Dynamic Programming | Set 24 (Optimal Binary Search Tree) Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys

uva 10245 - The Closest Pair Problem

点击打开链接uva 10245 题目意思:   给定N个点,找到所有点中距离最小的 解题思路: 1:递归+分治 <网上大牛的解释如下> 2在二维空间里,可用分治法求解最近点对问题.预处理:分别根据点的x轴和y轴坐标进行排序,得到X和Y,很显然此时X和Y中的点就是S中的点.            1情况(1):点数小于等于三时:                                  2情况(2):点数大于三时:            3首先划分集合S为SL和SR,使得SL中的每一个点

坚持编程:如何找到一份工程师工作

说实话,你是一位优秀的工程师候选人吗?你怎么评价自己?你面试过多少家公司?你拿到offer的比率是多少?试一下用以下的公式来计算. x = number of companies interviewed with onsite   y = number of offers received     value = 100 * log(x) * y / x  如果你的计算结果小于90,请仔细阅读这篇文章:如果大于120,那你并不需要这篇文章. 我是谁? 我没有高中学历.我19岁从芝加哥前往南加州开