您的位置:首页 > 职场人生

Geeks面试题: Closest Pair of Points

2014-03-08 08:23 393 查看


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.

7) Finally return the minimum of d and distance calculated in above step (step 6)
http://www.geeksforgeeks.org/closest-pair-of-points/
好困难的一道题目,虽然方法是二分法,但是其中也牵涉到暴力法,其中的函数qsort的使用也值得学习学习。

其中最难的地方:

就是上面说的第6步。其中的证明可以上网找找资料,其实也不是很难。

会了证明之后,下面程序的实现函数是midClosest。要想到这样去实现这个函数也是挺困难的。

这里的程序是在原Geeks上的程序修改而来的,主要是改了squDist函数,直接返回平方值就可以了,最后再使用sqrt,返回距离:

struct Point
{
    int x, y;
};

int xCompare(const void *a, const void *b)
{
	const Point *p1 = (const Point*)a;
	const Point *p2 = (const Point*)b;
	return p1->x - p2->x;
}
int yCompare(const void *a, const void *b)
{
	const Point *p1 = (const Point*)a;
	const Point *p2 = (const Point*)b;
	return p1->y - p2->y;
}
float squDist(Point &a, Point &b)
{
	return float(a.x - b.x)*(a.x - b.x) + float(a.y - b.y)*(a.y - b.y);
}

float bruteSolve(Point P[], int n)
{
	float clo_dist = FLT_MAX;
	for (int i = 0; i < n; i++)
	{
		for (int j = i+1; j < n; j++)
		{
			float t = squDist(P[i], P[j]);
			if (clo_dist > t)  clo_dist = t;
		}
	}
	return clo_dist;
}
#include<stdlib.h>
#include<math.h>
float midClosest(Point P[], int n, float d)
{
	qsort(P, n, sizeof(Point), yCompare);
	for (int i = 0; i < n; i++)
	{
		for (int j = i+1; j < n && P[j].y - P[i].y < d; j++)
		{
			float t = squDist(P[j], P[i]);
			if (t < d) d = t;
		}
	}
	return d;
}

float squClosest(Point P[], int n)
{
	if (n <= 3) return bruteSolve(P, n);
	int m = n>>1;;

	float ld = squClosest(P, m);
	float rd = squClosest(P+m, n-m);
	float d = ld < rd? ld:rd;

	Point *strip = new Point
;
	int j = 0;
	for (int i = 0; i < n; i++)
		if (abs(P[i].x - P[m].x) < d) strip[j++] = P[i];

	float t = midClosest(strip, j, d);
	return d < t? d:t;
}

float closest(Point P[], int n)
{
	qsort(P, n, sizeof(Point), xCompare);
	return sqrt(squClosest(P, n));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: