Algorithm for finding nearby points?

Bemmu picture Bemmu · May 8, 2009 · Viewed 9.6k times · Source

Given a set of several million points with x,y coordinates, what is the algorithm of choice for quickly finding the top 1000 nearest points from a location? "Quickly" here means about 100ms on a home computer.

Brute force would mean doing millions of multiplications and then sorting them. While even a simple Python app could do that in less than a minute, it is still too long for an interactive application.

The bounding box for the points will be known, so partitioning the space into a simple grid would be possible. However the points are distributed somewhat unevenly, so I suspect most grid squares would be empty and then suddenly some of them would contain a large portion of the points.

Edit: Does not have to be exact, actually can be quite inaccurate. It wouldn't be a huge deal if the top 1000 are actually just some random points from the top 2000 for example.

Edit: Set of points rarely changes.

Answer

Juha Syrjälä picture Juha Syrjälä · May 8, 2009

How about using quadtree?

You divide area to rectangles, if area has low density of points, rectangles are large, and if area has high density of points, rectangles will be small. You recursively subdivide each rectangle to four sub rectangles until rectangles are small enough or contain few enough points.

You can then start looking at points in rectangles near the location, and move outwards until you have found your 1000 points.

Code for this could get somewhat complex, so maybe you should try first with the simple grid and see if it is fast enough.