Effectively find n nearest neighbors of 50k 2D coordinates?

I have an array with latitudes and longitudes. The task is to find 5 nearest coordinates for all coordinates without cyclic transition in all coordinates every time.

+4
source share
1 answer

There are several solutions depending on your data (which you did not say anything about) and how confident you want to be.

  • If your data is evenly distributed, you can create a grid on top of your data and assign points to the grid. After that, for each element you will find out which grid it belongs to and compares the distances in this grid (and in the nearest grids). With a good mesh selection and assuming that there are an average of k elements in the mesh, this can give you potential time O(n * k^2). Take a look at this answer for more explanation .
  • Knowing nothing about the data, you can build a 2-d tree in O (n log n) time, and then for each point in your database to find out what is the point closest to it (you ask about it in O (logn ) for all n points). So the overall complexityO(n log n )
  • , . , , . , .
  • @Gene , quadtree ( , )

, , O(n^2).

, , . , , , , 5 .

+3

Source: https://habr.com/ru/post/1624856/


All Articles