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 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.

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

Following is a recap of 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 from 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 in above step 6.

The great thing about the above approach is, if the array strip[] is sorted according to y coordinate, then we can find the smallest distance in strip[] in O(n) time. In the implementation discussed in previous post, strip[] was explicitly sorted in every recursive call that made the time complexity O(n (Logn)^2), assuming that the sorting step takes O(nLogn) time.

In this post, we discuss an implementation where the time complexity is O(nLogn). The idea is to presort all points according to y coordinates. Let the sorted array be Py[]. When we make recursive calls, we need to divide points of Py[] also according to the vertical line. We can do that by simply processing every point and comparing its x coordinate with x coordinate of middle line.

本题是前一题的二分法的改进,前一个算法的效率是O(nlgnlgn)

前一遍二分法:http://blog.csdn.net/kenden23/article/details/20737523

为什么这个二分法是O(nlgnlgn)呢?除了使用公式计算之外,也可以这么考虑最坏情况,当距离中点的小于d距离,集中了所有的点,那么每次递归都需要对所有的点进行一次排序,效率是O(nlgn),那么每次二分都需要这么的效率,就要乘以做二分法的lgn效率,那么总效率就是O(nlgnlgn)。

呵呵,不是很正规的分析,不过我觉得这么分析更“聪明”,而且不会有错的。

那么为什么很多书上都说这个算法是O(nlgn)呢?因为大多书本都没有分析计算中间点最小距离的时候会出现最坏情况。

而Geeks这个网站考虑的非常仔细,这也是我也很推崇这个网站的原因之一。

本算法提高效率到O(nlgn),就是省去了中间需要重复排序的步骤。如果熟悉了前面的二分法算法,那么就很容易写出这个程序了。不明白的,先搞懂前面的算法,在来改进成这个算法。

时间: 2025-01-21 06:33:22

Geeks面试题: Closest Pair of Points及O(nlogn) Implementati的相关文章

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 c

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岁从芝加哥前往南加州开