Beauty of programming-2.11-find the nearest point

Source: Internet
Author: User

1. Brief Introduction

Specify the coordinates of N points on the plane to locate the two closest points.

2. Ideas
The brute force method is the complexity of C (n, 2) and N ^ 2.

The binary method, for example, first sorts all vertices according to the abscissa and recursively Splits them. Termination condition: returns the result when only one vertex or two vertices exist. If one vertex is null, the two vertices are returned. Recursive process: Calculate the closest point between n/2 points on the left, calculate the closest point between n/2 points on the right, and then calculate the closest point between the left point and the right point. Formula for complexity: T (n) = 2 T (n/2) + N ^ 2/4. According to the main theorem, T (n) = n ^ 2/4, or n ^ 2.

The binary method is further simplified, and the complexity is wasted on the combination (a point on the left and a point on the right. Suppose mindistleft = the distance from the nearest point on the left and mindistright = the distance from the nearest point on the right. The known distance from the nearest point can be represented as c = min {mindistleft, mindistright }, the last vertex coordinate on the left is (x1, Y1), and the coordinate of the first Vertex on the right is (X2, Y2), then the abscissa of the valid Vertex on the left must be in (x2-C, within the range of X2], the horizontal coordinates of valid points on the right must be in the range of [X1, X1 + C), because if the abscissa of a point is different from that of C, the distance, for example, is greater than C. After this is done, the number of points to be judged may be reduced a lot.

The second method is further simplified, and the previous step is simplified to control the points on the left and right sides in the range of (x2-C, x2) and [x1, x1 + C) respectively, in fact, x1 <= x2 is equivalent to a point between two vertical bars of 2 * C. The first step is to consider the abscissa. In this step, we should consider the ordinate coordinates. The vertices on the left and right are combined in a list and sorted by the ordinate, and each vertex is marked on the left or right. Then, traverse the list. For the current vertex, only the vertices from different directions after the vertex and whose ordinate difference is less than C are computed. If the distance exceeds C, the calculation of the current vertex stops. That is, each vertex takes into account a point in the C * C box in the diagonal direction. According to the beauty of programming, there are only four such points, if there are more than four (this should be the four feet of the box), then the values smaller than C can be obtained when the first recursion is on both sides, therefore, the number of times to traverse the list should be calculated at most 4 times. Of course, if you calculate the box on your own side, you can compare it up to 7 times (the three times are the points on your own side ).

After the calculation is complete, we assume that after range control, the number of vertices is not reduced, so the sorting is n * logn and the traversal list is n * 7. T (n) = 2 T (n/2) + n * logn + 7 * n. According to the main theorem, the complexity is n * logn + 7 * n, Which is n * logn level.

3. Code

I don't want to write it anymore. It's too short recently. I will try to write it well later. This is mainly because the test data is not very good.

4. scaling problems

The first one is to give an array and calculate the maximum value of the difference between adjacent values. The so-called adjacent value refers to the two values X and Y. Other numbers in the array are not in the range of [X, Y. Generally, it is sorting, and then the difference between the two values to update the maximum difference. The book uses the drawer principle + Barrel Method, first calculate the maximum and minimum values in the array, we can see that the maximum difference is greater than or equal to delta = (Max-Min)/(N-1), this is a good proof, inverse Method, if the maximum difference is less than delta, then N values, the difference between the N-1, the difference between the minimum and maximum is less than (delta * (N-1 )), that is, the difference between the minimum and maximum values is less than (Max-Min. Then, divide [Min, Max] into N portions, and place different values in the corresponding barrel, then, we only need to consider the difference between the last value of the first bucket and the first value of the last bucket.

For a value, you can map x to the (x-Min)/delta bucket. Because multiple values may exist in one bucket, it may be better to use an adjacent linked list. The minimum and maximum values are recorded in each linked list. In fact, when the difference between the maximum value and the minimum value is small, the Count sorting method can also be used, that is, to open the Max-Min + 1 length space, each value can be directly put in, calculate the difference between two numbers that are not null. However, the performance of counting sorting depends on the difference between the maximum and minimum values, and the value must be an integer.

The second question is to find the two points with the farthest distance for the given N points on the plane. I don't think much about this question. I think it's okay to use binary. I think it's okay to simplify it twice, because even if the horizontal or vertical differences between the two vertices are less than C, the distance between the two vertices will still exceed C. Search for the farthest point (1) In this blog post, the method seems to be first sorted by the polar angle, then the convex hull is obtained, and the method of rotating with a card shell can be more complex than n * logn, the jamming rotation does not seem to be understandable. It seems to be computational ry. Now, let's study it later.

5. Reference

The beauty of programming, section 2.11, looking for the latest point

Related Article

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.