99 Geometric Search
99 Geometric Search
1
Algorithms R OBERT S EDGEWICK | K EVIN W AYNE
‣ 1d range search
‣ kd trees
‣ line segment intersection
Algorithms
‣ interval search trees
F O U R T H E D I T I O N
‣ 1d range search
‣ kd trees
‣ interval search trees
Algorithms
‣ line segment intersection
5
1d range search: elementary implementations
unordered list 1 N N
N = number of keys
R = number of keys that match
6
1d range count: BST implementation
1d range count. How many keys between lo and hi ? number of keys < E
rank(E) = 2
rank 6
rank(F) = 3
2 S 7 rank(S) = 6
1 E 5 X rank(T) = 7
A 0 3 R size(E, S) = 5
C H 4 size(E, T) = 5
M size(F, T) = 4
‣ 1d range search
‣ kd trees
‣ interval search trees
Algorithms
‣ line segment intersection
Geometric interpretation.
Keys are point in the plane.
Find/count points in a given h-v rectangle
rectangle is axis-aligned
10
Binary search tree?
Tree construction
What order to store points?
– X coordinate determines order?
– Y coordinate determines order?
E R
11
2d orthogonal range search: grid implementation
Grid implementation.
Divide space into M-by-M grid of squares.
Create list of points contained in each square.
Use 2d array to directly index relevant square.
Insert: add (x, y) to list for corresponding square.
Range search: examine only squares that intersect 2d range query.
LB
12
2d orthogonal range search: grid implementation analysis
Space-time tradeoff.
Space: M 2 + N.
Time: 1 + N / M 2 per square examined, on average.
LB
13
Clustering
14
Clustering
Applications.
Ray tracing.
2d range search.
Flight simulators.
N-body simulation.
Collision detection.
Astronomical databases.
Nearest neighbor search.
Adaptive mesh generation.
Accelerate rendering in Doom.
Hidden surface removal and shadow casting.
8
1
6 3 2
9
4 6 7 8
1
5 5 10 9
2
10
18
Your turn
Draw the KdTree and matching graph when the following points are inserted:
(2,3) (4, 2) (4, 4) (3, 3) (1, 5)
(2, 3)
19
2d tree implementation
q
p
q
p
points points points points
left of p right of p below q above q
even levels odd levels
6 1
9
3 3 2
5 1
2
4 6 7 8
7
5 10 9
10
20
2d tree construction
8
1
6 3 2
9
4 6 7 8
1
5 5 10 9
2
10
10
21
Range search in a 2d tree demo
8
1
6 3 2
9
4 6 7 8
1
5 5 10 9
2
10
22
Range search in a 2d tree demo
8
1
6 3 2
9
4 6 7 8
1
5 5 10 9
2
10
4 done
23
Range search in a 2d tree analysis
8
1
6 3 2
9
4 6 7 8
1
5 5 10 9
2
10
24
Nearest neighbor search in a 2d tree demo
8
1
6 3 2
9
query point
3
4 6 7 8
1
5 5 10 9
2
10
25
Nearest neighbor search in a 2d tree demo
8
1
6 3 2
9
4 6 7 8
3
1
5 5 10 9
2
10
4 nearest neighbor = 5
26
Nearest neighbor search in a 2d tree analysis
8
1
6 3 2
9
4 6 7 8
3
1
5 5 10 9
2
10
4 nearest neighbor = 5
27
30
Flocking birds
http://www.youtube.com/watch?v=XH-groCeKbE
28
Flocking boids [Craig Reynolds, 1986]
29
Kd tree
level ≡ i (mod k)
points points
whose ith whose ith
coordinate coordinate
is less than p’s is greater than p’s
G m1 m2
Brute force. For each pair of particles, compute force: F =
r2
Running time. Time per step is N 2.
http://www.youtube.com/watch?v=ua7YlN4eL_w
31
Appel's algorithm for N-body simulation
Key idea. Suppose particle is far, far away from cluster of particles.
Treat cluster of particles as a single aggregate particle.
Compute force between particle and center of mass of aggregate.
32
Appel's algorithm for N-body simulation
SIAM J. ScI. STAT. COMPUT. 1985 Society for Industrial and Applied Mathematics
Vol. 6, No. 1, January 1985 O08
Abstract. The simulation of N particles interacting in a gravitational force field is useful in astrophysics,
but such simulations become costly for large N. Representing the universe as a tree structure with the
particles at the leaves and internal nodes labeled with the centers of mass of their descendants allows several
simultaneous attacks on the computation time required by the problem. These approaches range from
algorithmic changes (replacing an O(N ’) algorithm with an algorithm whose time-complexity is believed
to be O(N log N)) to data structure modifications, code-tuning, and hardware modifications. The changes
reduced the running time of a large problem (N 10,000) by a factor of four hundred. This paper describes
both the particular program and the methodology underlying such speedups.
‣ 1d range search
‣ kd trees
‣ interval search trees
Algorithms
‣ line segment intersection
35
1d interval search API
void put(Key lo, Key hi, Value val) put interval-value pair into ST
Value get(Key lo, Key hi) value paired with given interval
(17, 19) 24
max endpoint in
subtree rooted at node
(7, 10) 10
37
Interval search tree demo
(17, 19) 24
(7, 10) 10
38
Interval search tree demo
To search for any one interval that intersects query interval ( lo, hi ) :
If interval in node intersects query interval, return it.
Else if left subtree is null, go right.
Else if max endpoint in left subtree is less than lo, go right.
Else go left.
39
Search for an intersecting interval implementation
To search for any one interval that intersects query interval ( lo, hi ) :
If interval in node intersects query interval, return it.
Else if left subtree is null, go right.
Else if max endpoint in left subtree is less than lo, go right.
Else go left.
Node x = root;
while (x != null)
{
if (x.interval.intersects(lo, hi)) return x.interval;
else if (x.left == null) x = x.right;
else if (x.left.max < lo) x = x.right;
else x = x.left;
}
return null;
40
Search for an intersecting interval analysis
To search for any one interval that intersects query interval ( lo, hi ) :
If interval in node intersects query interval, return it.
Else if left subtree is null, go right.
Else if max endpoint in left subtree is less than lo, go right.
Else go left.
Pf. Suppose search goes right and left subtree is non empty.
Since went right, we have max < lo.
For any interval (a, b) in left subtree of x,
we have b ≤ max < lo. max
(c, max)
definition of max reason for going right
(a, b) (lo, hi)
Thus, (a, b) will not intersect ( lo, hi ).
To search for any one interval that intersects query interval ( lo, hi ) :
If interval in node intersects query interval, return it.
Else if left subtree is null, go right.
Else if max endpoint in left subtree is less than lo, go right.
Else go left.
interval best
operation brute
search tree in theory
43
G EOMETRIC A PPLICATIONS OF BST S
‣ 1d range search
‣ kd trees
‣ interval search trees
Algorithms
‣ line segment intersection
3 3
2 2
1 1
0 0
y-coordinates 46
Orthogonal line segment intersection: sweep-line algorithm
3 3
1 1
0 0
y-coordinates 47
Orthogonal line segment intersection: sweep-line algorithm
3 3
1d range
4
search
2
1 1
0 0
y-coordinates 48
Orthogonal line segment intersection: sweep-line analysis
Pf.
Put x-coordinates on a PQ (or sort). N log N
‣ 1d range search
‣ kd trees
‣ interval search trees
Algorithms
‣ line segment intersection
Design-rule checking.
Certain wires cannot intersect.
Certain spacing needed between different types of wires.
Debugging = orthogonal rectangle intersection search.
Gordon Moore
quadratic 2x-faster
algorithm computer
2 3 2
0 0
1 1
y-coordinates
54
Orthogonal rectangle intersection: sweep-line analysis
Pf.
Put x-coordinates on a PQ (or sort). N log N
56