0

I am building a mySQL table listing points in n-dimensions, each dimension being indexed. Given any point in the n-dimensional system, I would like to be able to output all of the other points in order of their distance from the chosen point.

A simple solution would be to calculate distances from each point using the pythagorean theorem... sqrt(x^2+y^2)=z. I have been seeking a more efficient method. Only an approximate order is needed, so i'm very open minded.

Thanks.

-diddle

dittle
  • 41
  • 1
  • 7
  • It seems that I should note that I am looking for a method that entirely avoids the Pythagorean method. I assume it would involve the indexed columns and some form of advanced join or db theory that I have not come across. – dittle Oct 04 '09 at 04:57

2 Answers2

1

A common technique for this sort of thing is to consider the squared distance instead of the actual distance which eliminates the square root, but, if I am understanding the question correctly, you don't need to retrieve the actual distance from your index. In that case you could just use the sum of the absolute value of each component.

Andrew Khosravian
  • 1,069
  • 6
  • 13
  • Nope. Summing the absolute values is not a measure of 'distance' in the common, Euclidean sense. (You are talking of the '1-norm' measure of distance, instead.) You're right about storing / comparing the square though, that's fine. – Peter Oct 04 '09 at 04:47
  • yeah, squares look fine to compare, thanks for that. should help to reduce some of the overhead. – dittle Oct 04 '09 at 04:54
0

Along with what's been given, you could also consider "binning" your points -- i.e. (at least mentally) draw a grid over your "map", and track points based on which square they fall into. Basically, you start with the points in the same square, then the ones in a "ring" surrounding the chosen point's square, then the next ring outward, and so on. Depending on the size of grid you use, you can make this about as precise or approximate as you like. Of course, a 2D grid is for 2D points -- if have more dimensions, you'll have to increase the dimensionality of the grid to match.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111