Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the nearest dot in a 2D space

Tags:

algorithm

Yesterday I read a problem which can be translated into the following problem with slight modification:

The coordinate of a dot is expressed by (x, y) in a 2D space.

Input : An array of dots ARRAY = (x1, y1), (x2, y2), (x3, y3), ..., (xn, yn) and another dot D = (xi, yi)

Find the dot in the ARRAY which is nearest to D.

By saying "nearest", I am referring to the Euclidian distance.


There is an obvious solution: traverse each dot in the ARRAY and compute its Euclidian distance to D. Then, find the shortest distance. This can be done in O(N) time.

Can we do faster, say, in O(logN)? I am trying to use divide-and-conquer approach, but haven't come to a concrete idea yet.


Generalization of the problem: how to find the nearest dot in a K-dimensional space? Can we do it in less than O(N)?

like image 755
SiLent SoNG Avatar asked Aug 17 '10 02:08

SiLent SoNG


People also ask

What is the complexity of the 2d closest pair problem?

The time complexity of this algorithm will be O(n log n).

How do I find the nearest point in Matlab?

k = dsearchn( P , PQ ) returns the indices of the closest points in P to the query points in PQ measured in Euclidean distance. k = dsearchn( P , T , PQ ) returns the indices of the closest points in P by using the Delaunay triangulation T , where T = delaunayn(P) .

How do you find the closest pair of points?

The closest pair is the minimum of the closest pairs within each half and the closest pair between the two halves. To split the point set in two, we find the x-median of the points and use that as a pivot. Finding the closest pair of points in each half is subproblem that is solved recursively.


1 Answers

If the array is not sorted in any way, then it is not possible to do any faster than O(n), as you have to check every point to make sure it is not closer than anything you have found so far.

You CAN do some preprocessing to sort the points or pack them into some structure, then do searches on that structure. In this case, while the preprocessing step may be slower than O(n), the individual searches may be faster. If you have many points to check against the same set of points, this can be advantageous.

like image 124
slacker Avatar answered Dec 08 '22 00:12

slacker