tzaeschke / tinspin-indexes

Spatial index library with R*Tree, STR-Tree, Quadtree, CritBit, KD-Tree, CoverTree and PH-Tree
http://www.tinspin.org
Apache License 2.0
111 stars 24 forks source link

Unbounded KNN Iterator #39

Closed micycle1 closed 7 months ago

micycle1 commented 7 months ago

Can Tinspin support an indefinite nearest neighbor search, where I can iteratively query for the next nearest neighbor without a predetermined 'k' value?

Is it possible to simulate this by setting k to something like Integer.MAX_VALUE, or does Tinspin first search for and gather all points AND THEN provide an iterator on these, which would be less efficient?

tzaeschke commented 7 months ago

It depends on the index you are using. Most indexes now use incremental NN search, i.e. you can safely set k=Integer.MAX_VALUE without risking long initialization times or memory exhaustion. I think the only exception is the CoverTree which will internally first collect all candidates before returning an interator on the internal list.

The KDTree has two different NN search implementations. The default implementation queryKnn() works incrementally and can use k=Integer.MAX_VALUE. The old knnQuery() methods works differently and will first allocate a list of all results (sorry about those method names).

Note: I haven't actually tried setting k=Integer.MAX_VALUE but it should work fine for all except CoverTree (and Array but that should anyway not be used).

micycle1 commented 7 months ago

I'm most concerned with KDTree, so that's good to hear. Thanks.

Maybe worth clarifying this (particularly the cover tree exception) in Javadocs.

tzaeschke commented 7 months ago

Maybe worth clarifying this (particularly the cover tree exception) in Javadocs.

Yes, that makes sense.