-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkdtree.go
200 lines (174 loc) · 3.87 KB
/
kdtree.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
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
package kdtree
import (
"container/heap"
"github.com/hongshibao/go-algo"
)
type Point interface {
// Return the total number of dimensions
Dim() int
// Return the value X_{dim}, dim is started from 0
GetValue(dim int) float64
// Return the distance between two points
Distance(p Point) float64
// Return the distance between the point and the plane X_{dim}=val
PlaneDistance(val float64, dim int) float64
}
type PointBase struct {
Point
Vec []float64
}
func (b PointBase) Dim() int {
return len(b.Vec)
}
func (b PointBase) GetValue(dim int) float64 {
return b.Vec[dim]
}
func NewPointBase(vals []float64) PointBase {
ret := PointBase{}
for _, val := range vals {
ret.Vec = append(ret.Vec, val)
}
return ret
}
type kdTreeNode struct {
axis int
splittingPoint Point
leftChild *kdTreeNode
rightChild *kdTreeNode
}
type KDTree struct {
root *kdTreeNode
dim int
}
func (t *KDTree) Dim() int {
return t.dim
}
func (t *KDTree) KNN(target Point, k int) []Point {
hp := &kNNHeapHelper{}
t.search(t.root, hp, target, k)
ret := make([]Point, 0, hp.Len())
for hp.Len() > 0 {
item := heap.Pop(hp).(*kNNHeapNode)
ret = append(ret, item.point)
}
for i := len(ret)/2 - 1; i >= 0; i-- {
opp := len(ret) - 1 - i
ret[i], ret[opp] = ret[opp], ret[i]
}
return ret
}
func (t *KDTree) search(p *kdTreeNode,
hp *kNNHeapHelper, target Point, k int) {
stk := make([]*kdTreeNode, 0)
for p != nil {
stk = append(stk, p)
if target.GetValue(p.axis) < p.splittingPoint.GetValue(p.axis) {
p = p.leftChild
} else {
p = p.rightChild
}
}
for i := len(stk) - 1; i >= 0; i-- {
cur := stk[i]
dist := target.Distance(cur.splittingPoint)
if hp.Len() < k || (*hp)[0].distance >= dist {
heap.Push(hp, &kNNHeapNode{
point: cur.splittingPoint,
distance: dist,
})
if hp.Len() > k {
heap.Pop(hp)
}
}
if hp.Len() < k || target.PlaneDistance(
cur.splittingPoint.GetValue(cur.axis), cur.axis) <=
(*hp)[0].distance {
if target.GetValue(cur.axis) < cur.splittingPoint.GetValue(cur.axis) {
t.search(cur.rightChild, hp, target, k)
} else {
t.search(cur.leftChild, hp, target, k)
}
}
}
}
func NewKDTree(points []Point) *KDTree {
if len(points) == 0 {
return nil
}
ret := &KDTree{
dim: points[0].Dim(),
root: createKDTree(points, 0),
}
return ret
}
func createKDTree(points []Point, depth int) *kdTreeNode {
if len(points) == 0 {
return nil
}
dim := points[0].Dim()
ret := &kdTreeNode{
axis: depth % dim,
}
if len(points) == 1 {
ret.splittingPoint = points[0]
return ret
}
idx := selectSplittingPoint(points, ret.axis)
if idx == -1 {
return nil
}
ret.splittingPoint = points[idx]
ret.leftChild = createKDTree(points[0:idx], depth+1)
ret.rightChild = createKDTree(points[idx+1:len(points)], depth+1)
return ret
}
type selectionHelper struct {
axis int
points []Point
}
func (h *selectionHelper) Len() int {
return len(h.points)
}
func (h *selectionHelper) Less(i, j int) bool {
return h.points[i].GetValue(h.axis) < h.points[j].GetValue(h.axis)
}
func (h *selectionHelper) Swap(i, j int) {
h.points[i], h.points[j] = h.points[j], h.points[i]
}
func selectSplittingPoint(points []Point, axis int) int {
helper := &selectionHelper{
axis: axis,
points: points,
}
mid := len(points)/2 + 1
err := algo.QuickSelect(helper, mid)
if err != nil {
return -1
}
return mid - 1
}
type kNNHeapNode struct {
point Point
distance float64
}
type kNNHeapHelper []*kNNHeapNode
func (h kNNHeapHelper) Len() int {
return len(h)
}
func (h kNNHeapHelper) Less(i, j int) bool {
return h[i].distance > h[j].distance
}
func (h kNNHeapHelper) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *kNNHeapHelper) Push(x interface{}) {
item := x.(*kNNHeapNode)
*h = append(*h, item)
}
func (h *kNNHeapHelper) Pop() interface{} {
old := *h
n := len(old)
item := old[n-1]
*h = old[0 : n-1]
return item
}