C++分治法--最近对问题

最近对问题:
最近对问题要求在一个包含n个点的集合中找出距离最近的两个点。严格地讲,距离最近的点可能多于一个,简单起见,只找出其中的一对即可。
最近对问题的分治策略如下:
(1)划分:将集合S分成两个子集S1和S2,根据平衡子问题原则,每个子集中大约有n/2个点,设集合S的最近点对是 p i \displaystyle p_i pi p j \displaystyle p_j pj(1 ≤ \displaystyle\leq i,j ≤ \displaystyle\leq n),则会出现以下三种情况:
1. p i ∈ S 1 \displaystyle p_i\in S1 piS1 p j ∈ S 1 \displaystyle p_j\in S1 pjS1,即最近点对均在集合S1中;
2. p i ∈ S 2 \displaystyle p_i\in S2 piS2 p j ∈ S 2 \displaystyle p_j\in S2 pjS2,即最近点对均在集合S2中;
3. p i ∈ S 1 \displaystyle p_i\in S1 piS1 p j ∈ S 2 \displaystyle p_j\in S2 pjS2,即最近点对均在集合S1中;
(2)求解子问题,对于划分阶段的情况1和情况2可递归求解,如果最近点对分别在S1和S2中,问题就有些复杂了。
(3)合并:比较在划分阶段三种情况下的最近点对,取三者中的距离较小者为原问题的解。
C++实现:

#include<iostream>
#include<cmath>
using namespace std;

struct point
{
    int x;
    int y;
};

int Partition(point S[],int first,int end)
{
    int i=first,j=end;
    while(i<j)
    {
        while(i<j&&S[i].y<=S[j].y) j--;
        if(i<j)
        {
            point temp;
            temp=S[i];
            S[i]=S[j];
            S[j]=temp;
            i++;
        }
        while(i<j&&S[i].y<=S[j].y) i++;
        if(i<j)
        {
            point temp;
            temp=S[i];
            S[i]=S[j];
            S[j]=temp;
            j--;
        }
    }
    return i;
}

void QuickSort(point S[],int first,int end)
{
    int pivot;
    if(first<end)
    {
        pivot=Partition(S,first,end);
        QuickSort(S,first,pivot-1);
        QuickSort(S,pivot+1,end);
    }
}

double Distance(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double Closest(point S[],int low,int high)
{
    double d1,d2,d3,d;
    int mid,i,j,index;
    point P[1000];                                   //存放集合P1和P2
    if(high-low==1)                              //只有两个点,返回两点间的距离
        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))
            return d1;
        else if(d2<d3)
            return d2;
        else
            return d3;
    }
    mid=(low+high)/2;
    d1=Closest(S,low,mid);                         //递归求解子问题1
    d2=Closest(S,mid+1,high);                      //递归求解子问题2
    if(d1<=d2) d=d1;                               //求解子问题3,最近点对在两个集合中
    else d=d2;
    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
        QuickSort(P,0,index-1);                     //对集合P1,P2按y坐标升序排列
    for(i=0;i<index;i++)                            //依次处理集合P1,P2中的点
    {
        for(j=i+1;j<index;j++)
        {
            if(P[i].y-P[i].y>=d)                    //超出y的范围,点P[i]处理完毕
                break;
            else
            {
                d3=Distance(P[i],P[j]);
                if(d3<d)
                    d=d3;
            }
        }
    }
    return d;
}



int main()
{
    struct point S[1000];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>S[i].x>>S[i].y;
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-i-1;j++)
            if(S[j].x>S[j+1].x)
            {
                point temp=S[j+1];
                S[j+1]=S[j];
                S[j]=temp;
            }
    }
    cout<<Closest(S,0,n-1);
}
  • 4
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
最近问题是指在平面上给定n个,找出其中距离最近的两个的距离。分治法最近问题的经典算法之一。 1. 将所有按照横坐标排序,然后分成左右两个子问题; 2. 对于左右两个子问题,分别递归最近对; 3. 在左右两个子问题最近对中,选取距离更近的一对作为候选解; 4. 计算横跨左右两个子问题对的距离,并将它们按照纵坐标排序; 5. 在纵向排序后的集中,依次计算每个与后面最多7个之间的距离,并选取距离更近的一对作为候选解; 6. 在步骤3和步骤5中选取距离更近的一对作为最终解。 下面是C++代码实现: #include <iostream> #include <algorithm> #include <cmath> #include <vector> using namespace std; struct Point { int x; int y; }; bool cmp_x(Point a, Point b) { return a.x < b.x; } bool cmp_y(Point a, Point b) { return a.y < b.y; } double dist(Point a, Point b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } double brute_force(vector<Point>& points, int lo, int hi) { double min_dist = 1e9; for (int i = lo; i <= hi; i++) { for (int j = i + 1; j <= hi; j++) { min_dist = min(min_dist, dist(points[i], points[j])); } } return min_dist; } double closest_pair(vector<Point>& points, int lo, int hi) { if (lo == hi) { return 1e9; } if (hi - lo == 1) { return dist(points[lo], points[hi]); } int mid = (lo + hi) / 2; double d1 = closest_pair(points, lo, mid); double d2 = closest_pair(points, mid + 1, hi); double d = min(d1, d2); vector<Point> strip; for (int i = lo; i <= hi; i++) { if (abs(points[i].x - points[mid].x) < d) { strip.push_back(points[i]); } } sort(strip.begin(), strip.end(), cmp_y); int n = strip.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n && strip[j].y - strip[i].y < d; j++) { d = min(d, dist(strip[i], strip[j])); } } return d; } int main() { int n; cin >> n; vector<Point> points(n); for (int i = 0; i < n; i++) { cin >> points[i].x >> points[i].y; } sort(points.begin(), points.end(), cmp_x); double ans = closest_pair(points, 0, n - 1); if (ans < 1e9) { cout << ans << endl; } else { cout << "No solution" << endl; } return 0; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值