Download Geometric Algorithms: Range Searching and Intersection in Multi-Dimensional Spaces and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
Geometric Algorithms
2
Geometric search: Overview
Types of data. Points, lines, planes, polygons, circles, ...
This lecture. Sets of N objects.
Geometric problems extend to higher dimensions.
! Good algorithms also extend to higher dimensions.
! Curse of dimensionality.
Basic problems.
! Range searching.
! Nearest neighbor.
! Finding intersections of geometric objects.
3
7. 3 Range Searching
4
1 D Range Search
Extension to symbol-table ADT with comparable keys.
! Insert key-value pair.
! Search for key k.
! How many records have keys between k 1 and k 2?
! Iterate over all records with keys between k 1 and k 2.
Application: database queries.
Geometric intuition.
! Keys are point on a line.
! How many points in a given interval?
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 count G to K 2 search G to K H I
5
1 D Range Search Implementations
Range search. How many records have keys between k 1 and k 2?
Ordered array. Slow insert, binary search for k 1 and k 2 to find range.
Hash table. No reasonable algorithm (key order lost in hash).
BST. In each node x, maintain number of nodes in tree rooted at x.
Search for smallest element! k 1 and largest element " k 2.
log N N log N insert count range ordered array N R + log N hash table 1 N BST log N R + log N nodes examined within interval not touched N = # records R = # records that match 6
2 D Orthogonal Range Search
Extension to symbol-table ADT with 2D keys.
! Insert a 2D key.
! Search for a 2D key.
! Range search: find all keys that lie in a 2D range?
! Range count: how many keys lie in a 2D range?
Applications: networking, circuit design, databases.
Geometric interpretation.
! Keys are point in the plane.
! Find all points in a given h-v rectangle?
7
2 D Orhtogonal Range Search: Grid Implementation
Grid implementation. [Sedgewick 3 .18]
! Divide space into M-by-M grid of squares.
! Create linked list for each square.
! Use 2D array to directly access relevant square.
! Insert: insert (x, y) into corresponding grid square.
! Range search: examine only those grid squares that could have
points in the rectangle.
LB RT 8
2 D Orthogonal Range Search: Grid Implementation Costs
Space-time tradeoff.
! Space: M^2 + N.
! Time: 1 + N / M^2 per grid cell examined on average.
Choose grid square size to tune performance.
! Too small: wastes space.
! Too large: too many points per grid square.
! Rule of thumb:! N by! N grid.
Running time. [if points are evenly distributed]
! Initialize: O(N).
! Insert: O(1).
! Range: O(1) per point in range.
LB RT
13
2 D Trees
2D tree. Recursively partition plane into 2 halfplanes.
Implementation: BST, but alternate using x and y coordinates as key.
! Search gives rectangle containing point.
! Insert further subdivides the plane.
even levels q p le^ pfotin otfs p ri^ pgohitn otsf p points below q a^ pbooinvtes q odd levels p q 14
kD Trees
kD tree. Recursively partition k-dimensional space into 2 halfspaces.
Implementation: BST, but cycle through dimensions ala 2D trees.
Efficient, simple data structure for processing k-dimensional data.
! Adapts well to clustered data.
! Adapts well to high dimensional data.
! Discovered by an undergrad in an algorithms class!
level! i (mod k) points c^ wohoorsdien^ aitthe is less than p’s w^ phooisnet sith is gr^ ceooatredrin tahtaen p’s p 15
Summary
Basis of many geometric algorithms: search in a planar subdivision.
grid 2 D tree (^) dVioargornaomi^ inter lisneecsting basis #N h-v lines N points N points #N lines representation (^2) ofD N^ a rlrisatys N-node BST (^) mNu-lntoidliset ~N-node BST cells ~N squares N rectangles N polygons ~N triangles search cost 1 log N log N log N extend to kD? too many cells easy (^) cocmelpllsi^ ctaotoed h^ uyspe^ e(rkp-l^1 a)nDe 16
7. 4 Geometric Intersection
17
Geometric Intersection
Problem. Find all intersecting pairs among set of N geometric objects.
Applications. CAD, games, movies, virtual reality.
Simple version: 2 D, all objects are horizontal or vertical line segments.
Brute force. Test all $(N^2 ) pairs of line segments for intersection.
Sweep line. Efficient solution extends to 3D and general objects.
18
Sweep vertical line from left to right.
! Event times: x-coordinates of h-v line segments.
! Left endpoint of h-segment: insert y coordinate into ST.
! Right endpoint of h-segment: remove y coordinate from ST.
! v-segment: range search for interval of y endpoints.
Orthogonal Segment Intersection: Sweep Line Algorithm
insert y range search delete y 19
Orthogonal Segment Intersection: Sweep Line Algorithm
Sweep line: reduces 2D orthogonal segment intersection problem to
1D range searching!
Running time of sweep line algorithm.
! Put x-coordinates on a PQ (or sort). O(N log N)
! Insert y-coordinate into SET. O(N log N)
! Delete y-coordinate from SET. O(N log N)
! Range search. O(R + N log N)
Efficiency relies on judicious use of data structures.
N = # line segments R = # intersections 20
Immutable H-V Segment ADT
public final class SegmentHV implements Comparable { public final int x 1 , y 1 ; public final int x 2 , y 2 ; public SegmentHV(int x 1 , int y1, int x 2 , int y 2 ) public boolean isHorizontal() { … } public boolean isVertical() { … } public int compareTo(SegmentHV b) { … } public String toString() { … } } (^) compare by y-coordinate; break ties by x-coordinate (x1, y1) (x2, y1) horizontal segment vertical segment (x1, y1) (x1, y2)
25
Line Segment Intersection: Implementation
Efficient implementation of sweep line algorithm.
! Maintain PQ of important x-coordinates: endpoints and intersections.
! Maintain ST of segments intersecting sweep line, sorted by y.
! O(R log N + N log N).
Implementation issues.
! Degeneracy.
! Floating point precision.
! Use PQ since intersection events aren't known ahead of time.
26
VLSI Rules Checking
27
Algorithms and Moore's Law
Rectangle intersection. Find all intersections among h-v rectangles.
Application. VLSI rules checking in microprocessor design.
Early 1970s: microprocessor design became a geometric problem.
! Very Large Scale Integration (VLSI).
! Computer-Aided Design (CAD).
! Design-rule checking.
28
Algorithms and Moore's Law
"Moore’s Law." Processing power doubles every 18 months.
! 197x: need to check N rectangles.
! 197(x+1.5): need to check 2N rectangles on a 2x-faster computer.
Quadratic algorithm. Compare each rectangle against all others.
! 197x: takes M days.
! 197(x+1.5): takes (4M)/2 = 2M days. (!)
Need O(N log N) CAD algorithms to sustain Moore’s Law.
29
VLSI Database Problem
Move a vertical "sweep line" from left to right.
! Sweep line: sort rectangles by x-coordinate and process in this order,
stopping on left and right endpoints.
! Maintain set of intervals intersecting sweep line.
! Key operation: given a new interval, does it intersect one in the set?
30
Support following operations.
! Insert an interval (lo, hi).
! Delete the interval (lo, hi).
! Search for an interval that intersects (lo, hi).
Non-degeneracy assumption. No intervals have the same x-coordinate.
Interval Search Trees
(7, 10) (5, 11) (4, 8) (15, 18) (17, 19) (20, 22) 31
Interval Search Trees
Interval tree implementation with BST.
! Each BST node stores one interval.
! BST nodes sorted on lo endpoint.
( 7 , 10 ) ( 5 , 11 ) ( 4 , 8 ) ( 15 , 18 ) ( 17 , 19 ) ( 20 , 22 ) (4, 8) (17, 19) (5, 11) (20, 22) (15, 18) (7, 10) 32
Interval Search Trees
Interval tree implementation with BST.
! Each BST node stores one interval.
! BST nodes sorted on lo endpoint.
! Additional info: store and maintain
max endpoint in subtree rooted at node.
max in subtree (4, 8) (17, 19) (5, 11) (20, 22) (15, 18) (7, 10) 22 18 22 18 10 8 ( 7 , 10 ) ( 5 , 11 ) ( 4 , 8 ) ( 15 , 18 ) ( 17 , 19 ) ( 20 , 22 )
37
VLSI Database Problem: Sweep Line Algorithm
Sweep line: reduces 2D orthogonal rectangle intersection problem to 1D
interval searching!
Running time of sweep line algorithm.
! Sort by x-coordinate. O(N log N)
! Insert y-interval into ST. O(N log N)
! Delete y-interval from ST. O(N log N)
! Interval search. O(R log N)
Efficiency relies on judicious extension of BST.
N = # line segments R = # intersections