fullscreen
timer
qrcode
plickers
selector
edit
reset

Geometric Applications of BSTs

COS 265 - Data Structures & Algorithms

overview

This lecture: intersections among geometric objects



Applications: CAD, games, movies, virtual reality, databases, GIS, ...

Efficient solutions: Binary search trees (and extensions)

overview




This lecture is only the tip of the iceberg!

See COS 350 and COS 424!

Geometric Applications of BSTs

1D range search

1d range search

Extension of ordered symbol table

Application: database queries

1d range search

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

Quiz 1: Geometric Applications of BSTs

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?


  1. \(\log R\)

  2. \(\log N\)

  3. \(R + \log N\)

  4. \(R + N\)

1d range search: elementary impl

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\)

1d range count: BST implementation

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: BST implementation

1D range search: find all keys between lo and hi

1d range search: BST implementation

keys(E,Q) => {E, G, H, L, M, P}

1d range search: BST implementation

Proposition: running time proportional to \(R + \log N\)

Proof: Nodes examined = search path to lo + search path to hi + matches

1d range search: summary of performance

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\)

Exercise: interval stabbing query

Goal: Insert intervals ((left,right)) and support queries of the form "how many intervals contain x?"

Geometric applications of bsts

line segment intersection

orthogonal line segment intersection

Given \(N\) horizontal and vertical line segments, find all intersections

Quadratic algorithm: Check all pairs of line segments for intersections

microprocessors and geometry

Early 1970s: microprocessor design became a geometric problem

Design-rule checking

algorithms and moore's law

Moore's law (1965): Transistor count doubles every 2 years

Gordon Moore
Gordon Moore

Sustaining Moore's law

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!

Sustaining Moore's law

alg run time 1970 1972 1974 2000
\(N\) $ \(x\) $ \(x\) $ \(x\) $ \(x\)
\(N \log N\) $ \(x\) \(\sim\) $ \(x\) \(\sim\) $ \(x\) \(\sim\) $ \(x\)
\(N^2\) $ \(x\) $ \(2x\) $ \(4x\) $ \(2^nx\)

where \(n = 15\)


Bottom line: Linearithmic alg is necessary to sustain Moore's law

orthogonal line segment intersection: sweep-line algorithm

Nondegeneracy assumption: All x- and y- coordinates are distinct

orthogonal line segment intersection: sweep-line algorithm

Sweep vertical line from left to right

orthogonal line segment intersection: sweep-line algorithm

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

sweep-line algorithm: context

The sweep-line algorithm is a key technique in computation geometry

Geometric intersection

sweep-line algorithm: context

More problems

geometric applications of bsts

\(k\)-d trees

2D orthogonal range search

Extension of ordered symbol-table to 2D keys

Applications: Networking, circuit design, databases, ...

2D orthogonal range search

Geometric interpretation

2D ortho search: grid implementation

Grid implementation

2D ortho search: grid impl. analysis

Space-time tradeoff

Choose grid square size to tune performance

2D ortho search: grid impl. analysis

Running time (if points are evenly distributed)

clustering

grid implementation: fast, simple solution for evenly-distributed points

Problem: Clustering is a well-known phenomenon in geometric data

clustering

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

space-partitioning trees

Use a tree to represent a recursive subdivision of 2D space


space-partitioning trees: applications

Applications

  • Ray tracing
  • 2D range search
  • Flight simulators
  • N-body simulation
  • Collision detection
  • Astronomical databases
  • Nearest neighbor search
  • Adaptive mesh generation
  • Acceleration rendering in Doom
  • Hidden surface removal and shadow casting

2d tree construction

Recursively partition plane into two halfplanes




Note

x increases going right, and y increases going up

2d tree implementation

Data structure: BST, but alternate using \(x\)- and \(y\)-coordinate as key

quiz 2: Geometric Applications of BSTs

Where would point K be inserted in the \(k\)-d tree below?

  1. Right child of F (x node)
  2. Left child of G (x node)
  3. Left child of J (y node)
  4. Right child of J (y node)

x increases going right
y increases going up

2d tree demo: range search

Goal: Find all points in a query axis-aligned rectangle

range search in a 2d tree analysis

Typical case: \(R + \log N\)

Worst case (assuming tree is balanced): \(R + \sqrt{N}\)

2d tree demo: nearest neighbor

Goal: Find closest point to query point

Quiz 3: Geometric Applications of BSTs

Which of the following is the worst case for nearest neighbor search?

C.




D. I don't know

nn search in a 2d tree analysis

Typical case: \(\log N\)

Worst case (even if tree is balanced): \(N\)

\(k\)-d tree

\(k\)-d tree: Recursively partition \(k\)-dim space into 2 halfspaces

Implementation: BST, but cycle through dimensions as in 2D trees

\(k\)-d tree

Efficient, simple data structure for processing \(k\)-dimensional data

Jon Bentley
Jon Bentley

flocking birds

Q. What "natural algorithm" do starlings, migrating geese, cranes, bait balls of fish, and flashing fireflies use to flock?

flocking boids (craig reynolds, 1986)

Boids: 3 simple rules lead to complex emergent flocking behavior:

\(N\)-body simulation

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\)

appel's algorithm for \(N\)-body simulation

Key idea: Suppose particle is far, far away from cluster of particles

appel's algorithm for \(N\)-body simulation

Impact: running time per step is \(N \log N\), enabling new research!

geometric applications of BSTs

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

orthogonal rectangle intersection

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

ortho rect intersection: sweep-line algo

Sweep vertical line from left to right

ortho rect intersection: sweep-line analysis

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:


Bottom line: Sweep line reduces 2D orthogonal rectangle intersection search to 1D interval search

geometric applications of BSTs

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
×