Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to find for all points in set A the nearest neighbor in set B

Suppose we have two sets of points A, B, and we want to find for every point in set A its nearest neighbor in set B.

There are many good algorithms to find the nearest neighbor for one point. Is there some way to use the information we got for a_1, to more efficiently search for the nearest neighbor for a_2 or other points in the set?

I am thinking something like: use triangular inequlity to get a interval for possible distance between every point in B and new point a_2, and sort the max and min of the intervals, and then I can search only the points in B which falls in the first interval.

like image 407
gstar2002 Avatar asked Oct 21 '12 17:10

gstar2002


People also ask

What type of algorithm is nearest neighbor?

The k-nearest neighbors algorithm, also known as KNN or k-NN, is a non-parametric, supervised learning classifier, which uses proximity to make classifications or predictions about the grouping of an individual data point.

Is KNN a neighborhood search algorithm?

In statistics, the k-nearest neighbors algorithm (k-NN) is a non-parametric supervised learning method first developed by Evelyn Fix and Joseph Hodges in 1951, and later expanded by Thomas Cover. It is used for classification and regression.

What is nearest-neighbor algorithm in math?

One strategy for solving the traveling salesman problem is the nearest-neighbor algorithm. Simply stated, when given a choice of vertices this algorithm selects the nearest (i.e., least cost) neighbor. In our applet below your goal is to select a Hamiltonian circuit using the nearest-neighbor algorithm.


2 Answers

  1. Find Voronoi diagram for points of set B.
  2. Apply a Sweep line algorithm over points of set A and Voronoi diagram of set B. Wherever sweep line covers some point from set A, look between which edges of Voronoi diagram this point is located. This allows to determine to which face of Voronoi diagram this point belongs. Which gives the closest point from set B.

Details for step 2: Keep all edges of Voronoi diagram, currently intersected by sweep line, in some ordered container. When sweep line covers some vertex of Voronoi diagram, remove/add edges, incident to this vertex, from/to container. To look, between which edges some point is located, get successor/predecessor edges to this point in container.

Time complexity is O((M+N) log M). N = |A|, M = |B|.

like image 163
Evgeny Kluev Avatar answered Nov 15 '22 21:11

Evgeny Kluev


You may benefit from reading bentleys "writing efficient programs" where he deals with a case study of the traveling salesman program. One of the savings that he recognized was that the distince between two points involved taking a square root which was expensive. Taking the square root gives you the actual distance, not taking the square root gives you a number which can be used to compare against other relative values.

I highly recommend reading the book. It will put your brain in the right place.

like image 26
EvilTeach Avatar answered Nov 15 '22 22:11

EvilTeach