CS 372: Computational Geometry
Lecture 1
Fixed-Radius Near Neighbors Searching
Antoine Vigneron
King Abdullah University of Science and Technology
September 2, 2012
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
1 / 12
1
Introduction
2
Problem formulation
3
Brute-force approach
4
The one-dimensional case
5
Higher Dimension
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
2 / 12
Introduction
Reference:
Dave Mount’s lecture notes, lecture 2.
Wikipedia.
An old problem.
Applications: Air traffic control, molecular dynamics . . .
Simple and very fast algorithm.
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
3 / 12
Problem Formulation
p6
p9
p5
p1
p7
p4
p2
r
p3
p8
INPUT: a set of points P = {p1 , . . . , pn } and a radius r .
We assume we are in fixed dimension, that is, P ⊂ Rd with d = O(1).
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
4 / 12
Problem Formulation
p6
p9
p5
p1
p7
p4
p2
r
p3
p8
OUTPUT: all pairs (pi , pj ) ∈ P 2 such that the distance |pi pj | is at most r .
Here, the output is: (p1 , p2 ), (p1 , p4 ), (p2 , p3 ), (p2 , p4 ), (p4 , p5 ), (p6 , p7 ).
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
5 / 12
Brute-Force Approach
For any two points pi , pj , we can compute |pi pj |2 in constant time.
I
Example: if d = 2, pi = (xi , yi ) and pj = (xj , yj ), then
|pi pj |2 = (xi − xj )2 + (yi − yj )2 .
So we can just check for each pair (i, j) whether |pi pj |2 6 r 2 .
I
I
Running time?
Θ(n2 ).
Can we do better than O(n2 ) ?
I
I
o(n2 ) is impossible, because in
the worst case the output size k (the
number of output pairs) is n2 = n(n − 1)/2 = Θ(n2 ).
But we will give below an O(n + k) algorithm, which is better in that it
is output sensitive.
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
6 / 12
The One-Dimensional Case
INPUT: x1 , . . . , xn ∈ R and r > 0.
OUTPUT: the k pairs (xi , xj ) such that |xi − xj | 6 r .
Exercise: find a simple O(n log n + k) algorithm.
Sort the numbers in increasing order.
Traverse this sorted list from left to right.
Pseudocode:
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
7 / 12
A Bucketing Approach
We partition R using intervals (buckets) of size r :
. . . , [−3r , −2r ), [−2r , −r ), [−r , 0), [0, r ), [r , 2r ), [2r , 3r ) . . .
So each interval [br , (b + 1)r ) corresponds to some integer b.
A point x ∈ R is in bucket b = bx/r c.
I
So we can determine the bucket containing x in O(1) time.
Observation: we may only have |xi − xj | 6 r if
I
I
I
xi and xj lie in the same bucket b . . .
or xi is in b and xj is in b + 1.
or xj is in b and xi is in b + 1.
So our algorithm only checks pairs in the same bucket, or in adjacent
buckets.
We store the nonempty buckets in a dictionary data structure:
I
It allows deletion, insertion, lookup.
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
8 / 12
Pseudocode
1D-Bucketing algorithm
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Create an empty dictionary D.
for i ← 1, n do
bi ← bxi /r c.
if bi ∈
/ D then
Insert bucket bi into D.
Insert xi into bucket bi .
for each nonempty bucket b do
Report all pairs (x, x 0 ) in bucket b.
if b + 1 is nonempty then
Report all pairs (x, x 0 ) such that
x is in b, x 0 is in b + 1, and x 0 − x 6 r .
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
9 / 12
Analysis
The bucketing algorithm runs in time O(k) + n.T (n), where T (n) is
the time needed for one dictionary operation.
I
Proof done in class. See D. Mount’s notes.
So it is O(n log(n) + k) if we implement the dictionary with a
balanced binary search tree.
It is (randomized) expected time O(n + k) using a hash table.
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
10 / 12
Two-Dimensional Case
The bucketing approach applies.
The integer pair (bx , by ) correspond to the square
[bx r 0 , (bx + 1)r 0 ) × [by r 0 , (by + 1)r 0 )
√
where r 0 = r / 2.
For a bucket (bx , by ), we only need to check 20 adjacent buckets.
r
√
r0 = r/ 2
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
11 / 12
Higher Dimension
Bucketing applies to any dimension d.
But the running time is exponential in d.
In high dimension, the best known time bounds are not much better
than brute force.
Antoine Vigneron (KAUST)
CS 372 Lecture 1
September 2, 2012
12 / 12
© Copyright 2026 Paperzz