So the question is like this:
Given a location X and an array of locations, I want to get an array of locations that are closest to location X, in other words sorted by closest distance.
The way I solved this is by iterating through each location array and calculate the distance between X and that particular location, store that distance, and sort the location by distance using a Comparator. Done! Is there a better way to do this? Assuming that the sort is a merge sort, it should be O(n log n).
If I understand this right, you can do this pretty quickly for multiple queries - as in, given several values of X, you wouldn't have to re-sort your solution array every time. Here's how you do it:
This takes O(n + logn) for each query with O(nlogn) preprocessing. Of course, if we were talking about just one X, this is not better at all.
The problem you describe sounds like a m
-nearest neighbor search to me.
So, if I correctly understood your question, i.e. the notion of a location
being a vector in a multidimensional metric space, and the distance
being a proper metric in this space, then it would be nice to put the array of locations
in a k
-d-Tree.
You have some overhead for the tree building once, but you get the search for O(log n)
then.
A benefit of this, assuming you are just interested in the m < n
closest available locations, you don't need to evaluate all n
distances every time you search for a new X
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With