Recent point-to-point problem definition: a set of M points is known to locate a pair of close points.
In two-dimensional space, you can use the divide and conquer method to solve the closest point problem. Pre-processing: sort the values based on the X axis and Y axis respectively to obtain X and Y. Obviously, the points in X and Y are the points in S.
Case (1): When the number of points is less than or equal to three hours:
Case (2): When the number of points is greater than three:
First, the set S is divided into SL and Sr, so that each vertex in Sl is located on the left of each vertex in SR, and the number of vertices in SL and Sr are the same. Solve the closest point problem in SL and SR, and obtain DL and Dr, respectively, representing the distance between the closest point in SL and Sr. Make d = min (DL, Dr ). If the nearest point in S is (P1, P2 ). P1 and P2 are 2.1 in SL and one in Sr, so P1 and P2 must be bounded by L-D and L + D in the gap centered on L, as shown in:
If the point P in SL and the point Q in SR become the closest point, the distance between p and q must be less than D. Therefore, for each point in the gap, in the merge step, you only need to check the points in YP + D and YP-D.
Step 1: sort the values of Y and X in S.
Step 2: locate the midline L and divide s into SL and SR
Step 3: Apply Step 2 recursively to solve the closest point of SL and SR, and make d = min (DL, Dr ).
Step 4: Convert l-D ~ The points in L + D are sorted by y values. For each point (x1, Y1), the Y value is found in the y1-d ~ All vertices in Y1 + d, and the calculated distance is D '. If D is less than D, make d = D. The last D value is the answer.
Experiment 1 recursion and grouping Algorithms
I. Purpose and requirements of the experiment
(1) further master the design idea of recursive algorithms and the debugging technology of recursive Programs;
(2) understand the idea that grouping and Recursion are often applied to algorithm design at the same time.
(3) Use the brute force method and divide and conquer method to solve recent problems respectively;
(4) analyze the time performance of the algorithm and design the experiment program to verify the analysis conclusion.
Ii. experiment content
Set p1 = (x1, Y1), P2 = (X2, Y2 ),..., Pn = (Xn, yn) is a set of S composed of N points on the plane. It is designed to find the closest point in S.
Iii. Lab Environment
Turbo C or VC ++
Iv. lab hours
2 hours, lab required
V. data structures and algorithms
# Include <iostream. h>
# Include <cmath>
# Define true 1
# Define false 0
Typedef struct Node
{
Double X;
Double Y;
} Node; // coordinates
Typedef struct list
{
Node * data; // Vertex
Int count; // number of points
} List;
Typedef struct closenode
{
Node;
Node B; // calculate two points of distance
Double space; // Distance Square
} Closenode;
Int N; // number of points
// Input each point to the list
Void create (List & L)
{
Cout <"Enter the number of points on the plane: \ n ";
Cin> N;
L. Count = N;
L. Data = new node [L. Count]; // dynamic space allocation
Cout <"coordinate of Input Points: x_y):" <Endl;
For (INT I = 0; I <L. Count; ++ I)
Cin> L. Data [I]. x> L. Data [I]. Y;
}
// Calculates 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
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;
}
}
}
}
// Sort the coordinates in ascending order of the X axis in the Division and control method.
Void paixu (list l)
{
Bubblesort (L. Data, L. Count); // call the bubble sort
}
// The nearest Algorithm for the left and right vertices from the midline d
Void middle (const list & L, closenode & cNode, int mid, double midx)
{
Int I, j; // The left and right vertices of the midline, respectively.
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) // determines whether 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) // judge in sequence within the region where the conditions are met
{
CNode. A = L. Data [I];
CNode. B = L. Data [J];
CNode. Space = space;
}
}
-- I;
}
}
// Method of separation and Control
Void divideconquer (const list & L, closenode & closenode, int begin, int end)
{
If (begin! = End)
{
Int mid = (begin + end)/2; // the point in the middle of the arrangement
Double midx = L. Data [Mid]. X;
Divideconquer (L, closenode, begin, mid); // continue to use the division and Control Method on the left half to find the latest pair
Divideconquer (L, closenode, Mid + 1, end); // continue to use the division and Control Method on the right half side to find the latest pair
Middle (L, closenode, mid, midx); // determines whether there is a closest pair
}
}
Void main ()
{
// Initialization
List list;
Closenode;
Closenode. square = 10000; // The initial value must be paid, depending on the actual situation
Closenode. Space = 10000; // distance from the nearest Vertex
Create (list); // enter each point to NLIST
Cout <"coordinate of 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 <"Calculate the nearest pair using the brute force method:" <Endl;
Cout <"Recent pair is a vertex (" <closenode. a. x <"," <closenode. a. Y <") and point (" <closenode. b. x <"," <closenode. b. Y <") \ n" <"closest:" <SQRT (closenode. space) <Endl;
Cout <Endl;
Cout <"Calculate the nearest pair using the division and Control 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 pair is a vertex (" <closenode. a. x <"," <closenode. a. Y <") and point (" <closenode. b. x <"," <closenode. b. Y <") \ n" <"closest:" <SQRT (closenode. space) <Endl;
}