Download Geometric Algorithms: Convex Hull, Closest Pair, and Voronoi Diagram and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
Geometric Algorithms
2
Geometric Algorithms
Applications.
! Data mining.
! VLSI design.
! Computer vision.
! Mathematical models.
! Astronomical simulation.
! Geographic information systems.
! Computer graphics (movies, games, virtual reality).
! Models of physical world (maps, architecture, medical imaging).
History.
! Ancient mathematical foundations.
! Most geometric algorithms less than 25 years old.
Reference: http://www.ics.uci.edu/~eppstein/geom.html airflow around an aircraft wing 3
Geometric Primitives
Point: two numbers (x, y).
Line: two numbers a and b [ax + by = 1]
Line segment: two points.
Polygon: sequence of points.
Primitive operations.
! Is a point inside a polygon?
! Compare slopes of two lines.
! Distance between two points.
! Do two line segments intersect?
! Given three points p 1 , p 2 , p 3 , is p 1 - p 2 - p 3 a counterclockwise turn?
Other geometric shapes.
! Triangle, rectangle, circle, sphere, cone, …
! 3D and higher dimensions sometimes more complicated.
any line not through origin 4
Intuition
Warning: intuition may be misleading.
! Humans have spatial intuition in 2D and 3D.
! Computers do not.
! Neither has good intuition in higher dimensions!
Is a given polygon simple?
we think of this algorithm sees this 1 6 5 8 7 2 7 8 6 4 2 1 1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 18 4 18 4 19 4 19 4 20 3 20 3 20 1 10 3 7 2 8 8 3 4 6 5 15 1 11 3 14 2 16 no crossings
5
Polygon Inside, Outside
Jordan curve theorem. [Veblen 1 905] Any continuous simple closed
curve cuts the plane in exactly two pieces: the inside and the outside.
Is a point inside a simple polygon?
Application. Draw a filled polygon on the screen.
http://www.ics.uci.edu/~eppstein/geom.html 6 public boolean contains(double x 0 , double y 0 ) { int crossings = 0 ; for (int i = 0 ; i < N; i++) { double slope = (y[i+ 1 ] - y[i]) / (x[i+ 1 ] - x[i]); boolean cond 1 = (x[i] <= x 0 ) && (x 0 < x[i+ 1 ]); boolean cond 2 = (x[i+ 1 ] <= x 0 ) && (x 0 < x[i]); boolean above = (y 0 < slope * (x 0 - x[i]) + y[i]); if ((cond 1 || cond 2 ) && above) crossings++; } return (crossings % 2 != 0 ); }
y = yi +^1 "^ yi xi + 1 " xi ( x " xi ) + yi where xi # x # xi + 1! ( xi + 1 , yi + 1 ) ! ( xi , yi ) ! ( x , y )
Polygon Inside, Outside: Crossing Number
Does line segment intersect ray?
7
CCW. Given three point a, b, and c, is a-b-c a counterclockwise turn?
! Analog of comparisons in sorting.
! Idea: compare slopes.
Lesson. Geometric primitives are tricky to implement.
! Dealing with degenerate cases.
! Coping with floating point precision.
Implementing CCW
c a b yes b a c no c a b Yes (! slope) c a b ??? (collinear) c b a ??? (collinear) b a c ??? (collinear) 8
Implementing CCW
CCW. Given three point a, b, and c, is a-b-c a counterclockwise turn?
! Determinant gives twice area of triangle.
! If area > 0 then a-b-c is counterclockwise.
If area < 0, then a-b-c is clockwise.
If area = 0, then a-b-c are collinear.
2 " Area ( a , b , c ) = ax ay 1 bx by 1 cx cy 1 = ( bx # ax )( cy # ay ) # ( by # ay )( cx # ax )
(ax, ay)
(bx, by) (cx, cy) (cx, cy) (bx, by) (ax, ay)
13
Brute Force
Observation 1. Edges of convex hull of P connect pairs of points in P.
Observation 2. Edge pq is on convex hull if all other points are
counterclockwise of pq.
O(N^3 ) algorithm. For all points p and q in P, check whether pq is
an edge of convex hull.
p q each check requires O(N) ccw calculations, where N is the number of points in P 14
Package Wrap (Jarvis March)
Package wrap.
! Start with point with smallest y-coordinate.
! Rotate sweep line around current point in ccw direction.
! First point hit is on the hull.
! Repeat.
15
Package Wrap (Jarvis March)
Implementation.
! Compute angle between current point and all remaining points.
! Pick smallest angle larger than current angle.
! "(N) per iteration.
16
How Many Points on the Hull?
Parameters.
! N = number of points.
! h = number of points on the hull.
Package wrap running time. "(N h) per iteration.
How many points on hull?
! Worst case: h = N.
! Average case: difficult problems in stochastic geometry.
- in a disc: h = N^1 /^3.
- in a convex polygon with O(1) edges: h = log N.
17
Graham Scan: Example
Graham scan.
! Choose point p with smallest y-coordinate.
! Sort points by polar angle with p to get simple polygon.
! Consider points in order, and discard those that
would create a clockwise turn.
p 18
Graham Scan: Example
Implementation.
! Input: p[ 1 ], p[ 2 ], …, p[N] are points.
! Output: M and rearrangement so that p[ 1 ], ..., p[M] is convex hull.
Running time. O(N log N) for sort and O(N) for rest.
// preprocess so that p[ 1 ] has smallest y-coordinate // sort by angle with p[ 1 ] points[ 0 ] = points[N]; // sentinel int M = 2 ; for (int i = 3 ; i <= N; i++) { while (Point.ccw(p[M- 1 ], p[M], p[i]) <= 0 ) { M--; } M++; swap(points, M, i); } why? discard points that would create clockwise turn add i to putative hull 19
Quick Elimination
Quick elimination.
! Choose a quadrilateral Q or rectangle R with 4 points as corners.
! If point is inside, can eliminate.
- 4 ccw tests for quadrilateral
- 4 comparisons for rectangle
Three-phase algorithm
! Pass through all points to compute R.
! Eliminate points inside R.
! Find convex hull of remaining points.
Practice. Can eliminate almost all points
in linear time.
Q
these points eliminated
R
20
Convex Hull Algorithms Costs Summary
t assumes "reasonable" point distribution Package wrap Algorithm Graham scan Sweep line Quick elimination N h Running Time N log N N log N N t Quickhull N log N Best in theory N log h Mergehull N log N asymptotic cost to find h-point hull in N-point set output sensitive running time
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < $.
! Observation: only need to consider points within $ of line L.
! Sort points in 2$-strip by their y coordinate.
! Only check distances of those within 11 positions in sorted list!
1 2 3 4 5 6 7
L
$ = min(12, 21)
31
Closest Pair of Points
Def. Let si be the point in the 2$-strip, with
the ith^ smallest y-coordinate.
Claim. If |i – j| % 12, then the distance between
si and sj is at least $.
Pf.
! No two points lie in same !$-by-!$ box.
! Two points at least 2 rows apart
have distance % 2(!$).!
Fact. Still true if we replace 12 with 7.
27 29 30 31 28 26 25 $
2 rows
39 i j 32
Closest Pair Algorithm
Closest-Pair(p 1 , …, pn) { Compute separation line L such that half the points are on one side and half on the other side. $ 1 = Closest-Pair(left half) $ 2 = Closest-Pair(right half) $ = min( $ 1 , $ 2 ) Delete all points further than $ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next 11 neighbors. If any of these distances is less than $ , update $. return $. } O(N log N) 2 T(N / 2 ) O(N) O(N log N) O(N) 33
Closest Pair of Points: Analysis
Running time.
Upper bound. Can be improved to O(N log N).
Lower bound. In quadratic decision tree model, any algorithm for
closest pair requires #(N log N) steps.
T( N ) " 2 T ( N / 2 ) + O ( N log N ) # T( N ) = O ( N log^2 N )
avoid sorting by y-coordinate from scratch
34
Nearest Neighbor
35
1854 Cholera Outbreak, Golden Square, London
http://content.answers.com/main/content/wp/en/c/c 7 /Snow-cholera-map.jpg 36
Nearest Neighbor
Input. N Euclidean points.
Nearest neighbor problem. Given a query point p, which one of original
N points is closest to p?
Brute Algorithm Goal
Preprocess N log N
N
Query log N 37
Voronoi Diagram
Voronoi region. Set of all points closest to a given point.
Voronoi diagram. Planar subdivision delineating Voronoi regions.
Fact. Voronoi edges are perpendicular bisector segments.
Voronoi of 2 points (perpendicular bisector) Voronoi of 3 points (passes through circumcenter)
42
Fortune's Algorithm
http://www.diku.dk/hjemmesider/studerende/duff/Fortune 43
Fortune's Algorithm
Fortune's algorithm. Sweep-line algorithm can be implemented in
O(N log N) time.
Brute Algorithm Fortune
Preprocess N log N
N
Query log N but very tricky to get right due to degeneracy and floating point! 44
Discretized Voronoi
Discretized Voronoi. Solve nearest neighbor problem on an N-by-N grid.
Brute force. For each grid cell, maintain closest point. When adding a
new point to Voronoi, update N^2 cells.
45
Hoff's algorithm. Align apex of a right circular cone with sites.
! Minimum envelope of cone intersections projected onto plane is
the Voronoi diagram.
! View cones in different colors & render Voronoi.
Implementation. Draw cones using standard graphics hardware!
Hoff's Algorithm
http://www.cs.unc.edu/~geom/voronoi/siggraph_paper/voronoi.pdf
46
Delaunay Triangulation
Delaunay triangulation. Triangulation of N points such that no point
is inside circumcircle of any other triangle.
Fact 0. It exists and is unique (assuming no degeneracy).
Fact 1. Dual of Voronoi (connect adjacent points in Voronoi diagram).
Fact 2. No edges cross & O(N) edges.
Fact 3. Maximizes the minimum angle for all triangular elements.
Fact 4. Boundary of Delaunay triangulation is convex hull.
Fact 5. Shortest Delaunay edge connects closest pair of points.
Delaunay Voronoi 47 asymptotic time to solve a 2D problem with N points convex hull Problem closest pair
N^2
Brute N^2 furthest pair N^2 N log N Cleverness N log N N log N Delaunay triangulation N^4 N log N polygon triangulation N^2 N
Summary
Summary. Many fundamental geometric problems require ingenuity
to solve large instances.