forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvp-tree.js
324 lines (270 loc) · 7.48 KB
/
vp-tree.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Mnemonist Vantage Point Tree
* =============================
*
* JavaScript implementation of the Vantage Point Tree storing the binary
* tree as a flat byte array.
*
* Note that a VPTree has worst cases and is likely not to be perfectly
* balanced because of median ambiguity. It is therefore not suitable
* for hairballs and tiny datasets.
*
* [Reference]:
* https://en.wikipedia.org/wiki/Vantage-point_tree
*/
var iterateOver = require('./utils/iterate.js'),
Heap = require('./heap.js');
// TODO: implement better selection technique for the vantage point
// The one minimizing spread of sample using stdev is usually the accepted one
// TODO: rationalize registers. use getArrayPointers to optimize memory
/**
* Heap comparator used by the #.nearestNeighbors method.
*/
function comparator(a, b) {
if (a.distance < b.distance)
return 1;
if (a.distance > b.distance)
return -1;
return 0;
}
/**
* Function used to create the binary tree.
*
* @param {function} distance - Distance function to use.
* @param {array} items - Items to index (will be mutated).
* @param {array} indexes - Indexes of the items.
* @return {Float64Array} - The flat binary tree.
*/
function createBinaryTree(distance, items, indexes) {
var N = indexes.length,
C = 0,
data = new Float64Array(N * 4),
stack = [0, indexes],
distances = [],
sortedDistances = [],
nodeIndex,
currentIndexes,
vantagePoint,
medianIndex,
mu,
left,
right,
i,
l,
h;
while (stack.length) {
currentIndexes = stack.pop();
nodeIndex = stack.pop();
// Getting our vantage point
vantagePoint = currentIndexes.pop();
l = currentIndexes.length;
// Storing vantage point
data[nodeIndex] = vantagePoint;
// If we only have few items left
if (!l)
continue;
if (l === 1) {
// We put remaining item to the right
mu = distance(items[vantagePoint], items[currentIndexes[0]]);
data[nodeIndex + 1] = mu;
// Right
C += 4;
data[nodeIndex + 3] = C;
data[C] = currentIndexes[0];
continue;
}
// Computing distance from vantage point to other points
distances.length = l;
sortedDistances.length = l;
for (i = 0; i < l; i++)
distances[i] = distance(items[vantagePoint], items[currentIndexes[i]]);
// Finding median of distances
h = l / 2;
sortedDistances = distances.slice().sort();
medianIndex = h - 1;
mu = (medianIndex === (medianIndex | 0)) ?
(sortedDistances[medianIndex] + sortedDistances[medianIndex + 1]) / 2 :
sortedDistances[Math.ceil(medianIndex)];
// Storing mu
data[nodeIndex + 1] = mu;
// Dispatching the indexes left & right
left = [];
right = [];
for (i = 0; i < l; i++) {
if (distances[i] >= mu)
right.push(currentIndexes[i]);
else
left.push(currentIndexes[i]);
}
// Right
if (right.length) {
C += 4;
data[nodeIndex + 3] = C;
stack.push(C);
stack.push(right);
}
// Left
if (left.length) {
C += 4;
data[nodeIndex + 2] = C;
stack.push(C);
stack.push(left);
}
}
return data;
}
/**
* VPTree.
*
* @constructor
* @param {function} distance - Distance function to use.
* @param {Iterable} items - Items to store.
*/
function VPTree(distance, items) {
if (typeof distance !== 'function')
throw new Error('mnemonist/VPTree.constructor: given `distance` must be a function.');
if (!items)
throw new Error('mnemonist/VPTree.constructor: you must provide items to the tree. A VPTree cannot be updated after its creation.');
// Properties
this.distance = distance;
this.items = [];
var indexes = [],
self = this,
i = 0;
iterateOver(items, function(value) {
self.items.push(value);
indexes.push(i++);
});
// Creating the binary tree
this.size = indexes.length;
this.data = createBinaryTree(distance, this.items, indexes);
}
/**
* Function used to retrieve the k nearest neighbors of the query.
*
* @param {number} k - Number of neighbors to retrieve.
* @param {any} query - The query.
* @return {array}
*/
VPTree.prototype.nearestNeighbors = function(k, query) {
var neighbors = new Heap(comparator),
stack = [0],
tau = Infinity,
nodeIndex,
itemIndex,
vantagePoint,
leftIndex,
rightIndex,
mu,
d;
while (stack.length) {
nodeIndex = stack.pop();
itemIndex = this.data[nodeIndex];
vantagePoint = this.items[itemIndex];
// Distance between query & the current vantage point
d = this.distance(vantagePoint, query);
if (d < tau) {
neighbors.push({distance: d, item: vantagePoint});
// Trimming
if (neighbors.size > k)
neighbors.pop();
// Adjusting tau
tau = neighbors.peek().distance;
}
leftIndex = this.data[nodeIndex + 2];
rightIndex = this.data[nodeIndex + 3];
// We are a leaf
if (!leftIndex && !rightIndex)
continue;
mu = this.data[nodeIndex + 1];
if (d < mu) {
if (leftIndex && d < mu + tau)
stack.push(leftIndex);
if (rightIndex && d >= mu - tau) // ALT
stack.push(rightIndex);
}
else {
if (rightIndex && d >= mu - tau)
stack.push(rightIndex);
if (leftIndex && d < mu + tau) // ALT
stack.push(leftIndex);
}
}
var array = new Array(neighbors.size);
for (var i = neighbors.size - 1; i >= 0; i--)
array[i] = neighbors.pop();
return array;
};
/**
* Function used to retrieve every neighbors of query in the given radius.
*
* @param {number} radius - Radius.
* @param {any} query - The query.
* @return {array}
*/
VPTree.prototype.neighbors = function(radius, query) {
var neighbors = [],
stack = [0],
nodeIndex,
itemIndex,
vantagePoint,
leftIndex,
rightIndex,
mu,
d;
while (stack.length) {
nodeIndex = stack.pop();
itemIndex = this.data[nodeIndex];
vantagePoint = this.items[itemIndex];
// Distance between query & the current vantage point
d = this.distance(vantagePoint, query);
if (d <= radius)
neighbors.push({distance: d, item: vantagePoint});
leftIndex = this.data[nodeIndex + 2];
rightIndex = this.data[nodeIndex + 3];
// We are a leaf
if (!leftIndex && !rightIndex)
continue;
mu = this.data[nodeIndex + 1];
if (d < mu) {
if (leftIndex && d < mu + radius)
stack.push(leftIndex);
if (rightIndex && d >= mu - radius) // Might not be necessary to test
stack.push(rightIndex);
}
else {
if (rightIndex && d >= mu - radius)
stack.push(rightIndex);
if (leftIndex && d < mu + radius) // Might not be necessary to test
stack.push(leftIndex);
}
}
return neighbors;
};
/**
* Convenience known methods.
*/
VPTree.prototype.inspect = function() {
var array = this.items.slice();
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: VPTree,
enumerable: false
});
return array;
};
/**
* Static @.from function taking an abitrary iterable & converting it into
* a tree.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} distance - Distance function to use.
* @return {VPTree}
*/
VPTree.from = function(iterable, distance) {
return new VPTree(distance, iterable);
};
/**
* Exporting.
*/
module.exports = VPTree;