Solving the nearest two points in the plane n points by divide-and-conquer method

Source: Internet
Author: User
Tags min sort
Nearest point-to-problem definition: A collection of known m points, to find a pair of points close to each other. In two-dimensional space, the nearest point pair problem can be solved by the method of divide and conquer. Preprocessing: Sorting according to the x-axis and y-coordinate of the points, and getting x and Y, it is obvious that the points in X and Y are the points in S. Case (1): points less than or equal to three o'clock:

Situation (2): points greater than three o'clock: first dividing the set S for SL and SR, so that each point in SL is on the left side of each point in the SR, and the SL and SR midpoint numbers are the same. Solve the most recent point pair problem in SL and SR, and get the DL and Dr, respectively, representing the distance from the nearest point pair in SL and SR. Make D=min (DL,DR). If the nearest point pair (P1,P2) in S. P1, P2 2.1 in SL and one in Sr, then P1 and P2 must be in the gap centered on L, with l-d and l+d as the boundary, as shown in the following figure:

If the point P in SL and the point Q in the SR become the nearest point pair, then the distance between P and Q must be less than D. So for each point in the gap, in the merge step, only the points within the yp+d and yp-d need to be inspected. Step 1: Sort the points in s based on the Y-values and X-values of the points. Step 2: Find out the midline l divides s into SL and SR Step 3: Apply Step 2 recursion to the application to solve the nearest point pair problem of SL and SR, and make D=min (DL,DR).                 Step 4: Sort the points within the l-d~l+d with the Y-values, and for each point (x1,y1) Find all the points in the Y-value within the y1-d~y1+d, and calculate the distance to d '. If d ' is less than D, make D=d ', the last D value is the answer.

Experiment 1 recursive and divide-and-conquer algorithm

First, the purpose and requirements of the experiment

(1) Further mastering the design idea of recursive algorithm and the debugging technology of recursive program;

(2) Understand the idea that division and recursion are often applied simultaneously in algorithmic design.

(3) Using brute force method and separate treatment method to solve the recent problems;

(4) Analyze the time performance of the algorithm, and design the experimental procedure to verify the analysis conclusion.

Second, the experimental content

Set p1= (x1, y1), p2= (x2, y2), ..., pn= (xn, yn) is a set of n dots on a plane, and the design algorithm finds the nearest point pair in the set S.

Third, the experimental environment

Turbo C or VC + +

Four, the experimental hours

2 hours, must do the experiment

Five, data structure and algorithm

#include <iostream.h>

#include <cmath>

#define TRUE 1

#define FALSE 0



typedef struct NODE

{

Double X;

Double y;

}node; Coordinate



typedef struct LIST

{

node* data; Point

int count; Number of points

}list;



typedef struct CLOSENODE

{

Node A;

Node b; Two points for calculating distances

Double space; Distance squared

}closenode;



int n; Number of points



Enter each point into the list

void Create (List &l)

{

cout<< "Please enter the number of points on the plane: \ n";

cin>>n;

L.count=n;

L.data = new Node[l.count]; Dynamic Space allocation

cout<< "Input point coordinates: x_y):" <<endl;

for (int i=0;i<l.count;++i)

cin>>l.data[i].x>>l.data[i].y;

}



To find the square of the distance

Double Square (Node A,node b)

{

Return ((a.x-b.x) * (a.x-b.x)) + ((A.Y-B.Y) * (A.Y-B.Y));

}



Brute Force method

void Bruteforce (const List &l,closenode &cnode,int Begin,int end)

{

for (int i=begin;i<=end;++i)

{

for (int j=i+1;j<=end;++j)

{

Double Space=square (l.data[i],l.data[j]);

if (space<cnode.space)

{

Cnode.a=l.data[i];

CNODE.B=L.DATA[J];

Cnode.space=space;

}

}

}

}



Bubble sort

void Bubblesort (Node r[],int length)

{

int change,n;

N=length;change=true;

Double b,c;

for (int i=0;i<n-1&&change;++i)

{

Change=false;

for (int j=0;j<n-i-1;++j)

{

if (r[j].x>r[j+1].x)

{

B=R[J].X;C=R[J].Y;

R[J].X=R[J+1].X;R[J].Y=R[J+1].Y;

R[j+1].x=b;r[j+1].y=c;

Change=true;

}

}

}

}



The coordinates are arranged in the order of the x-axis from small to large in the divide-and-conquer method

void Paixu (List L)

{

Bubblesort (L.data,l.count); Call Bubble Sort

}



The nearest pair algorithm for the area of center d of the left and right margin

void Middle (const List & l,closenode &cnode,int mid,double midx)

{

int i,j; Indicates the left side of the midline, the right point

Double d=sqrt (cnode.space);

I=mid;

while (i>=0&&l.data[i].x>= (midx-d))//in the D area on the left

{

J=mid;

while (l.data[++j].x<= (midx+d) &&j<=l.count)//In the D area on the right

{

if (l.data[j].y< (l.data[i].y-d) | | L.data[j].y> (L.DATA[I].Y+D))//Determine if the ordinate is in the 2d area of a fixed point on the left

Continue

Double space = Square (l.data[i],l.data[j]);

if (cnode.space>space)//In order to meet the conditions of the area in turn to determine

{

Cnode.a=l.data[i];

CNODE.B=L.DATA[J];

Cnode.space=space;

}

}

I.;

}

}



Divide and conquer the law to seek the recent

void Divideconquer (const List &l,closenode &closenode,int Begin,int end)

{

if (begin!=end)

{

int mid = (begin+end)/2; The middle point after the arrangement.

Double midx = l.data[mid].x;

Divideconquer (L,closenode,begin,mid); Continue on the left half with the division method to find the most recent

Divideconquer (L,closenode,mid+1,end); Continue to use the right half of the method to find the most recent

Middle (L,CLOSENODE,MID,MIDX); Determine the area of center d of the left and right distance, whether there is a recent

}

}



void Main ()

{

Initialization

List List;

Closenode Closenode;
closenode.square=10000;//must pay the initial value, according to the actual situation

Closenode.space = 10000; Distance to Nearest point



Create (list); Enter the points into the nlist

cout<< "coordinates for each point:" <<endl;

for (int i=0;i<list.count;++i)

cout<< "x=" <<list.data[i].x<< "y=" <<list.data[i].y<< "\ n";

Bruteforce (list,closenode,0,list.count-1);

cout<< "Using brute force method to find the nearest pair:" <<endl;

cout<< "Recent pairs for points (" <<closenode.a.x<< "," <<closenode.a.y<< ") and points (" <<closenode.b.x << "," <<closenode.b.y<< ") \ n" << "Nearest Distance:" <<sqrt (closenode.space) <<endl;

cout<<endl<<endl;



cout<< "to seek the most recent by the Division of Law:" <<endl;

Paixu (list);

cout<< "sorted points:" <<endl;

for (int j=0;j<list.count;++j)

cout<< "x=" <<list.data[j].x<< "y=" <<list.data[j].y<< "\ n";

Divideconquer (list,closenode,0,list.count-1);

cout<< "Recent pairs for points (" <<closenode.a.x<< "," <<closenode.a.y<< ") and points (" <<closenode.b.x << "," <<closenode.b.y<< ") \ n" << "Nearest Distance:" <<sqrt (closenode.space) <<endl;

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.