Spatial Association Search

Suppose you have a large number entities spread out over a large area and you need to know the nearest other entity or entities. Suppose you also have fairly tight time constraints and you don't mind some imperfection. I present an algorithm to quickly and simultaneously approximate the nearest neighbors for a large number of objects.

Each of N entities has M pointers to other entities. These pointers all start by referencing the entity itself. As often as desirable, we will iterate over all entities, exchanging one of these pointers with a pointer from another entity that we have a pointer to. We swap only when the total distance between both entities and those entities being considered for a swap would be reduced by the swap. We never swap a pointer to an entity to that same entity, and should an entity have a pointer to itself, it shall be swapped at random with any pointer in any other entity.

For each iteration of this loop, the average distance to discovered neighbors will decrease to some steady state. However, this is not completely desirable: it is possible that this steady state has no path to discover better neighbors, or that loops of pointers have formed. To correct for this, we will swap a small fraction (~0.5%) of pointers at random each iteration.

When an entity needs to find nearby entities, simply iterate over its pointers to other entities and consider those entities as the results. This can be repeated to any desired depth by searching those entities' pointers, but a greater depth returns many more results and may be stuck in a cycle. Entities returned in this search will typically be the nearest, but there is no guarantee.

Removing an element requires recovering all pointers to an entity by iterating through all entities and swapping any pointers to the deleted entity with one of the pointers available from the deleted entity.

Performance