Calculate geometry-Nearest point pair

Source: Internet
Author: User
Tags comparison min

On a two-dimensional plane, there are n points, which are the closest pair of points to a distance:

1. Violence: The n points are sorted first by the horizontal axis, and then from the first point, then compare all the points above its horizontal axis, find the minimum distance d, and then start from the second point, in turn, compare all the points above the horizontal axis of the distance, has been compared to the penultimate point. Time complexity O (n^2)


2. Division: After the n points are sorted, the size of the horizontal axis is divided into left subset and right subset, find the shortest distance between left subset and right subset Dl,dr, take D=min (DL,DR), then compare the distance between the larger point of the horizontal axis of the left sub-set and the smaller point of the right sub-set. If there is a point pair less than D, the D value is updated. Time complexity O (NLGN)


HDU 1007

First of all, test instructions, very simple, to the coordinates of n points, the distance between the nearest pair of points is half the distance.

The first line is a number n means there are n points, and the next n rows are the X-and y-coordinates of n points. Real.


The question is actually the distance from the nearest point. "Introduction to the algorithm" is explained in detail, Wang Xiaodong's book also has code. The main idea is divide and conquer. First the n points in the x-coordinate order, and then the left N/2 and the right n/2 the nearest distance, and finally merged.

To focus on the merger is more troublesome.


First, the false SetPoint is n, numbered 1 to N. We want to divide and conquer, then find a middle number m, first of all 1 to M point of the nearest distance is set to D1, and m+1 to n the nearest distance is set to D2. The points here need to be sorted in the order of the x-coordinates, and assume that there is no 2 o'clock in the same position in these points. (if any, the direct minimum distance is 0).


Then, make d the D1, the smaller point in the D2. If two points in the nearest point pair are in the 1-m collection, or m+1 to the N collection, then D is the minimum distance. But it is also possible that the nearest point pair of two points belong to these two sets, so we must first check if this situation will exist, if any, then the nearest point to the distance recorded, to update d. So we can get to the minimum distance d.


The key is to detect the nearest point pair, in theory each point should be matched with the point of the opposite set, that efficiency is not enough to meet our requirements. So here to optimize. How to optimize it. Consider, if we choose the Division point M for the boundary, if a point of the horizontal axis of the horizontal axis of the absolute value of more than D1 and more than D2, then this point to M point distance is bound to exceed D1 and D2 of the small, so this point to the other side of the collection of any point of the distance must not be the smallest.



So let's filter out all the points in a range of M-bounds and put them in a set.

After screening, of course, you can take these points 22 distance to update D, but this is still very slow, in case the point of satisfying conditions a lot of it. We have to continue to optimize here. Start by sorting the points in y-coordinates. Assuming that the order is good, there are p points, numbered 1 to p. Then we use number 1th and 2 to the P number of points to find the distance, then 2nd and 3 to p number points to find the distance ... is not finished, because this is more than, the number of times or O (p^2), so in fact, and no difference between optimization.


Suppose there is a point q, the coordinates are XQ, YQ. It can be proved that there will be no more than 6 points in a rectangular region with a length of 2d and a width of D at the midpoint of Q. The concrete proof process can refer to the algorithm introduction.


Using this conclusion, we can continue to optimize the comparison process. We have just used the number 1th to find the distance from the 2 to p number, we know that there will not be more than 6 points in the rectangle in the 1th-point structure diagram. But we can not directly from number 1th to the number 6th, because the P points here are sorted by y-coordinate rather than by distance, it is possible in the y-coordinate, the first 10 points are close to the 1th points, but the first 6 points of the x-coordinate is very far, and the 10th point x-Coordinate and 1th point x-coordinate is very good So the distance from the 10th point to point 1th is closer.


So how do we make use of this conclusion? In this case, assuming that the 1th point and the 2 to P point comparison, due to the y-coordinate ordering, assuming that the y-coordinate of the P-point is greater than the y-coordinate of the 1th point is larger than the current can find the minimum value, then this and all points after this point distance of 1th points is necessarily greater than the current minimum value, so
And because there are few points to meet the comparison criteria, no more than 6, so here can be seen as O (1) efficiency. Then the efficiency of the whole algorithm is probably in O (Nlogn), very fast.


See the code for details:

HDU 1007

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <
Math.h> using namespace std;
Const double EPS = 1e-6;
const int MAXN = 100010;
Const double INF = 1E20;
struct point {double x, y;};
Double Dist (Point A, point B) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));}
Point P[MAXN];
Point TMPT[MAXN];
    BOOL Cmpxy (Point A, point B) {if (a.x! = b.x) {return a.x < b.x;
    } else {return a.y < b.y; }} bool Cmpy (point A, point B) {return a.y < b.y;} double Closest_pair (int left, int. right) {Double d = INF
    ;
    if (left = = right) {return D;
    } if (left + 1 = = right) {return dist (P[left], p[right]);
    } int mid = (left + right)/2;
    Double D1 = Closest_pair (left, mid);
    Double D2 = Closest_pair (mid + 1, right);
    
    d = min (d1, D2);
    int k = 0;
 for (int i = left, I <= right; i++)   {if (Fabs (p[mid].x-p[i].x) <= D) {tmpt[k++] = P[i];
    }} sort (tmpt, Tmpt + K, cmpy);
        for (int i = 0, i < K; i++) {for (int j = i + 1; j < K && Tmpt[j].y-tmpt[i].y < D; j + +)
        {d = min (d, Dist (Tmpt[i], tmpt[j]));
}} return D;
    } int main () {int n; while (scanf ("%d", &n) = = 1 && N) {for (int i = 0; i < n; i++) {scanf ("%l
        F%lf ", &p[i].x, &AMP;P[I].Y);
        } sort (p, p + N, cmpxy);
    printf ("%.2lf\n", Closest_pair (0, n-1)/2);
} return 0;
 }



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.