分治算法(Divide-and-Conquer Algorithm)二

用分治算法解决“最近点对”(Closest Pair of Points)问题

在平面上有一些点,现在的任务是找出一对(俩个点)点,使这两个点的距离是所有点对中最小的。在解决实际问题的时候这个问题经常被用到,比如空中交通管制,你或许想找到连个飞机是不是离得太近,以免发生碰撞。用p和q代表两个点(想象成飞机也行)。


上面的式子就是计算p和q之间的距离的。下标x代表水平坐标,小标y代表竖直坐标。

我们当然可以暴力破解求出所有点之间的距离,比较值得出结果。可是这样复杂度为O(n^2)。惊人。我们可以用分支策略,使时间复杂度降到O(nlogn)。

下面设计一个分治算法.

输入:n个点组成的数组P

输出:数组元素之间距离的最小值

建设元素已经按x左边排好序了。

1)找到中间那个元素P[n/2]那个点作为中间点

2)数组按中见到分为左边和右边两个子数组,数组元素分别是P[0]到P[n/2]和P[n/2+1]到P[n-1]。

3)递归求解两个子数组的最小值dl和dr。去两者最小值为d。


4)还应考虑到一种情况是最小距离的两个点跨过中间线,也就是说这对点一个在左边数组中,另外一个在右边数组中。我们在中间点的x坐标处画一条竖直线,将所有x坐标到竖直线的距离小于d的点“收集"起来,用他们组成一个strip[]数组。如下图:


5)将strip[]数组按y坐标排序。一般的排序使用的是qsort,快速排序。时间复杂度是nlogn。

6)找出strip[]数组元素间距离最小值。这里我们用两个循环(循环嵌套)暴力破解的办法求解。这种方法求解strip[]数组,乍看起来时间复杂度是O(n^2),其实只有O(n)。因为内循环最多只会检查7个点。详细解释看这里

7)比较6中求出的最小值和d的值,返回最小值。

#include <stdio.h>
#include <float.h>
#include <stdlib.h>
#include <math.h>
 
struct Point
{
    int x, y;
};
 
// 按x坐标排序
int compareX(const void* a, const void* b)
{
    Point *p1 = (Point *)a,  *p2 = (Point *)b;
    return (p1->x - p2->x);
}
// 按y坐标排序
int compareY(const void* a, const void* b)
{
    Point *p1 = (Point *)a,   *p2 = (Point *)b;
    return (p1->y - p2->y);
}
 
float dist(Point p1, Point p2)
{
    return sqrt( (p1.x - p2.x)*(p1.x - p2.x) +
                 (p1.y - p2.y)*(p1.y - p2.y)
               );
}
 

float bruteForce(Point P[], int n)
{
    float min = FLT_MAX;
    for (int i = 0; i < n; ++i)
        for (int j = i+1; j < n; ++j)
            if (dist(P[i], P[j]) < min)
                min = dist(P[i], P[j]);
    return min;
} 

float min(float x, float y)
{
    return (x < y)? x : y;
}

float stripClosest(Point strip[], int size, float d)
{
    float min = d;  // Initialize the minimum distance as d
 
    qsort(strip, size, sizeof(Point), compareY); 
    for (int i = 0; i < size; ++i)
        for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j)
            if (dist(strip[i],strip[j]) < min)
                min = dist(strip[i], strip[j]);
 
    return min;
}
 
float closestUtil(Point P[], int n)
{
    // If there are 2 or 3 points, then use brute force
    if (n <= 3)
        return bruteForce(P, n);
 
    // Find the middle point
    int mid = n/2;
    Point midPoint = P[mid];
 
    // Consider the vertical line passing through the middle point
    // calculate the smallest distance dl on left of middle point and
    // dr on right side
    float dl = closestUtil(P, mid);
    float dr = closestUtil(P + mid, n-mid);
 
    // Find the smaller of two distances
    float d = min(dl, dr);
 
    // Build an array strip[] that contains points close (closer than d)
    // to the line passing through the middle point
    Point strip[n];
    int j = 0;
    for (int i = 0; i < n; i++)
        if (abs(P[i].x - midPoint.x) < d)
            strip[j] = P[i], j++;
 
    // Find the closest points in strip.  Return the minimum of d and closest
    // distance is strip[]
    return min(d, stripClosest(strip, j, d) );
}
 
// The main functin that finds the smallest distance
// This method mainly uses closestUtil()
float closest(Point P[], int n)
{
    qsort(P, n, sizeof(Point), compareX);
 
    // Use recursive function closestUtil() to find the smallest distance
    return closestUtil(P, n);
}
 
// Driver program to test above functions
int main()
{
    Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
    int n = sizeof(P) / sizeof(P[0]);
    printf("The smallest distance is %f ", closest(P, n));
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值