1/13/15CMPS 3130/6130: Computational Geometry1 CMPS 3130/6130: Computational Geometry Spring 2015 Convex Hulls Carola Wenk.

Slides:



Advertisements
Similar presentations
2/9/06CS 3343 Analysis of Algorithms1 Convex Hull  Given a set of pins on a pinboard  And a rubber band around them  How does the rubber band look when.
Advertisements

CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Lecture 3: Parallel Algorithm Design
2/3/15CMPS 3130/6130 Computational Geometry1 CMPS 3130/6130 Computational Geometry Spring 2015 Triangulations and Guarding Art Galleries II Carola Wenk.
Computing Convex Hulls CLRS 33.3
Convex Hull obstacle start end Convex Hull Convex Hull
CS16: Introduction to Data Structures & Algorithms
One of the most important problems is Computational Geometry is to find an efficient way to decide, given a subdivision of E n and a point P, in which.
C o m p u t i n g C O N V E X H U L L S by Kok Lim Low 10 Nov 1998 COMP Presentation.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Divide-and-Conquer The most-well known algorithm design strategy:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Convex Hulls in Two Dimensions Definitions Basic algorithms Gift Wrapping (algorithm of Jarvis ) Graham scan Divide and conquer Convex Hull for line intersections.
CS4413 Divide-and-Conquer
Theory of Algorithms: Divide and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Convex Sets & Concave Sets A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R.
Computational Geometry
8/29/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk.
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Today’s Material Computational Geometry Problems –Closest Pair Problem –Convex Hull Jarvis’s March, Graham’s scan –Farthest Point Problem.
What does that mean? To get the taste we will just look only at some sample problems... [Adapted from S.Suri]
ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis.
Convex Hulls Computational Geometry, WS 2006/07 Lecture 2 Prof. Dr. Thomas Ottmann Algorithmen & Datenstrukturen, Institut für Informatik Fakultät für.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
UMass Lowell Computer Science Advanced Algorithms Computational Geometry Prof. Karen Daniels Spring, 2007 Lecture 3 Chapter 3: 2D Convex Hulls Friday,
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Lection 1: Introduction Computational Geometry Prof.Dr.Th.Ottmann 1 History: Proof-based, algorithmic, axiomatic geometry, computational geometry today.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading.
9/7/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2006 Triangulations and Guarding Art Galleries Carola Wenk.
Analysis of Algorithms
Convex Hull. What is the Convex Hull? Imagine a set of points on a board with a nail hammered into each point. Now stretch a rubber band over all the.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
14/13/15 CMPS 3130/6130 Computational Geometry Spring 2015 Windowing Carola Wenk CMPS 3130/6130 Computational Geometry.
C o m p u t i n g C O N V E X H U L L S. Presentation Outline 2D Convex Hulls –Definitions and Properties –Approaches: Brute Force Gift Wrapping QuickHull.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CMPS 3120: Computational Geometry Spring 2013
CMPS 3130/6130 Computational Geometry Spring 2015
Convex Hull. University of Manchester biologists used lasers to measure the minimum amount of skin required to wrap around the skeletons of modern-day.
Computational Geometry 2D Convex Hulls
A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R. Otherwise, it is called concave.
COMPUTATIONAL GEOMETRY AND MATRIX MULTIPLICATION Mohammed Zeeshan Farooqui Minhaj Uddin.
Convex Hull 2012/10/23. Convex vs. Concave A polygon P is convex if for every pair of points x and y in P, the line xy is also in P; otherwise, it is.
Convex Sets & Concave Sets A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
9/8/10CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Fall 2010 Triangulations and Guarding Art Galleries Carola Wenk.
Convex Hulls Guo Qi, Chen Zhenghai, Wang Guanhua, Shen Shiqi, Himeshi De Silva.
Lecture 3: Parallel Algorithm Design
Convex Hull.
Algorithm design techniques Dr. M. Gavrilova
Convex Hull obstacle start end 11/21/2018 4:05 AM Convex Hull
Convex Sets & Concave Sets
CHAPTER 33 Computational Geometry
CMPS 3130/6130: Computational Geometry Spring 2017
CMPS 3130/6130 Computational Geometry Spring 2017
Divide-and-Conquer The most-well known algorithm design strategy:
CMPS 3120: Computational Geometry Spring 2013
Convex Hull obstacle start end 4/30/2019 5:21 PM Convex Hull
Introduction to Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

1/13/15CMPS 3130/6130: Computational Geometry1 CMPS 3130/6130: Computational Geometry Spring 2015 Convex Hulls Carola Wenk

1/13/15CMPS 3130/6130: Computational Geometry2 Convex Hull Problem  Given a set of pins on a pinboard and a rubber band around them. How does the rubber band look when it snaps tight?  The convex hull of a point set is one of the simplest shape approximations for a set of points.

1/13/15CMPS 3130/6130: Computational Geometry3 Convexity  A set C  R 2 is convex if for every two points p,q  C the line segment pq is fully contained in C. convex non-convex

1/13/15CMPS 3130/6130: Computational Geometry4 Convex Hull  The convex hull CH(P) of a point set P  R 2 is the smallest convex set C  P. In other words CH(P) =  C. C  P C convex P

1/13/15CMPS 3130/6130: Computational Geometry5 Convex Hull  Observation: CH(P) is the unique convex polygon whose vertices are points of P and which contains all points of P  Goal: Compute CH(P). What does that mean? How do we represent/store CH(P)?  Represent the convex hull as the sequence of points on the convex hull polygon (the boundary of the convex hull), in counter-clockwise order.

1/13/15CMPS 3130/6130: Computational Geometry6 A First Try Algorithm SLOW_CH(P): /* CH(P) = Intersection of all half-planes that are defined by the directed line through ordered pairs of points in P and that have all remaining points of P on their left */ Input: Point set P  R 2 Output: A list L of vertices describing the CH(P) in counter-clockwise order E:=  for all (p,q)  P  P with p≠q // ordered pair valid := true for all r  P, r≠p and r≠q if r lies to the right of directed line through p and q // takes constant time valid := false if valid then E:=E  pq // directed edge Construct from E sorted list L of vertices of CH(P) in counter-clockwise order Runtime: O(n 3 ), where n = |P| How to test that a point lies to the right of a directed line?

1/13/15CMPS 3130/6130: Computational Geometry7 Orientation Test / Halfplane Test p q r r q p positive orientation (counter-clockwise) r lies to the left of pq negative orientation (clockwise) r lies to the right of pq r q p zero orientation r lies on the line pq Orient(p,q,r) = sign det Can be computed in constant time 1 p x p y 1 q x q y 1 r x r y,where p = (p x,p y )

1/13/15CMPS 3130/6130: Computational Geometry8 Jarvis’ March (Gift Wrapping) Algorithm Giftwrapping_CH(P): // Compute CH(P) by incrementally inserting points from left to right Input: Point set P  R 2 Output: List q 1, q 2,… of vertices in counter-clockwise order around CH(P) q 1 = point in P with smallest y (if ties, with smallest x) q 2 = point in P with smallest angle to horizontal line through q 1 i = 2 do { i++ q i = point with smallest angle to line through q i-2 and q i-1 } while q i ≠ q 1 q1q1 q2q2 q3q3 Runtime: O(hn), where n = |P| and h = #points on CH(P) Output-sensitive algorithm

1/13/15CMPS 3130/6130: Computational Geometry9 Incremental Insertion Algorithm Incremental_CH(P): // Compute CH(P) by incrementally inserting points from left to right Input: Point set P  R 2 Output: C=CH(P), described as a list of vertices in counter-clockwise order Sort points in P lexicographically (by x-coordinate, break ties by y-coordinate) Remove first three points from P and insert them into C in counter-clockwise order around the triangle described by them. for all p  P // Incrementally add p to hull Compute the two tangents to p and C Remove enclosed non-hull points from C, and insert p Runtime: O(  i) = O(n 2 ), where n = |P| O(n log n) O(1) n-3 times O(i) i=3 n Really?

1/13/15CMPS 3130/6130: Computational Geometry10 Tangent computation pipi upper_tangent(C,p i ): // Compute upper tangent to p i and C. Return tangent vertex v t v t = p i-1 while succ(v t ) lies above line through p i and v t v t = succ(v t ) return v t vtvt vbvb p i-1 succ(p i-1 ) pred(p i-1 )  Amortization: Every vertex that is checked during tangent computation is afterwards deleted from the current convex hull C

1/13/15CMPS 3130/6130: Computational Geometry11 Incremental Insertion Algorithm Incremental_CH(P): // Compute CH(P) by incrementally inserting points from left to right Input: Point set P  R 2 Output: C=CH(P), described as a list of vertices in counter-clockwise order Sort points in P lexicographically (by x-coordinate, break ties by y-coordinate) Remove first three points from P and insert them into C in counter-clockwise order around the triangle described by them. for all p  P // Incrementally add p to hull Compute the two tangents to p and C Remove enclosed non-hull points from C, and insert p Runtime: O(n log n + n) = O(n log n), where n = |P| O(n log n) O(1) n-3 times O(1) amort.

1/13/15CMPS 3130/6130: Computational Geometry12 Convex Hull: Divide & Conquer  Preprocessing: sort the points by x- coordinate  Divide the set of points into two sets A and B:  A contains the left  n/2  points,  B contains the right  n/2  points  Recursively compute the convex hull of A  Recursively compute the convex hull of B  Merge the two convex hulls A B

1/13/15CMPS 3130/6130: Computational Geometry13 Merging  Find upper and lower tangent  With those tangents the convex hull of A  B can be computed from the convex hulls of A and the convex hull of B in O(n) linear time A B

1/13/15CMPS 3130/6130: Computational Geometry14 check with orientation test right turn left turn Finding the lower tangent a = rightmost point of A b = leftmost point of B while T=ab not lower tangent to both convex hulls of A and B do{ while T not lower tangent to convex hull of A do{ a=a-1 } while T not lower tangent to convex hull of B do{ b=b+1 } } A B 0 a= =b 5 6 7

1/13/15CMPS 3130/6130: Computational Geometry15 Convex Hull: Runtime  Preprocessing: sort the points by x- coordinate  Divide the set of points into two sets A and B:  A contains the left  n/2  points,  B contains the right  n/2  points  Recursively compute the convex hull of A  Recursively compute the convex hull of B  Merge the two convex hulls O(n log n) just once O(1) T(n/2) O(n)

1/13/15CMPS 3130/6130: Computational Geometry16 Convex Hull: Runtime  Runtime Recurrence: T(n) = 2 T(n/2) + cn  Solves to T(n) =  (n log n)

1/13/15CMPS 3130/6130: Computational Geometry17 Recurrence (Just like merge sort recurrence) 1.Divide: Divide set of points in half. 2.Conquer: Recursively compute convex hulls of 2 halves. 3.Combine: Linear-time merge. T(n) = 2 T(n/2) + O(n) # subproblems subproblem size work dividing and combining

1/13/15CMPS 3130/6130: Computational Geometry18 Recurrence (cont’d) T(n) =  (1) if n = 1; 2T(n/2) +  (n) if n > 1. How do we solve T(n)? I.e., how do we find out if it is O(n) or O(n 2 ) or …?

1/13/15CMPS 3130/6130: Computational Geometry19 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.

1/13/15CMPS 3130/6130: Computational Geometry20 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n)T(n)

1/13/15CMPS 3130/6130: Computational Geometry21 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n/2) dn

1/13/15CMPS 3130/6130: Computational Geometry22 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn T(n/4) dn/2

1/13/15CMPS 3130/6130: Computational Geometry23 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) …

1/13/15CMPS 3130/6130: Computational Geometry24 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n

1/13/15CMPS 3130/6130: Computational Geometry25 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn

1/13/15CMPS 3130/6130: Computational Geometry26 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn

1/13/15CMPS 3130/6130: Computational Geometry27 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn …

1/13/15CMPS 3130/6130: Computational Geometry28 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn #leaves = n (n)(n) …

1/13/15CMPS 3130/6130: Computational Geometry29 Recursion tree Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn #leaves = n (n)(n) Total  (n log n) …

1/13/15CMPS 3130/6130: Computational Geometry30 The divide-and-conquer design paradigm 1.Divide the problem (instance) into subproblems. a subproblems, each of size n/b 2.Conquer the subproblems by solving them recursively. 3.Combine subproblem solutions. Runtime is f(n)

1/13/15CMPS 3130/6130: Computational Geometry31 Master theorem T(n) = a T(n/b) + f (n) C ASE 1: f (n) = O(n log b a –  )  T(n) =  (n log b a ). C ASE 2: f (n) =  (n log b a log k n)  T(n) =  (n log b a log k+1 n). C ASE 3: f (n) =  (n log b a +  ) and a f (n/b)  c f (n)  T(n) =  ( f (n))., where a  1, b > 1, and f is asymptotically positive. Convex hull: a = 2, b = 2  n log b a = n  CASE 2 (k = 0)  T(n) =  (n log n).

1/13/15CMPS 3130/6130: Computational Geometry32 Graham’s Scan Another incremental algorithm –Compute solution by incrementally adding points –Add points in which order? Sorted by x-coordinate But convex hulls are cyclically ordered  Split convex hull into upper and lower part upper convex hull UCH(P) lower convex hull LCH(P)

1/13/15CMPS 3130/6130: Computational Geometry33 Graham’s LCH Algorithm Grahams_LCH(P): // Incrementally compute the lower convex hull of P Input: Point set P  R 2 Output: A list L of vertices describing LCH(P) in counter-clockwise order Sort P in increasing order by x-coordinate  P = {p 1,…,p n } L = {p 2,p 1 } for i=3 to n while |L|>=2 and orientation(L.second(), L.first(), p i,) <= 0 // no left turn delete first element from L Append p i to the front of L Each element is appended only once, and hence only deleted at most once  the for-loop takes O(n) time O(n log n) time total O(n log n) O(n)

1/13/15CMPS 3130/6130: Computational Geometry34 Lower Bound Comparison-based sorting of n elements takes  (n log n) time. How can we use this lower bound to show a lower bound for the computation of the convex hull of n points in R 2 ?

35 Decision-tree model A decision tree models the execution of any comparison sorting algorithm: One tree per input size n. The tree contains all possible comparisons (= if-branches) that could be executed for any input of size n. The tree contains all comparisons along all possible instruction traces (= control flows) for all inputs of size n. For one input, only one path to a leaf is executed. Running time = length of the path taken. Worst-case running time = height of tree. 1/13/15CMPS 3130/6130: Computational Geometry

36 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3            a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 1/13/15CMPS 3130/6130: Computational Geometry

37 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3            a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 1/13/15CMPS 3130/6130: Computational Geometry

38 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3           a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 9  4 1/13/15CMPS 3130/6130: Computational Geometry

39 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3           a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 9  6 1/13/15CMPS 3130/6130: Computational Geometry

40 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3           a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 4  6 1/13/15CMPS 3130/6130: Computational Geometry

41 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Each internal node is labeled a i :a j for i, j  {1, 2,…, n}. The left subtree shows subsequent comparisons if a i  a j. The right subtree shows subsequent comparisons if a i  a j. Sort  a 1, a 2, a 3            a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 4  6  9 1/13/15CMPS 3130/6130: Computational Geometry

42 Decision-tree for insertion sort a1:a2a1:a2 a1:a2a1:a2 a2:a3a2:a3 a2:a3a2:a3 a1a2a3a1a2a3 a1a2a3a1a2a3 a1:a3a1:a3 a1:a3a1:a3 a1a3a2a1a3a2 a1a3a2a1a3a2 a3a1a2a3a1a2 a3a1a2a3a1a2 a1:a3a1:a3 a1:a3a1:a3 a2a1a3a2a1a3 a2a1a3a2a1a3 a2:a3a2:a3 a2:a3a2:a3 a2a3a1a2a3a1 a2a3a1a2a3a1 a3a2a1a3a2a1 a3a2a1a3a2a1 Sort  a 1, a 2, a 3            a1 a2 a3a1 a2 a3 a1 a2 a3a1 a2 a3 a2 a1 a3a2 a1 a3 i j i j i j a2 a1 a3a2 a1 a3 i j a1 a2 a3a1 a2 a3 i j insert a 3 insert a 2 4  6  9 Each leaf contains a permutation , ,…,  (n)  to indicate that the ordering a  (1)  a  (2)   a  (n) has been established. 1/13/15CMPS 3130/6130: Computational Geometry

43 Lower bound for comparison sorting Theorem. Any decision tree that can sort n elements must have height  (n log n). Proof. The tree must contain  n! leaves, since there are n! possible permutations. A height-h binary tree has  2 h leaves. Thus, n!  2 h.  h  log(n!)(log is mono. increasing)  log ((n/2) n/2 ) = n/2 log n/2  h   (n log n). 1/13/15CMPS 3130/6130: Computational Geometry

1/13/15CMPS 3130/6130: Computational Geometry44 Lower Bound Comparison-based sorting of n elements takes  (n log n) time. How can we use this lower bound to show a lower bound for the computation of the convex hull of n points in R 2 ? Devise a sorting algorithm which uses the convex hull and otherwise only linear-time operations  Since this is a comparison-based sorting algorithm, the lower bound  (n log n) applies  Since all other operations need linear time, the convex hull algorithm has to take  (n log n) time

1/13/15CMPS 3130/6130: Computational Geometry45 CH_Sort Algorithm CH_Sort(S): /* Sorts a set of numbers using a convex hull algorithm. Converts numbers to points, runs CH, converts back to sorted sequence. */ Input: Set of numbers S  R Output: A list L of of numbers in S sorted in increasing order P=  for each s  S insert (s,s 2 ) into P L’ = CH(P) // compute convex hull Find point p’  P with minimum x-coordinate for each p=(p x,p y )  L’, starting with p’, add p x into L return L s2s2 s

1/13/15CMPS 3130/6130: Computational Geometry46 Convex Hull Summary Brute force algorithm:O(n 3 ) Jarvis’ march (gift wrapping):O(nh) Incremental insertion:O(n log n) Divide-and-conquer: O(n log n) Graham’s scan: O(n log n) Lower bound:  (n log n)