分治法求解最近点对问题

1. 问题

有多个点在笛卡尔平面的x、y轴上都是升序排列。
设S是平面上n个点的集合,我们考虑在S中找到一个点对p和q的问题,使其相互距离最短。

2. 解析

采用分治策略,设n为点的个数,d为最近距离,分为两种情况。(题目中n>1)
第一种:2<=n<=3时,直接求出每个点之间的距离,求最短即可
第二种:n>3时,将S分为平均的左右两个部分S1、S2,分界线为L,这两个点可能都在S1中,也有可能都在S2中,还有可能一个在S1中,另一个在S2中。分别求出S1、S2中的最近点对d1、d2(采用递归),d=min(d1,d2),如果最近对处于第三种情形,那么他们的距离一定小于d,所以对于L-d到L+d进行查询,所在区域内的每个点和其他的五个点(在此区域内之可能存在最多6个点,至于为什么,已经被证明拿来当已知用)进行距离计算,再与d比较,求出最小距离。

3. 设计

//分治法求解最近点对问题
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct point {           //点结构
    double x, y;
};
double Distance(point a, point b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool cmp(point a, point b) {          //按y升排序辅助函数
    return a.y < b.y;
}
bool cmp2(point a, point b) {          //按x升排序辅助函数
    return a.x < b.x;
}
double closestPoint(point s[], int low, int high, point rec[]) {
    double d1, d2, d3, d;
    int mid, i, j, index;
    double x1, y1, x2, y2;         //记录点对的位置
    point P[high - low + 1], temp1[2], temp2[2], temp3[2];         //辅助空间
    if (high - low == 1) {             //两个点的情况
        rec[0].x = s[low].x; rec[0].y = s[low].y;
        rec[1].x = s[high].x; rec[1].y = s[high].y;
        return Distance(s[low], s[high]);
    }
    if (high - low == 2) {            //三个点的情况
        d1 = Distance(s[low], s[low + 1]);
        d2 = Distance(s[low + 1], s[high]);
        d3 = Distance(s[low], s[high]);
        if ((d1 < d2) && (d1 < d3)) {
            rec[0].x = s[low].x; rec[0].y = s[low].y;
            rec[1].x = s[low + 1].x; rec[1].y = s[low + 1].y;
            return d1;
        }
        else if (d2 < d3) {
            rec[0].x = s[low + 1].x; rec[0].y = s[low + 1].y;
            rec[1].x = s[high].x; rec[1].y = s[high].y;
            return d2;
        }
        else {
            rec[0].x = s[low].x; rec[0].y = s[low].y;
            rec[1].x = s[high].x; rec[1].y = s[high].y;
            return d3;
        }
    }
    mid = (low + high) / 2;       //其他情况递归
    d1 = closestPoint(s, low, mid, rec);
    temp1[0] = rec[0];
    temp1[1] = rec[1];
    d2 = closestPoint(s, mid + 1, high, rec);
    temp2[0] = rec[0];
    temp2[1] = rec[1];
    if (d1 < d2) {
        d = d1;
        rec[0] = temp1[0];
        rec[1] = temp1[1];
    }
    else {
        d = d2;
        rec[0] = temp2[0];
        rec[1] = temp2[1];
    }
    index = 0;
    for (i = mid; (i >= low) && ((s[mid].x - s[i].x) < d); i--)      //点集合p1
        P[index++] = s[i];
    for (i = mid + 1; (i <= high) && ((s[i].x - s[mid].x) < d); i++)      //点集合p2
        P[index++] = s[i];
    sort(P, P + index, cmp);                    //升序排列
    for (i = 0; i < index; i++) {
        for (j = j + 1; j < index; i++) {
            if ((P[j].y - P[i].y) >= d)
                break;
            else {
                d3 = Distance(P[i], P[j]);
                if (d3 < d) {
                    rec[0].x = P[i].x; rec[0].y = P[i].y;
                    rec[1].x = P[j].x; rec[1].y = P[j].y;
                    d = d3;
                }
            }
        }
    }
    return d;
}
int main() {
    point p[10];            //设定点的集合
    int n;
    double minDist;
    cout << "输入点的个数:\n";      //输入点的个数
    cin >> n;
    cout << "输入点集:(x,y)\n";
    for (int i = 0; i < n; i++)
        cin >> p[i].x >> p[i].y;
    sort(p, p + n, cmp2);      //对输入的点先排序
    point index[2];
    minDist = closestPoint(p, 0, n - 1, index);
    cout << "最小距离点对为:(" << index[0].x << "," << index[0].y << "),(" << index[1].x << "," << index[1].y << ")\n";
    cout << "最小距离为:\n" << minDist;      //输出点对的最小问题
    return 0;
}

4. 分析

时间复杂度:
经过预排序处理后的算法所需的计算时间T(n)满足递归方程:当n小于4时,T(n)=O(1);当n大于等于4时,T(n)=2T(n/2)+O(n)。由此易知,T(n)=O(nlogn)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值