tags:

views:

185

answers:

4

Hi All,

I have a large array (>10^5 entries) of 3D coordinates r=(x, y, z), where x, y and z are floats. Which is the most efficient way to search a given coordinate r' in the array and give the array index. Note that the r' may not given with the same accuracy as r; say, if the array has stored coordinate (1.5, 0.5, 0.0) and r' is given as (1.49999, 0.49999, 0.0), the algorithm should rightly pick the coordinate. I am developing the code in C.

How can one use O(1) search capability of hash table for this purpose? Converting the coordinate into string is out of question due to accuracy related issue. Is there any particular data structure that would help in O(1) algorithm?

Thanks

OnRoadCoder

+4  A: 

check R-trees, already implemented on some RDBMS, like SQLite, and (i think) Postgres

Javier
A tree can't do O(1), but then again, my approach cannot either unless you make the hash table balanced.
Hamish Grubijan
BTW - the R-Tree implementation available in most RDBMSs is 2D/2.5D only, since they're almost always based on the OpenGIS Spec, which doesn't provide true 3D support --- However, R-Trees are a very good space partioning option, if you implement your own (or find a good 3D implementation).
Reed Copsey
A: 
Hamish Grubijan
This can be very difficult to do in a general purpose case - distribution of input data will either make this work, or break it horribly.
Reed Copsey
I agree. I was reinventing the wheel.
Hamish Grubijan
The data point is sparse, not more than 20, within a single cube (say, unit cube); but I was wondering if there are algorithm for general case. Anyway, let me try something based on tree.
OnRoadCoder
+4  A: 

In order to have "fuzzy" searching as you're describing (so you can support slight inaccuracies), you will have to sacrifice on O(1) algorithms.

That being said, there are some very good algorithms for this. Space partitioning (such as using an Octree or KD-Tree) is a common, popular option.

Reed Copsey
Thanks Reed. Are there GNU lib for efficient space partitioning, including Octree/KD-Tree?
OnRoadCoder
ANN (LGPL) will do this, and is pretty fast: http://www.cs.umd.edu/~mount/ANN/ STANN is also good: http://sites.google.com/a/compgeom.com/stann/ These aren't space partitioning libraries as much as searching libraries (so they do the partitioning for you, and you just use them...)
Reed Copsey
A: 

What you are asking sounds like Nearest Neighbour Search. One approach might be to code a kd-tree (or any space partition based technique) and use that to find the nearest point to your query. But you can also go with a hash based approach, which basically does what Ipthnc's answer describes, but tries to avoid bad performance for degenerate cases.

MAK
Very similar to nearest neighbor search, but not precisely; trying to find points which forms certain type of 3D geometrical clusters. Thanks for your suggestion.
OnRoadCoder