Print closest point reprinted

Source: Internet
Author: User

There are two methods for finding the closest point in a vertex set:

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.

 

1. Brute Force Method (applicable when the number of points is small)

1) Algorithm Description: it is known that there are N points in the Set S, and a total of N (n-1)/2 point pairs can be formed. The brute force method is for this n (n-1) /2 calculate the distance between vertices on a pair-by-pair basis, and obtain the closest point in the vertex set cyclically:

2) Code Description:

Double mindistance = double. maxvalue; // you can specify a mindistance to store the distance from the nearest vertex. The initial value is infinite.

Int pointindex1, pointindex2; // set pointindex1 and pointindex2 to store the two vertex numbers of the closest vertex respectively.

For (I = 1; I <n; I ++) // calculate the distance of N (n-1)/2 to the point pair cyclically.
{

For (j = I + 1; j <= N; j ++)
{

Double pointdistance = distance (S [I], s [J]); // obtain the distance between point I and Point J.

If pointdistance <mindistance; // if the distance between the current point is smaller than the minimum point, the minimum point-to-point distance is equal to the distance between the current point-to-point.

{

Mindistance = pointdistance;

Pointindex1 = I;

Pointindex2 = J;

}

}

}

}
3) algorithm time complexity: The algorithm executes N (n-1)/2 cycles in total, so the algorithm complexity is O (n2)

 

2. Divide and conquer Law

1) Algorithm Description: it is known that there are N points in the Set S. The idea of the division and control method is to split S into two parts to find the closest point. The algorithm selects a vertical line l each time and splits s into SL and SR on the left and right. L is generally divided by the X coordinates of some intermediate points in the point set S, this ensures that the number of vertices in the SL and Sr is n/2,

(Otherwise, dividing s in other ways may result in one of the SL and SR point numbers being 1 and the other being n-1, which is not conducive to algorithm efficiency and should be balanced as much as possible)

Find the minimum point-to-point distance in the two parts in sequence: Delta l and Delta R, and remember the smallest point-to-point distance in SL and Sr. Delta = min (Delta l, Delta R), 1:

Take L as the center, and delta as the radius to divide a long band. The minimum point may also exist at the junction of SL and Sr, for example, the virtual line band in the left Graph 2, p and q are located in the dotted line range of SL and SR respectively. In this range, the distance between p and q is smaller than delta, and the minimum point is meaningful for calculation.

 

 

Figure 2

 

 

For the pvertex in the range of the SL virtual box, there are only six vertices at the top of the SR virtual box with a distance less than delta, that is, the six vertices of the two squares in Figure 2. It can be inferred that if the distance between the seven vertices on the right side of the two squares is less than delta, for example, QPS, the distance between the QPS and the four vertices of the following squares is less than delta, then, it is in conflict with the minimum point-to-distance between delta-SL and Sr. Therefore, for the P point in the SL dotted box, you do not need to find the distance between the P point and all the points in the right dotted box. You only need to calculate the six points closest to the Y coordinate of the P point in the SR, you can find the closest point, saving the number of comparisons.

(Otherwise, in the worst case, there may be n/2 points in the SR virtual box. For the P point in the SL virtual box, compare n/2 times each time, the algorithm efficiency is wasted)

Code Description:

1) sort the point X and Y coordinates of the Point Set S in ascending order to obtain the point set SX and Sy.

2) Let Delta = ∞; // Delta be the minimum point distance.

3) divide_conquer (sx, Sy, Delta) // divide and conquer Law

If (sx. Count = 1) then Delta = ∞; // if there is only one vertex in Sx, then Delta = ∞

Return delta;

Else if (sx. count = 2 and D (sx. [0], Sx. [1]) <delta) // if there are only two points in Sx, then Delta is the distance between two points.

Delta = D (sx. [0],) Sx. [1]);

Return delta;

Else // if there are more than two points in Sx, the SX and SY are divided into two parts, sxl and sxr, And Sy is divided into Syl and Syr.

J1 = 1, J2 = 1, k1 = 1, K2 = 1;

Mid = Sx. Count/2; // mid is the middle dot in SX

L = Sx. [Mid]. X; // L indicates the X coordinate of the center point in Sx.

For (I = 1, I <Sx. Count, I ++)

{

If (I <= mid) // store the SX median to sxl from the left vertex, and the new array retains the original ascending nature.

Sxl [k1] = SX [I] K1 ++;

Else // store the point at the right of the SX median to sxr. The new array retains the original ascending order.

Sxr. Count [k2] = SX [I] K2 ++;

If (SY [I]. x <L) // store the SY center line to Syl at the left point. The new array retains the original ascending nature.

Syl [J1] = SX [I] J1 ++;

Else // store the SY median to Syr at the right point, and the new array maintains the original ascending order.

SYR [J2] = SX [I] J2 ++;

}

Delta l = divide_conquer (sxl, Syl, Delta); // obtain the minimum point distance of SX.

Delta r = divide_conquer (sxr, Syr, Delta); // obtain the minimum point distance from Delta R in SY.

Delta = min (Delta l, Delta R );

Delta = Merge (Syl, Syr, Delta); // obtain the minimum point distance at the SY junction in Sx, and obtain the minimum point distance between the Delta l and Delta R.

Return delta;

 

Function Merge (Syl, Syr, Delta)

Merge (Syl, Syr, Delta)

{

I1 = 1, I2 = 1;

For (I = 1, I <Syl. count, I ++) // obtain the points in the left dotted box (distance less than delta) in Syl and store them in S 'yl. The new array retains the original ascending nature.

{

If (Syl [I]. x> L-delta)

Then s 'yl [I1] = Syl [I], i1 ++,

}

For (I = 1, I <Syr. count, I ++) // obtain the points in the right dotted box (distance less than delta) in Syr and store them in S's yr. The new array retains the original ascending nature.

{

If (SYR [I]. x <L + delta)

Then s 'yr [I2] = Syr [I], I2 ++,

}

 

T = 1;

For (I = 1, I <s 'yl. Count, I ++)

{

While (s 'yr [T]. Y <s 'yl [T]. Y and T <Syr. count) // obtain the point number closest to the point 'yl [T] Y coordinate of the Point Set S 'yr.

{T ++ ;}

For (j = max (1, T-3), j <= min (t + 3, S 'yr. count), J ++) // calculate the distance between the points in S 'yr and the six adjacent points in S 'yl [T] Y coordinate.

{

If (D (s 'yl [I], s 'yl [J]) <delta) // if the distance between the first two points is less than Delta

Then Delta = D (s 'yl [I], s 'yl [J]); // the minimum distance between the Delta and the current two points

}

Return Delta

}

 

3) algorithm time complexity:

First, the point X and Y coordinates of the Point Set S are sorted in ascending order. 2nlogn cycles are required, and the complexity is O (2 nlogn)

Next, in the sub-governance process, the points in each s 'yl must be compared with the six points in S 'yr.

O (n) = 2O (n/2) + (n/2) * 6 (a point set is divided into two left and right points. The time complexity is the sum of the two left and right points plus the calculation of the intermediate area)

The solution is O (n) <O (3 nlogn)

Therefore, the total time complexity is O (3 nlogn), which is more efficient than O (n2) in the brute force method.

 

 

 

The basic knowledge of divide and conquer law can refer to http://blog.csdn.net/junerfsoft/archive/2008/09/25/2975495.aspx

For an improved algorithm, see "an improved algorithm for finding the closest point of a point set in a plane"

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.