This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vptree.go
153 lines (128 loc) · 3.74 KB
/
vptree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package vptree
import (
"container/heap"
"math"
"math/rand"
)
type node struct {
Item interface{}
Threshold float64
Left *node
Right *node
}
type heapItem struct {
Item interface{}
Dist float64
}
// A Metric is a function that measures the distance between two provided
// interface{}-values. The function *must* be a metric in the mathematical
// sense, that is, the metric d must fullfill 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)
type Metric func(a, b interface{}) float64
// A VPTree struct represents a Vantage-point tree. Vantage-point trees are
// useful for nearest-neighbour searches in high-dimensional metric spaces.
type VPTree struct {
root *node
distanceMetric Metric
}
// New creates a new VP-tree using the metric and items provided. The metric
// measures the distance between two items, so that the VP-tree can find the
// nearest neighbour(s) of a target item.
func New(metric Metric, items []interface{}) (t *VPTree) {
t = &VPTree{
distanceMetric: metric,
}
t.root = t.buildFromPoints(items)
return
}
// Search searches the VP-tree for the k nearest neighbours of target. It
// returns the up to k narest neighbours and the corresponding distances in
// order of least distance to largest distance.
func (vp *VPTree) Search(target interface{}, k int) (results []interface{}, distances []float64) {
if k < 1 {
return
}
h := make(priorityQueue, 0, k)
tau := math.MaxFloat64
vp.search(vp.root, &tau, target, k, &h)
for h.Len() > 0 {
hi := heap.Pop(&h)
results = append(results, hi.(*heapItem).Item)
distances = append(distances, hi.(*heapItem).Dist)
}
// Reverse results and distances, because we popped them from the heap
// in large-to-small order
for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
results[i], results[j] = results[j], results[i]
distances[i], distances[j] = distances[j], distances[i]
}
return
}
func (vp *VPTree) buildFromPoints(items []interface{}) (n *node) {
if len(items) == 0 {
return nil
}
n = &node{}
// Take a random item out of the items slice and make it this node's item
idx := rand.Intn(len(items))
n.Item = items[idx]
items[idx], items = items[len(items)-1], items[:len(items)-1]
if len(items) > 0 {
// Now partition the items into two equal-sized sets, one
// closer to the node's item than the median, and one farther
// away.
median := len(items) / 2
pivotDist := vp.distanceMetric(items[median], n.Item)
items[median], items[len(items)-1] = items[len(items)-1], items[median]
storeIndex := 0
for i := 0; i < len(items)-1; i++ {
if vp.distanceMetric(items[i], n.Item) <= pivotDist {
items[storeIndex], items[i] = items[i], items[storeIndex]
storeIndex++
}
}
items[len(items)-1], items[storeIndex] = items[storeIndex], items[len(items)-1]
median = storeIndex
n.Threshold = pivotDist
n.Left = vp.buildFromPoints(items[:median])
n.Right = vp.buildFromPoints(items[median:])
}
return
}
func (vp *VPTree) search(n *node, tau *float64, target interface{}, k int, h *priorityQueue) {
if n == nil {
return
}
dist := vp.distanceMetric(n.Item, target)
if dist < *tau {
if h.Len() == k {
heap.Pop(h)
}
heap.Push(h, &heapItem{n.Item, dist})
if h.Len() == k {
*tau = h.Top().(*heapItem).Dist
}
}
if n.Left == nil && n.Right == nil {
return
}
if dist < n.Threshold {
if dist-*tau <= n.Threshold {
vp.search(n.Left, tau, target, k, h)
}
if dist+*tau >= n.Threshold {
vp.search(n.Right, tau, target, k, h)
}
} else {
if dist+*tau >= n.Threshold {
vp.search(n.Right, tau, target, k, h)
}
if dist-*tau <= n.Threshold {
vp.search(n.Left, tau, target, k, h)
}
}
}