UMass Lowell Computer Science 91.503 91 503 Graduate Algorithms P f K Prof. Karen D Daniels i l Spring, 2011 Computational Geometry Overview from Cormen Cormen,, et al. Chapter 33 (with additional material from other sources) 1 Overview • C Computational i l Geometry G Introduction I d i • Line Segment Intersection • Convex Hull Algorithms • Nearest Neighbors/Closest Points 2 Introduction What is Computational Geometry? 3 Applied Computational Geometry Research Assoc Prof. Assoc. Prof Karen Daniels & Collaborators Design Clustering for Data Mining Analyze feasibility, estimation, optimization problems for covering, clustering, packing, layout, geometric modeling Covering for Geometric Modeling Packing for Manufacturing Apply Meshing for Geometric Modeling Courtesy of Cadence Design Systems Topological Invariant Estimation for 4 Geometric Modeling Typical Problems • bin packing • Voronoi diagram • simplifying polygons • shape similarity • convex hull • maintaining g line arrangements • polygon partitioning • nearest neighbor search • kd--trees kd SOURCE:: Steve Skiena’s Algorithm Design Manual SOURCE 5 (for problem descriptions, see graphics gallery at http://www.cs.sunysb.edu/~algorith)) Common Computational Geometry Structures Convex Hull Voronoi Diagram New Point Delaunay Triangulation 6 source: O’Rourke, Computational Geometry in C Sample Tools of the Trade Al ith Design Algorithm D i Patterns/Techniques: P tt /T h i binary search divide divide--andand-conquer duality randomization sweepsweep-line incremental derandomization parallelism Algorithm Analysis Techniques: asymptotic analysis, amortized analysis Data Structures: winged--edge, quadwinged quad-edge, range tree, kd kd--tree Th Theoretical i l Computer C Science S i principles: i i l NP--completeness, hardness NP MATH Sets Summations Probability Growth of Functions Combinatorics Proofs Geometry Linear Algebra Recurrences Graph Theory 7 Computational Geometry in Context Geometry D i Design Applied pp Math A l Analyze Computational Geometry Efficient Geometric Algorithms Theoretical Computer Science Apply Applied Computer Science 8 Line i Segment Intersections i (2D) Intersection of 2 Line Segments Intersection of > 2Line Segments 9 Cross-Product-Based Geometric Primitives Some fundamental geometric questions: source: 91.503 91 503 ttextbook tb k C Cormen ett al. l p3 p2 p3 (1) p4 p2 p1 p0 p2 p1 p1 (2) (3) 10 Cross-Product-Based Geometric Primitives: (1) p2 33.1 p1 p0 ⎛ x1 p1 × p2 = det⎜⎜ ⎝ y1 x2 ⎞ ⎟⎟ = x1 y2 − x2 y1 y2 ⎠ Advantage:: less sensitive to accumulated roundAdvantage round-off error (1) 11 source: 91.503 textbook Cormen et al. Cross-Product-Based Geometric Primitives: (2) p2 p1 33.2 p0 (2) 12 source: 91.503 textbook Cormen et al. Intersection of 2 Line Segments Step p 1: 1: Bounding Box Test p3 p3 and p4 on opposite sides of p1p2 p2 p4 p1 (3) Step 2: 2: Does each segment straddle the line containing the other? 33.3 13 source: 91.503 textbook Cormen et al. Segment-Segment Intersection • • Findingg the actual intersection ppoint Approach: parametric vs. slope/intercept • parametric generalizes to more complex intersections • • Lcd e.g. segment/triangle P Parameterize i eachh segment Lcd c C=d--c C=d Lab c b a Lab b q(t)=c+tC A=b--a A=b d d a p(s)=a+sA Intersection: values of s, t such that p(s) =q(t) : a+sA a+sA= =c+tC 2 equations in unknowns s, t : 1 for x, 1 for y 14 source: O’Rourke, Computational Geometry in C Intersection of >2 Line Segments Sweep--Line Algorithmic Paradigm: Sweep 33.4 15 source: 91.503 textbook Cormen et al. Intersection of >2 Line Segments Sweep-Line Algorithmic Paradigm: Sweep- 16 source: 91.503 textbook Cormen et al. Intersection of >2 Line Segments Time to detect if any 2 segments intersect:O(n lg n) Balanced BST stores segments in order of intersection with sweep line. Associated operations take O(lgn) time. Note that it exits as soon as one intersection is detected. 33.5 17 source: 91.503 textbook Cormenet et al. al. source: 91.503 textbook Cormen Intersection of Segments • • Goal: “Output Goal: “Outputp -size sensitive” line segment g intersection algorithm g that computes all intersection points Bentley--Ottmann plane sweep: O(( Bentley O((n+k n+k)log( )log(n+k n+k))= ))= O(( O((n+k n+k))logn) logn) time • • k = number of intersection points in output Intuition: sweep vertical line rightwards • • • • just before intersection, 2 segments are adjacent in sweepsweep-line intersection structure check for intersection onlyy adjacent j segments g insert intersection event into sweepsweep-line structure event types: • • • top endpoint of a segment bottom endpoint of a segment intersection between 2 segments • swap order Improved to O(nlogn+k) [Chazelle/Edelsbrunner] 18 source: O’Rourke, Computational Geometry in C Convex Hull Algorithms g Definitions Gift Wrapping Graham Scan QuickHull Incremental Di id -andDivideDivide and d-Conquer C Lower Bound in Ω(nlgn) 19 Convexity & Convex Hulls source: O’Rourke, Computational Geometry in C • A convex combination of points x1, ..., xk is a sum of the form α1x1+...+ αkxk where α i ≥ 0 ∀i and α1 + L + α k = 1 • Convex hull of a set of points is the set of all convex combinations of points i t in i the th set. t nonconvex polygon 20 source: 91.503 textbook Cormen et al. convex hull of a point set Naive Algorithms for Extreme Points Algorithm: INTERIOR POINTS Algorithm: for each i do for each j = i do for each k = j = i do for each L = k = j = i do if pL in triangle(p g (pi, pj, pk) then pL is nonextreme O(n4) Algorithm Algorithm: g : EXTREME EDGES for each i do for each j = i do for each k = j = i do if pk is not left or on (pi, pj) then (pi , pj) is not extreme O(n3) 21 source: O’Rourke, Computational Geometry in C Algorithms: 2D Gift Wrapping • Use one extreme edge as an anchor for finding g the next θ Algorithm: GIFT WRAPPING Algorithm: index of the lowest point i0 i i0 repeat for each j = i Compute counterclockwise angle θ from previous hull edge k index of point with smallest θ Output (pi , pk) as a hull edge i k 22 2 until i = i0 O(n ) source: O’Rourke, Computational Geometry in C Gift Wrapping source: 91.503 textbook Cormen et al. 33.9 Output Sensitivity: Sensitivity: O(n2) runrun-time is actually O(nh) where h is the number of vertices of the convex hull. 23 Algorithms: 3D Gift Wrapping O(n2) time [output sensitive: O(nF) for F faces on hull] 24 CxHull Animations: Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html Algorithms: 2D QuickHull • • Concentrate on points close to hull boundary N Named d ffor similarity i il it to t Quicksort a b A c finds one of upper or lower hull Algorithm: QUICK HULL Algorithm: function QuickHull(a,b,S) if S = 0 return() else c index of point with max distance from ab A points strictly right of (a (a,c) c) B points strictly right of (c,b) return QuickHull(a,c,A) + (c) + QuickHull(c,b,B) O(n252) source: O’Rourke, Computational Geometry in C Algorithms: 3D QuickHull CxHull Animations: Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html 26 Graham’s Algorithm source: O’Rourke, Computational Geometry in C • • Points sorted angularly provide “star--shaped” starting point “star Prevent “dents” dents as you go via convexity testing θ p0 Algorithm: GRAHAM SCAN Algorithm: Find rightmost lowest point; label it p0. Sort all other points angularly about p0. In case of tie, delete point(s) closer to p0. Stack S (p1, p0) = (pt, pt-1); t indexes top i 2 while i < n do if pi is strictly left of pt-1pt then Push(pi, S) and set i i +1 else Pop(S) “multipop” O(nlgn) 27 Graham Scan 28 source: 91.503 textbook Cormen et al. Graham Scan 33.7 29 source: 91.503 textbook Cormen et al. Graham Scan 33.7 30 source: 91.503 textbook Cormen et al. Graham Scan pj cannot be on CH(Q CH( Q) pi must be in shaded region region, preserving convexity. 31 source: 91.503 textbook Cormen et al. Graham Scan 32 source: 91.503 textbook Cormen et al. Algorithms: 2D Incremental source: O’Rourke, Computational Geometry in C • Add points, i one at a time i • • update hull for each new point Key step becomes adding a single point to an existing hull. • Find 2 tangents g • • Results of 2 consecutive LEFT tests differ Idea can be extended to 3D. Algorithm: INCREMENTAL ALGORITHM Algorithm: ConvexHull{p {p0 , p1 , p2 } Let H2 for k 3 to n - 1 do ConvexHull{ Hk-1 U pk } Hk O(n2) 33 can be improved to O(nlgn) Algorithms: 3D Incremental O(n2) time 34 CxHull Animations: Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html Algorithms: 2D Divide Divide-and-Conquer and Conquer source: O’Rourke, Computational Geometry in C • Divide-andDivideand-Conquer q in a ggeometric setting • O(n) merge step is the challenge • • • Fi d upper and Find d llower tangents t t Lower tangent: find rightmost pt of A & leftmost pt of B; then “walk it downwards” B A Id can be Idea b extended t d d to t 3D. 3D Algorithm: DIVIDE Algorithm: DIVIDE--and and--CONQUER Sort points by x coordinate Divide points into 2 sets A and B: A contains left n/2 points B contains t i right i ht n/2 /2 points i t Compute ConvexHull(A) and ConvexHull(B) recursively Merge ConvexHull(A) and ConvexHull(B) O(nlgn) 35 Algorithms: 3D Divide and Conquer O(n log n) time ! CxHull Animations: Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html 36 Combinatorial Size of Convex Hull Convex Hull boundary is intersection of hyperplanes,, so hyperplanes worst--case worst combinatorial size complexity (number of features, not necessarily running time) is in: Θ(n (n 2≤d ≤8 Qhull: http://www.qhull.org/ ⎣d / 2 ⎦ ) 37 Lower Bound of O(nlgn) source: O’Rourke, Computational Geometry in C • • Worst-case time to find convex hull of n Worstpoints in algebraic decision tree model is in Ω(nlgn nlgn)) Proof uses sorting reduction: • • • Given unsorted list of n numbers: (x1,x2 ,…, xn) F Form unsorted t d sett off points: i t (x ( i, xi2) for f eachh xi Convex hull of points produces sorted list! list! • • • Parabola: everyy ppoint is on convex hull Reduction is O(n) (which is in o(nlgn o(nlgn)) )) Finding convex hull of n points is therefore at least as hard as sorting n points, points so worstworst-case time is in Ω(nlgn) nlgn) Parabola for sorting 2,1,3 38 CONVEX LAYERS DEMO Courtesyy of David Wolfendale in 91.504,, Spring p g 2010 For practical motivation behind this problem, please see the following link: http://www.montefiore.ulg.ac.be/~briquet/algo3 http://www.montefiore.ulg.ac.be/~ briquet/algo3--chull chull--20070206.pdf (e.g. detection of outliers) 39 Nearest Neighbor/ Cl Closest t Pair P i off Points P i t 40 Closest Pair Goal:: Given n ((2D)) points Goal p in a set Q,, find the closest p pair under the Euclidean metric in O(n lgn) time. Divide-and-Conquer Strategy: -X = points sorted by increasing x -Y = points sorted by increasing y Divide: partition with vertical line L into PL, PR -Divide: -Conquer: recursively find closest pair in PL, PR δL, δR are closest-pair distances δ = min( δL, δR ) -Combine: C bi closest-pair l t i is i either ith δ or pair i straddles t ddl partition titi line li L Check for pair straddling partition line L both points must be within δ of L create array Y’ Y = Y with only points in 2δ strip for each point p in Y’ find (<= 7) points in Y’ within δ of p 41 source: 91.503 textbook Cormen et al. Closest Pair Correctness 33.11 42 source: 91.503 textbook Cormen et al. Closest Pair Running g Time: T (n) = ⎧2T (n / 2) + O(n) ⎨ ⎩ O(1) if n > 3⎫ ⎬ = O(n lg n) if n ≤ 3⎭ Key Point: Point: Presort points, then at each step form sorted subset of sorted array in linear time Like opposite of MERGE step in MERGE MERGE--SORT SORT… … L 43 R source: 91.503 textbook Cormen et al. Additional Computational Geometry Resources • • Computational Geometry in C, 2nd edition • by Joseph O’Rourke • Cambridge University Press • 1998 Computational Geometry: Algorithms & Applications, 3rd edition • by deBerg et al. al. • Springer • 2008 See also 91.504 Course Web Site (and its additional resources): http://www.cs.uml.edu/~kdaniels/courses/ALG_504.html 44