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
kkk1 and k2k1 and k2Application: 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
keys(G, K) => { H, I }
size(G, 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 (size) as a function of \(N\) and \(R\), where \(N\) is number of keys and \(R\) is number of matching keys?
\(\log R\)
\(\log N\)
\(R + \log N\)
\(R + N\)
Ordered array: slow insert; fast range search
Unordered list: fast insert; slow range search
| data structure | insert | range count | range search |
|---|---|---|---|
| ordered array | \(N\) | \(\log N\) | \(R + \log N\) |
| unordered list | \(1\) | \(N\) | \(N\) |
| goal | \(\log N\) | \(\log N\) | \(R + \log N\) |
\(N\) is number of keys
\(R\) is number of keys that match
assuming distinct keys
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 }
|
![]() |
public Iterable<Key> keys(Key lo, Key hi) {
Queue<Key> queue = new Queue<Key>();
keys(root, queue, lo, hi);
return queue;
}
private void keys(Node node, Queue<Key> queue, Key lo, Key hi) {
if (node == null) return;
int cmplo = lo.compareTo(node.key);
int cmphi = hi.compareTo(node.key);
if (cmplo < 0) keys(node.left, queue, lo, hi);
if (cmplo <= 0 && cmphi >= 0) queue.enqueue(node.key);
if (cmphi > 0) keys(node.right, queue, lo, hi);
}
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: fast 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 | \(1\) | \(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: How much money/time do I need to get the job done with a quadratic algorithm?
Running time:
\[\begin{array}{llll} \text{today:} & T(N) & = & a N^2 \\ \text{in 2 years:} & T(2N) & = & (a/2)(2N)^2 = 2aN^2 = 2T(N) \end{array}\]
Would need 2x money/time to solve quadratic problems!
| alg run time | 1970 | 1972 | 1974 | 1976 | ... | 2000 | 2030 |
|---|---|---|---|---|---|---|---|
| \(N\) | \(x\) | \(x\) | \(x\) | \(x\) | ... | \(x\) | \(x\) |
| \(N \log N\) | \(x\) | \(\sim x\) | \(\sim x\) | \(\sim x\) | ... | \(\sim x\) | \(\sim x\) |
| \(N^2\) | \(x\) | \(2x\) | \(4x\) | \(8x\) | ... | \(2^{15}x\) | \(2^{30}x\) |
\(x\): cost in dollars
Bottom line: Linearithmic alg is necessary to sustain Moore's law
Nondegeneracy assumption: All x- and y- coordinates are distinct

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:
| Put x-coordinate on a PQ (or sort) | \(N \log N\) |
| Insert y-coordinate into BST | \(N \log N\) |
| Delete y-coordinate from BST | \(N \log N\) |
| Range searches is BST | \(N \log N + R\) |
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
(x,y) to list for corresponding squareSpace-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
| Grid | divide space uniformly into squares |
| Quadtree | recursively divide space into four quadrants |
| 2D tree | recursively divide into two axis-aligned halfplanes |
| BSP tree | recursively divide into two arbitrary halfplanes |
![]() |
![]() |
![]() |
![]() |
Recursively partition plane into two halfplanes


Note
x increases going right, and y increases going up
Data structure: BST, but alternate using \(x\)- and \(y\)-coordinate as key


Where would point K be inserted in the \(k\)-d tree below?
|
x increases going 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?
|
D. I don't know |
Typical case: \(\log N\)
Worst case (even if tree is balanced): \(N\)


\(k\)-d tree: Recursively partition \(k\)-dim space into 2 halfspaces
Implementation: BST, but cycle through dimensions as in 2D trees
dim[0], dim[1], ..., dim[k-1], dim[0], dim[1], ...

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
Proof:
| Put \(x\)-coordinates on a PQ or sort | \(N \log N\) |
| Insert \(y\)-intervals into ST | \(N \log N\) |
| Delete \(y\)-intervals from ST | \(N \log N\) |
| Interval searches for \(y\)-intervals | \(N \log N + R \log N\) |
Bottom line: Sweep line reduces 2D orthogonal rectangle intersection search to 1D interval search