output of a convex hull algorithm a b c g j
play

Output of a convex-hull algorithm: a, b, c, g, j A&DS Lecture 16 - PowerPoint PPT Presentation

The Convex Hull Definition 16.1 (1) A set C of points is convex if for all p, q C the whole line segment pq is contained in C . (2) The convex hull of a set S of points is the smallest convex set C that contains S . Observation 16.2 The convex


  1. The Convex Hull Definition 16.1 (1) A set C of points is convex if for all p, q ∈ C the whole line segment pq is contained in C . (2) The convex hull of a set S of points is the smallest convex set C that contains S . Observation 16.2 The convex hull of a finite set S of points is a convex polygon whose vertices (corner points) are elements of S . A&DS Lecture 16 1 Mary Cryan

  2. The Convex Hull Problem A finite set S of points in the plane Input: The vertices of the convex hull of S in counterclockwise order. Output: Example 16.3 g h d e i f c j b a Output of a convex-hull algorithm: a, b, c, g, j A&DS Lecture 16 2 Mary Cryan

  3. Polar Angles The polar angle of a point q with respect to a point p is the angle between a horizontal line and the line through p and q . q p Lemma There is an algorithm that, given points p 0 , p 1 , . . . , p n , sorts p 1 , . . . , p n by non-decreasing polar angle with respect to p 0 in O ( n lg n ) time (How? - this is related to Q1 of the week 10 tutorial). A&DS Lecture 16 3 Mary Cryan

  4. Graham’s Scan Idea • Let p 0 be a bottom-most point. Start walking around the points in the order of increasing polar angles. • As long as you turn left, keep on walking. • If you have to turn right to reach the next point, discard the current point and step back to the previous point. Repeat this until you can turn left to the next point. • The points that remain are the vertices of the convex hull. A&DS Lecture 16 4 Mary Cryan

  5. Example g h d e i f c j b a A&DS Lecture 16 5 Mary Cryan

  6. Implementation Algorithm G RAHAM -S CAN ( Q ) 1. Let p 0 be the point in Q with minimum y coordinate. In case of a tie, take the leftmost point. 2. Sort the points in Q \ { p 0 } by non-decreasing polar angles with respect to p 0 . If several points have the same polar angle, discard all but the one farthest away from p 0 . Let � p 1 ,...,p m � be the resulting list 3. if m ≤ 1 then return � p 0 ,...,p m � 4. Initialise stack S 5. S. PUSH ( p 0 ) 6. S. PUSH ( p 1 ) 7. S. PUSH ( p 2 ) 8. for i ← 3 to m do while the angle formed by the topmost two elements of S and p i 9. does not make a left turn do S. POP 10. S. PUSH ( p i ) 11. 12. return S A&DS Lecture 16 6 Mary Cryan

  7. Analysis Let n = | Q | , then m ≤ n . • Lines 3–7, 12 require time Θ ( 1 ) . • Line 1 requires time Θ ( n ) in the worst case. • Line 2 requires time Θ ( n lg n ) • The outer loop in lines 8–11 is iterated m − 2 times. Thus, disregarding the time needed by the inner loop, the loop requires time Θ ( m ) = O ( n ) . • The inner loop in lines 9–10 is executed at most once for each element, because every element enters the stack at most once and thus can only be popped once. Thus overall the inner loop requires time O ( n ) . Thus the overall worst-case running time is Θ ( n lg n ) . A&DS Lecture 16 7 Mary Cryan

  8. Proof of Correctness Let p 0 ,p 1 ,...,p m be the list of points obtained after executing line 2. (1) The vertices of the convex hull are among p 0 ,p 1 ,...,p m . Proof: If a point q is discarded because it has the same polar angle as some other point p i , then q is contained in p 0 p i , because p i is farther from p 0 than q is. Thus q is contained in the convex hull of p 0 ,p 1 ,...,p m . (2) If m ≤ 1 , the algorithm obviously works correctly. So assume that m ≥ 2 . For 2 ≤ i ≤ m , let C i denote the convex hull of p 0 ,...,p i . (3) After line 7 has been executed, the points on S are the vertices of C 2 in clockwise order. (4) By induction on i we show for 2 ≤ i ≤ m : After the ‘ i ’-execution of the outer loop, the points on S are the vertices of C i in clockwise order. ( ⋆ ) • Let i ≥ 3 . By (3) (if i = 3 ) or by the induction hypothesis (if i > 3 ), the points on S before the ‘ i ’-execution of the outer loop are the vertices of C i − 1 in clockwise order. 7-1

  9. • Since the polar angle of p i is greater than the polar angle of p i − 1 , p 0 p i − 1 p i form a triangle not contained in C i − 1 . p k p j p i p 0 In particular, p i is not contained in C i − 1 and thus is a vertex of C i . • Let p j and p k be the two topmost elements of the stack S . If the angle formed by p k p j p i makes a right turn, then the triangle p k p j p i is contained in the triangle p k p 0 p i . Thus p j is contained in the convex hull of p 0 ,p k ,p i and thus not a vertex of C i . Therefore, it is correct to remove p j from S in line 10. 7-2

  10. • On the other hand, if the angle formed by p k p j p i makes a left turn, then the triangle p k p j p i is not contained in the triangle p k p 0 p i . Thus p j is a vertex of C i . p i p 0 • Moreover, it can be easily seen that all vertices of C i − 1 between p 0 and p j in the counterclockwise order remain vertices of C j . So after the execution of line 11, S contains all vertices of C i . Thus ( ⋆ ) is proved. (5) Statement ( ⋆ ) for i = m proves the correctness of the algorithm. 7-3

  11. Remark 16.4 The correctness “proof” given above is still not a full mathematical proof, because many intuitive fact about convex polygons were just claimed without proof. But all the missing details can be filled in. At least intuitively, it should be clear now that Graham’s Scan works correctly. 7-4

  12. Optimality • The best-known algorithm for finding the convex hull has a running time of O ( n lg h ) , where h is the number of vertices of the convex hull. • On the other hand, it can be proved that every algorithm for finding the convex hull has a worst-case running time of Ω ( n lg n ) . The proof of this lower bound is due to the fact that we can implement sorting using Convex Hull (Q5 of week 10 tutorial sheet). A&DS Lecture 16 8 Mary Cryan

  13. Reading Assignment Section 33.3, pages 947-957, of [CLRS]. This is Section 35.3, pages 898-908, of [CLR] . Web resources http://en.wikipedia.org/wiki/Convex hull http://en.wikipedia.org/wiki/Graham scan Paul McCaffery’s animation of Graham’s Scan: “Geometric Algorithms” link from http://www.mac2000.pwp.blueyonder.co.uk/ A&DS Lecture 16 9 Mary Cryan

  14. Problems (1) Exercise 33.3-5, page 957, of [CLRS]. Ex 35.3-5, page 907, of [CLR] . (2) Prove that the problem of finding the Convex Hull of n points has a lower bound of Ω ( n lg n ) . For this, think about using a reduction from sorting to Convex Hull (that is, think about how to use a Convex Hull algorithm to sort a list of numbers). A&DS Lecture 16 10 Mary Cryan

Recommend


More recommend