vptree

package
v0.0.0-...-920cd30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 4 Imported by: 0

README

Go package

  • vptree : Vantage point tree for efficient exact or approximate k-nearest neighbor and range searches.

Implements the algorithm from: "Data Structures and Algorithms for Nearest Neighbor Search in General Metric Spaces" by Peter N. Yianilos

see http://web.cs.iastate.edu/~honavar/nndatastructures.pdf

Documentation

Overview

Package vptree implements a 'vantage point tree' for exact or approximate nearest neighbor and range searches. The dataset consists of n d-dimensional points in a metric space. Points with identical positions are treated as unique entities. Any datatype that implements the Point interface can be handled.

See http://web.cs.iastate.edu/~honavar/nndatastructures.pdf : "Data Structures and Algorithms for Nearest Neighbor Search in General Metric Spaces" by Peter N. Yianilos

TODO: serialize/deserialize TODO: updates (insert/delete element)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	NNodes   int // number of nodes
	NLeaves  int // number of leaves
	MaxDepth int
}

Info contains structural information about a vp-tree.

type Point

type Point interface {
	// Distance returns the distance between the receiver and p.
	Distance(p Point) float32
}

Point in a metric space of arbitrary dimensions. The distance metric must fulfil the following requirements:

d(x, y) >= 0
d(x, y) = 0 if and only if x == y
d(x, y) = d(y, x)
d(x, z) <= d(x, y) + d(y, z) (triangle inequality)

Manhattan (Lⁱ norm) or Euclidian (L² norm) metrics are suitable. Using squared distances won't work as this violates the triangle equality.

type VPTree

type VPTree struct {
	// contains filtered or unexported fields
}

VPTree represents a vantage point tree.

func NewVPTree

func NewVPTree(points []Point, nTrials int) *VPTree

NewVPTree returns an initialized tree holding the given points. If desired the tree is optimized by trial and error for efficient search by finding the best vantage point layout. The higher nTrials is, the faster the search performance, but the slower the tree initialization.

func (*VPTree) ApproximateKNN

func (t *VPTree) ApproximateKNN(query Point, k, nn int) ([]Point, []float32)

ApproximateKNN returns the approximate k nearest neighbors to the query point, together with their respective distances. The nn parameter specifies the maximal number of nodes that are visited during the search, and can thus be used to limit the time spent searching. The results are sorted from nearest to farthest. If k > total number of samples in the tree, k is limited to the total number of samples.

func (*VPTree) Info

func (t *VPTree) Info() Info

Info returns basic structural information about the vp-tree.

func (*VPTree) KNN

func (t *VPTree) KNN(query Point, k int) ([]Point, []float32)

KNN returns the exact k nearest neighbors to the query point, together with their respective distances. The results are sorted from nearest to farthest. If k > total number of samples in the tree, k is limited to the total number of samples. When there is a tie for the farthest of the nearest neighbors, k won't increase to always return all of them.

func (*VPTree) NN

func (t *VPTree) NN(query Point) (Point, float32)

NN returns the exact nearest neighbor to the query point, together with its distance.

func (*VPTree) RangeNN

func (t *VPTree) RangeNN(query Point, r float32) ([]Point, []float32)

RangeNN returns the neighbors with a distance <= r to the query point, together with their respective distances. The results, if any, are sorted from nearest to farthest.

func (*VPTree) String

func (t *VPTree) String() string

String returns a textual rendering of the tree. Warning: output can be huge!

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL