open class VantagePointTree[V](elements: Seq[V], val distance: (V, V) =>Double, val leafSize: Int = ...)
A vantage-point tree over elements: a static index for range and nearest-neighbour queries in any metric.
Each inner node holds one element (its vantage point) and splits the rest by distance to it, the nearer half to one child and the farther half to the other; the triangle inequality then lets a query skip a child without measuring its elements. Nodes with at most leafSize elements are scanned directly.
What a query returns does not depend on the tree's shape:
withinDistance(query, radius) is every element e with distance(query, e) <= radius, the boundary included (the comparison is on the computed distance, so an element exactly at the radius is found), in the order of elements.
nearest(query, k) is the k elements nearest to query, nearest first, the first k of the stable sort of elements by distance: among elements at equal distance the one earlier in elements comes first. k larger than size returns all of them.
Both equal a linear scan whenever distance is a metric. Pruning compares distances with a relative slack of 1e-12, so the rounding of a computed metric (Euclidean distances violate the triangle inequality in the last bits) never hides an element. A distance that violates the triangle inequality by more than that (a correlation-derived dissimilarity, say) may make a query miss elements; use BruteForce for those.
The tree is built deterministically (a fixed pseudo-random choice of vantage points, a split at the median), is balanced whatever the data, and handles coincident elements: it builds in O(n log n) distance evaluations, a query costs O(log n) distance evaluations in low dimension. distance must be symmetric, non-negative and not NaN; infinite distances are allowed.
Type parameters
V
the element type, which is also the type of a query
Value parameters
distance
a metric on the elements
elements
the indexed elements; positions in this sequence are the indices the ...Indices queries return
leafSize
the largest number of elements scanned without further splitting (at least 1)
The indices (ascending) of the elements at distance at most radius from query, and the distance to each, as the walk measured it: the elements indicesWithinDistance finds, with their distances, from one walk. Both arrays are fresh and aligned. None as soon as the walk has measured more than maxMeasurements distances (a radius so large that the tree prunes too little to beat a scan of every element, which is the caller's cue to scan). A negative or NaN radius finds nothing.
The indices (ascending) of the elements at distance at most radius from query, and the distance to each, as the walk measured it: the elements indicesWithinDistance finds, with their distances, from one walk. Both arrays are fresh and aligned. None as soon as the walk has measured more than maxMeasurements distances (a radius so large that the tree prunes too little to beat a scan of every element, which is the caller's cue to scan). A negative or NaN radius finds nothing.
The indices (positions in elements, ascending) of the elements at distance at most radius from query. A negative or NaN radius finds nothing, an infinite one everything.
The indices (positions in elements, ascending) of the elements at distance at most radius from query. A negative or NaN radius finds nothing, an infinite one everything.
The largest distance at most radius from query to an element, Double.NegativeInfinity when there is none (a negative or NaN radius finds nothing): the largest value indicesWithinDistance would have found, without the list of elements. The walk gives up and returns Double.NaN as soon as it has measured more than maxMeasurements distances: a radius so large that the tree prunes too little to beat a scan of every element, which is the caller's cue to scan.
The largest distance at most radius from query to an element, Double.NegativeInfinity when there is none (a negative or NaN radius finds nothing): the largest value indicesWithinDistance would have found, without the list of elements. The walk gives up and returns Double.NaN as soon as it has measured more than maxMeasurements distances: a radius so large that the tree prunes too little to beat a scan of every element, which is the caller's cue to scan.
The indices of the k elements nearest to query, nearest first, ties by ascending index. Fewer than k when the tree holds fewer elements. Throws IllegalArgumentException for a negative k.
The indices of the k elements nearest to query, nearest first, ties by ascending index. Fewer than k when the tree holds fewer elements. Throws IllegalArgumentException for a negative k.