Documente Academic
Documente Profesional
Documente Cultură
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
F O U R T H
E D I T I O N
kd trees
interval search trees
rectangle intersection
OF
BST S
Overview
This lecture. Intersections among geometric objects.
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
1d range search
Extension of ordered symbol table.
insert B
insert D
B D
insert A
A B D
Geometric interpretation.
insert I
A B D I
insert H
A B D H I
insert F
A B D F H I
insert P
A B D F H I P
count G to K
search G to K
H I
data structure
insert
range count
range search
unordered list
ordered array
log N
R + log N
goal
log N
log N
R + log N
N = number of keys
R = number of keys that match
R
4
M
public int size(Key lo, Key hi)
{
if (contains(hi)) return rank(hi) - rank(lo) + 1;
else
return rank(hi) - rank(lo);
}
number of keys < hi
Recursively find all keys in left subtree (if any could fall in range).
Check key in current node.
Recursively find all keys in right subtree (if any could fall in range).
searching in the range [F..T]
R
C
H
M
L
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
4
2
1
2
1
y-coordinates
11
4
2
1
y-coordinates
12
1d range
search
4
2
1
y-coordinates
13
N log N
N log N
N log N
N log N + R
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
Insert a 2d key.
Delete a 2d key.
Search for a 2d key.
Range search: find all keys that lie in a 2d range.
Range count: number of keys that lie in a 2d range.
Applications. Networking, circuit design, databases, ...
Geometric interpretation.
17
RT
LB
18
Space: M + N.
Time: 1 + N / M
2
RT
19
Clustering
Grid implementation. Fast, simple solution for evenly-distributed points.
Problem. Clustering a well-known phenomenon in geometric data.
20
Clustering
Grid implementation. Fast, simple solution for evenly-distributed points.
Problem. Clustering a well-known phenomenon in geometric data.
Ex. USA map data.
21
Space-partitioning trees
Use a tree to represent a recursive subdivision of 2d space.
Grid. Divide space uniformly into squares.
2d tree. Recursively divide space into two halfplanes.
Quadtree. Recursively divide space into four quadrants.
BSP tree. Recursively divide space into two regions.
Grid
2d tree
Quadtree
BSP tree
22
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.
Grid
2d tree
Quadtree
BSP tree
23
2d tree construction
Recursively partition plane into two halfplanes.
8
1
9
3
4
1
5
10
10
4
24
2d tree implementation
Data structure. BST, but alternate using x- and y-coordinates as key.
q
p
points
points
left of p
right of p
points
points
below q
above q
odd levels
even levels
8
1
3
1
2
4
7
5
10
10
4
25
3
4
1
5
10
10
4
26
3
4
1
5
10
10
4
done
27
8
1
3
4
1
5
10
10
4
28
8
1
query point
3
4
1
5
10
10
4
29
1
5
10
10
4
nearest neighbor = 5
30
8
1
1
5
10
10
4
nearest neighbor = 5
31
30
Flocking birds
Q. What "natural algorithm" do starlings, migrating geese, starlings,
cranes, bait balls of fish, and flashing fireflies use to flock?
http://www.youtube.com/watch?v=XH-groCeKbE
32
33
Kd tree
Kd tree. Recursively partition k-dimensional space into 2 halfspaces.
Implementation. BST, but cycle through dimensions ala 2d trees.
p
level
i (mod k)
points
points
whose ith
whose ith
coordinate
coordinate
is less than ps
is greater than ps
Widely used.
Adapts well to high-dimensional and clustered data.
Discovered by an undergrad in an algorithms class!
Jon Bentley
34
N-body simulation
Goal. Simulate the motion of N particles, mutually affected by gravity.
Brute force. For each pair of particles, compute force: F =
Running time. Time per step is N 2.
G m1 m2
r2
http://www.youtube.com/watch?v=ua7YlN4eL_w
35
36
Impact.
37
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
1d interval search
1d interval search. Data structure to hold set of (overlapping) intervals.
(7, 10)
(21, 24)
(17, 19)
(5, 8)
(4, 8)
(15, 18)
40
(17, 19)
(5, 8)
(4, 8)
(21, 24)
18
(15, 18)
24
24
18
max endpoint in
subtree rooted at node
(7, 10)
10
42
(5, 8)
(4, 8)
(7, 10)
(21, 24)
18
(15, 18)
24
24
18
10
43
(17, 19)
24
(4, 8)
(15, 18)
(21, 24)
22
24
22
compare (21, 23) to (16, 22)
(intersection!)
(7, 10)
10
(16, 22)
22
(21, 23)
44
Node x = root;
while (x != null)
{
if
x = x.right;
x = x.right;
else
x = x.left;
}
return null;
45
max
(c, max)
(a, b)
left subtree of x
(lo, hi)
right subtree of x
46
intervals sorted
by left endpoint
max
(c, max)
(lo, hi)
left subtree of x
(a, b)
right subtree of x
47
operation
brute
interval
search tree
best
in theory
insert interval
log N
log N
find interval
log N
log N
delete interval
log N
log N
log N
log N
R log N
R + log N
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
3
2
52
2x-faster
computer
3
2
y-coordinates
54
N log N
N log N
N log N
N log N + R log N
example
solution
1d range search
BST
2d orthogonal line
segment intersection
kd range search
kd tree
1d interval search
2d orthogonal
rectangle intersection
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
kd trees
interval search trees
rectangle intersection
OF
BST S
Algorithms
G EOMETRIC A PPLICATIONS
1d range search
line segment intersection
Algorithms
F O U R T H
E D I T I O N
kd trees
interval search trees
rectangle intersection
OF
BST S