This lecture: intersections among geometric objects
Applications: CAD, games, movies, virtual reality, databases, GIS, ...
Efficient solutions: Binary search trees (and extensions)
This lecture is only the tip of the iceberg!
See COS 350 and COS 424!
Extension of ordered symbol table
k
k
k1
and k2
k1
and k2
Application: database queries
insert B B insert D B D insert A A B D 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 search G to K H I count G to K 2
Geometric interpretation
Suppose that the keys are stored in a sorted array. What is the order of growth of the running time to perform range count as a function of \(N\) and \(R\), where \(N\) is number of keys and \(R\) is number of matching keys?
A. \(\log R\)
B. \(\log N\)
C. \(\log N + R\)
D. \(N + R\)
E. I don't know
Ordered array: slow insert; fast range search
Unordered list: slow insert; slow range search
data structure | insert | range count | range search |
---|---|---|---|
ordered array | \(N\) | \(\log N\) | \(R + \log N\) |
unordered list | \(N\) | \(N\) | \(N\) |
goal | \(\log N\) | \(\log N\) | \(R + \log N\) |
\(N\) is number of keys; \(R\) is number of keys that match
1D range count: how many keys between lo
and hi
, inclusively?
public int size(Key lo, Key hi) { int rhi = rank(hi); int rlo = rank(lo); if(contains(hi)) return rhi-rlo+1; else return rhi-rlo; // num of keys < hi } |
![]() |
Proposition: running time proportional to \(\log N\), assuming BST is balanced.
Pf: Nodes examined = search path to lo
+ search path to hi
1D range search: find all keys between lo
and hi
keys(E,Q) => {E, G, H, L, M, P}
Proposition: running time proportional to \(R + \log N\)
Proof: Nodes examined = search path to lo
+ search path to hi
+ matches
Ordered array: slow insert; fast range search
Unordered list: slow insert, slow range search
BST: fast insert, fast range search
data structure | insert | range count | range search |
---|---|---|---|
ordered array | \(N\) | \(\log N\) | \(R + \log N\) |
unordered list | \(N\) | \(N\) | \(N\) |
goal | \(\log N\) | \(\log N\) | \(R + \log N\) |
\(N\) is number of keys; \(R\) is number of keys that match
Goal: Insert intervals (left,right
) and support queries of the form "how many intervals contain x
?"
Given \(N\) horizontal and vertical line segments, find all intersections
Quadratic algorithm: Check all pairs of line segments for intersections
Early 1970s: microprocessor design became a geometric problem
Design-rule checking
Moore's law (1965): Transistor count doubles every 2 years
Sustaining Moore's law
Running time today: \(T_N = a N^2\)
Running time in 2 years: \(T_{2N} = (a/2)(2N)^2 = 2T_N\)
running time | 1970 | 1972 | 1974 | 2000 |
---|---|---|---|---|
\(N\) | $ \(x\) | $ \(x\) | $ \(x\) | $ \(x\) |
\(N \log N\) | $ \(x\) | = $ \(x\) | = $ \(x\) | = $ \(x\) |
\(N^2\) | $ \(x\) | $ \(2x\) | $ \(4x\) | $ \(2^nx\) |
where \(n = 15\)
Bottom line: Linearithmic algorithm is necessary to sustain Moore's law
Nondegeneracy assumption: All x- and y- coordinates are distinct
Sweep vertical line from left to right
Sweep vertical line from left to right
Sweep vertical line from left to right
Proposition: the sweep-line algorithm takes time proportional to \(N \log N + R\) to find all \(R\) intersections among \(N\) orthogonal line segments
Proof:
Bottom line: sweep line reduces 2D orthogonal line segment intersection search to 1D range search
The sweep-line algorithm is a key technique in computation geometry
Geometric intersection
More problems
Extension of ordered symbol-table to 2D keys
Applications: Networking, circuit design, databases, ...
Geometric interpretation
Grid implementation
|
![]() |
Space-time tradeoff
Choose grid square size to tune performance
Running time (if points are evenly distributed)
grid implementation: fast, simple solution for evenly-distributed points
Problem: Clustering is a well-known phenomenon in geometric data
grid implementation: fast, simple solution for evenly-distributed points
Problem: Clustering is a well-known phenomenon in geometric data
Example: USA Map data
13,000 points, 1000 grid squares: half the squares are empty, and half the points are in 10% of the squares
Use a tree to represent a recursive subdivision of 2D space
Applications
|
![]()
|
Recursively partition plane into two halfplanes
Data structure: BST, but alternate using \(x\)- and \(y\)-coordinate as key
Where would point 11 be inserted in the \(k\)-d tree below?
A. Right child of F |
Where would point 11 be inserted in the \(k\)-d tree below?
A. Right child of F |
F is LR node, so right = right |
Goal: Find all points in a query axis-aligned rectangle
Typical case: \(R + \log N\)
Worst case (assuming tree is balanced): \(R + \sqrt{n}\)
Goal: Find closest point to query point
Which of the following is the worst case for nearest neighbor search?
A. B. |
C. D. I don't know |
Typical case: \(\log N\)
Worst case (even if tree is balanced): \(N\)
\(k\)-d tree: Recursively partition \(k\)-dimensional space into 2 halfspaces
Implementation: BST, but cycle through dimensions as in 2D trees
Efficient, simple data structure for processing \(k\)-dimensional data
Q. What "natural algorithm" do starlings, migrating geese, cranes, bait balls of fish, and flashing fireflies use to flock?
Boids: 3 simple rules lead to complex emergent flocking behavior:
Goal: simulate motion of \(N\) particles, mutually affected by gravity
Brute force: for each pair of particles, compute force: \(F = \frac{Gm_1m_2}{r^2}\)
Running time: time per step is \(N^2\)
Key idea: Suppose particle is far, far away from cluster of particles
Impact: running time per step is \(N \log N\), enabling new research!
problem | example | solution |
---|---|---|
1d range search | ![]() |
binary search tree |
2d ortho line segment intersection | ![]() |
sweep line reduces problem to 1d range search |
2d range search, kd range search | ![]() |
2d tree, kd tree |
Goal: Find all intersections among a set of \(N\) orthogonal rectangles
Quadratic algorithm: Check all pairs of rectangles for intersections
Non-degeneracy assumption: all \(x\)- and \(y\)-coordinates are distinct
Sweep vertical line from left to right
Proposition: sweep line algorithm takes time proportional to \(N \log N + R \log N\) to find \(R\) intersections among a set of \(N\) rectangles
Pf:
Bottom line: Sweep line reduces 2D orthogonal rectangle intersection search to 1D interval search
problem | example | solution |
---|---|---|
1d range search | ![]() |
binary search tree |
2d ortho line segment intersection | ![]() |
sweep line reduces problem to 1d range search |
2d range search, kd range search | ![]() |
2d tree, kd tree |
2d ortho rectangle intersection | ![]() |
sweep line reduces problem to 1d interval search |