0

I have a large array (>1 million) of 3-dimensional points, a 3-tuple (x, y, z) which takes float values. The points are bounded between (0,0,0) and (Nx, Ny, Nz). I am interested in finding the index of a point coming from some calculation, say R, which is present somewhere in this array. What is the fastest way of doing this? Is there any O(1) algorithm to search for R through the array? The good thing is that the distance between any two points is always greater than 0.005.

I have been thinking of creating a hash-table for storing the index of all the points in the array, but I need a good hash-function that can convert (x, y, z) to a unique hash-key. Is there a simple hash-function that can do that? Is there any C-based library that will help?

A simple hash-function which I thought could generate a unique hash-key is:

h(x,y,z) = 1000*x + 1000000*y + 1000000000*z

This is followed by creating another integer array P of size = max(h), and storing the index of (x,y,z) at P[h(x,y,z)] (rounding-off needs to be taken care). The worry is the size of array P - is this approach good enough for this problem?

Alternative to hash would be using k-d tree. In terms of run-time, which performs better, hash-key or tree-based approach?

(I am using C for this work).

Rak
  • 99
  • 1
  • 4

1 Answers1

0

I'm not sure if I understand the question correct, but I can't leave comments. Just a tip to find values in large arrays is min(array - the value you looking for). I find this quite quick, but I'm not sure what is the fastest way. But with finding your value you never get O(1) always at least O(n) ;)

J. Goedhart
  • 209
  • 1
  • 14
  • I want the index of **R** in the array (it is somewhere in the array for sure). What you suggest is trivial algorithm of O(n); what I am looking is a fast algorithm, based on hash-function which, at the least, can deliver O(log(n)) performance. – Rak Mar 16 '17 at 04:18