Nearest Neighbor Finding
Using Kd-tree
Ref: Andrew Moore’s PhD thesis
(1991)
Kd-tree [Bentley ’75]
On the NN Problem
• Existing packages exist
– ANN (U. of Maryland)
– Ranger (SUNY Stony Brook)
• Only work for Rn. Configuration spaces
can have R and S
Algorithm NN(kd,target)
•
•
[Global output: nearest, min-dist-sqd = ]
If kd is leaf: compute d2 = d2(target,leafnode)
– if d2 < min-dist-sqd
• nearest leafnode
• min-dist-sqd d2
– Return
•
•
b
pivot
c
d2: distance
squared
Determine nearer-kd and further-kd
NN (nearer-kd, target)
target
e
c
nearer
e
•
Check d2(pivot_node,target); update min-dist-sqd & nearest if necessary
•
Check whether further-kd need to be checked
further
b
– {find p – closest pt in hr w.r.t. target, check if d2(p,target) < min-dist-sqd}
– If so, call NN(further-kd, target)
hr: hyper-rectangle
a
Nearest = ?
dist-sqd =
b
c
NN(c, x)
f
nearer
further
e
b
c
d
e
g
d
Nearer = e
Further = b
NN (e, x)
g
a
f
a
Nearest = ?
dist-sqd =
b
c
NN(e, x)
f
e
c
e
b
further
nearer
d
g
g
d
Nearer = g
Further = d
NN (g, x)
a
f
a
Nearest = ?
dist-sqd =
b
c
NN(g, x)
f
e
b
c
d
r
e
g
g
d
Nearest = g
dist-sqd = r
a
f
a
Nearest = g
dist-sqd = r
b
c
NN(e, x)
f
e
b
c
d
r
e
g
g
d
Check d2(e,x) > r
No need to update
a
f
a
Nearest = g
dist-sqd = r
b
c
NN(e, x)
f
c
e
b
p
d
r
e
g
a
g
d
Check further of e: find p
d (p,x) > r
No need to update
f
a
Nearest = g
dist-sqd = r
b
c
NN(c, x)
f
e
b
c
d
r
e
g
g
d
Check d2(c,x) > r
No need to update
a
f
a
Nearest = g
dist-sqd = r
b
c
NN(c, x)
f
e
b
p
c
d
r
e
g
a
g
d
Check further of c: find p
d(p,x) < r !!
NN (b,x)
f
a
Nearest = g
dist-sqd = r
b
c
NN(b, x)
f
e
b
c
d
r
e
g
d
Nearer = f
Further = g
NN (f,x)
g
a
f
a
Nearest = g
dist-sqd = r
b
c
NN(f, x)
f
e
r’
b
c
d
e
g
g
d
r’ = d2 (f,x) < r
dist-sqd r’
nearest f
a
f
a
Nearest = f
dist-sqd = r’
b
c
NN(b, x)
f
e
r’
b
c
d
e
g
g
d
Check d(b,x) < r’
No need to update
a
f
a
Nearest = f
dist-sqd = r’
b
c
NN(b, x)
f
c
p
e
r’
d
e
b
g
a
g
d
Check further of b; find p
d(p,x) > r’
No need to update
f
a
Nearest = f
dist-sqd = r’
b
c
NN(c, x)
f
e
r’
b
c
d
e
d
g
g
a
f
Find closest point p
in hr to target t
Kdtree vs. Target
ym`ax
p
t
ymin
xmin
xmin
px t x
x
max
xmax
t x xmin
xmin t x xmax
t x xmax
ymin
py t y
y
max
t y ymin
ymin t y ymax
t y ymax
Time Complexity
• At least O(log N) inspections are
necessary
• No more than N nodes are searched: the
algorithm visits each node at most once
• Depends on the point distribution
Pivoting Strategy
• Properties of ideal Kd-tree
– Reasonably balanced
• O(logN) behavior
– Leaf nodes fairly equally proportioned
• Maximum cutoff opportunities for the nearest neighbor
search
• Possible Strategies
– “Splitting dimension as the maximum variance, pivot
set at median” – pray for alternating and balancing
splits
– Other strategies possible: “middle of the most spread
dimension” (see next page)
Exercise
a
nearest = ?
min-dist-sqd =
b
c
NN(c, x)
f
e
b
c
d
e
d
g
g
a
f
© Copyright 2026 Paperzz