<p>I'd recommend using a kd-tree or an R-tree. These structures automatically partition the data.</p>

<p>A <a href="http://en.wikipedia.org/wiki/Kd-tree" rel="nofollow">kd-tree</a> (k-dimensional tree) is a binary tree in which every node is a point. Every non-leaf node splits the space in half along one of the axes. Points on one side of the split form the left subtree, points on another side form the right subtree. A kd-tree has guaranteed space complexity O(n) and average search, insertion and deletion complexity of O(log n). The cost of building the tree is O(n log n) when using an advanced linear median-finding algorithm. The k-nearest-neighbors search boils down to moving down the tree, checking distances to the elements in final subtree and unwinding the recursion up the tree if more elements are needed. kd-trees are easy to implement in memory, but non-trivial to implement on disk. They may need to be rebuilt when changing data.</p>

<p><a href="http://en.wikipedia.org/wiki/R-tree" rel="nofollow">R-trees</a> (rectangle trees) group nearby objects in minimum bounding rectangles, group the rectangles in larger rectangles, and so on. This means that they can store not only points, but also polygons. The complexity of search is between O(log n) and O(n), depending on the amount of overlap between rectangles. There are various optimization strategies and algorithms for splitting overflowing nodes and bulk-loading. While more complicated, R-trees have advantages over kd-trees. They are more disk-oriented, making them useful for real databases. Unlike non-bulk-loaded kd-trees, they are balanced, and therefore preferred for changing data.</p>

<p>In conclusion, if the data will change after building the tree or you want the data to be stored on disk, I'd use an R-tree, otherwise kd-tree.</p>

CC BY-SA 3.0
2012-07-17T17:21:16.033Z
2012-07-17T18:47:35.613Z
2012-07-17T18:47:35.613Z
Pkkm
<p>An elaboration on <a href="https://stackoverflow.com/questions/3944649/suitable-choice-of-data-structure-and-algorithm-for-fast-k-nearest-neighbor-sear">this question</a>, but with more constraints.</p>

<p>The idea is the same, to find a simple, fast algorithm for k-nearest-neighbors in 2 euclidean dimensions.  The bucketing grid seems to work nicely if you can find a grid size that will suitably partition your data.  However, what if the data is not uniformly distributed, but has areas with both very high and very low density (for example, the US population), so that no fixed grid size could guarantee both enough neighbors and efficiency?  Can this method still be salvaged?</p>

<p>If not, other suggestions would be helpful.</p>

<ds.algorithms><ds.data-structures><cg.comp-geom><clustering><near-neighbors>
Simple k-nearest-neighbor algorithm for euclidean data with highly variable density?
donnyton
# Simple k-nearest-neighbor algorithm for euclidean data with highly variable density?

(Asked by: username_0 on 2012-07-11)

An elaboration on [this question](https://stackoverflow.com/questions/3944649/suitable-choice-of-data-structure-and-algorithm-for-fast-k-nearest-neighbor-sear), but with more constraints.

The idea is the same, to find a simple, fast algorithm for k-nearest-neighbors in 2 euclidean dimensions. The bucketing grid seems to work nicely if you can find a grid size that will suitably partition your data. However, what if the data is not uniformly distributed, but has areas with both very high and very low density (for example, the US population), so that no fixed grid size could guarantee both enough neighbors and efficiency? Can this method still be salvaged?

If not, other suggestions would be helpful.

**Tags:** ds.algorithms, ds.data-structures, cg.comp-geom, clustering, near-neighbors

---

## 1 answers

### Answer by username_1 on 2012-07-17. Score: 1

I'd recommend using a kd-tree or an R-tree. These structures automatically partition the data.

A [kd-tree](http://en.wikipedia.org/wiki/Kd-tree) (k-dimensional tree) is a binary tree in which every node is a point. Every non-leaf node splits the space in half along one of the axes. Points on one side of the split form the left subtree, points on another side form the right subtree. A kd-tree has guaranteed space complexity O(n) and average search, insertion and deletion complexity of O(log n). The cost of building the tree is O(n log n) when using an advanced linear median-finding algorithm. The k-nearest-neighbors search boils down to moving down the tree, checking distances to the elements in final subtree and unwinding the recursion up the tree if more elements are needed. kd-trees are easy to implement in memory, but non-trivial to implement on disk. They may need to be rebuilt when changing data.

[R-trees](http://en.wikipedia.org/wiki/R-tree) (rectangle trees) group nearby objects in minimum bounding rectangles, group the rectangles in larger rectangles, and so on. This means that they can store not only points, but also polygons. The complexity of search is between O(log n) and O(n), depending on the amount of overlap between rectangles. There are various optimization strategies and algorithms for splitting overflowing nodes and bulk-loading. While more complicated, R-trees have advantages over kd-trees. They are more disk-oriented, making them useful for real databases. Unlike non-bulk-loaded kd-trees, they are balanced, and therefore preferred for changing data.

In conclusion, if the data will change after building the tree or you want the data to be stored on disk, I'd use an R-tree, otherwise kd-tree.


---

