Download presentation
Presentation is loading. Please wait.
Published byShauna Flowers Modified over 9 years ago
1
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. Otherwise, it is called concave. Convex p q R 1 p q R 2 Concave
2
An Example 12 3 4
3
Convex Hull The convex hull CH(Q) of a set Q is the smallest convex region that contains Q. Rubber band When Q is finite, its convex hull is the unique convex polygon whose vertices are from Q and that contains all points of Q.
4
The Convex Hull Problem Input: a set P = { p, p, …, p } of points Output: a list of vertices of CH(P) in counterclockwise order. 1 2 n Example Output: p, p, p, p, p, p. 5 9 2 8 10 7 8 p p p p p p p p p p 9 2 10 5 76 1 3 4
5
Edges of a Convex Hull For every edge with both endpoints p, q P. p q All other points in P lie to the same side of the line passing through p and q
6
A Slow Convex Hull Algorithm Slow-Convex-Hull(P) E {} // set of directed edges of CH(P) that bounds the // points of P on the right. for every ordered pair (p, q), where p, q P and p q // (n ) pairs do valid true for every point r p or q // n 2 such points do if r lies to the right of pq or collinear with p and q but not on pq then valid false if valid then E E { pq } // pq and qp cannot be both in E From E construct a list L of vertices of CH(P), sorted in counterclockwise order. // O(n ) easily improvable to O(n lg n) return L Running time (n ) 3 2 2 p q r
7
Floating Arithmetic is not Exact! p r q Nearly colinear points p, q, r. p to the left of qr. q to the left of rp. r to the left of qp. All three accepted as edges! Not robust – the algorithm could fail with small numerical error.
8
Polar Angle p polar angle x y q r
9
An Example of Graham Scan p 0 p p p p p p p p p p 1 2 3 4 p 5 6 7 8 9 10 11 (with the minimum y-coordinate) Labels are in the polar angle order. (What if two points have the same polar angle?) How to break a tie? Sort by polar angle. handling degeneracies
10
Stack Initialization p p p p p p p p p p p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 2
11
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 3
12
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4
13
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 5
14
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6
15
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 7 p 8
16
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 7
17
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 9 p 10
18
p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 11 p p p S 0 1 4 p 6 p 9 p
19
Finish p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 9 p
20
Graham’s Scan p p p p 0 j i k p p p 0 i l Every point in P is pushed onto S once Non-vertices of CH(P) determined so far are popped vertices of CH(P) in the counter- clockwise order. candidates for vertices of CH(P) p 0 p p 1 2 start p p 0 s finish p p p 0 j i
21
The Graham Scan Algorithm Graham-Scan(P) let p be the point in P with minimum y-coordinate let p, p, …, p be the remaining points in P sorted in counterclockwise order by polar angle around p. Top[S] 0 Push(p, S) for i 3 to n 1 do while p makes a nonleft turn from the line segment determined by Top(S) and Next-to-Top(S) do Pop(S) Push(S, p ) return S 0 1 2 n–1 0 1 2 i i 0
22
Proof of Correctness Claim 1 Each point popped from stack S is not a vertex of CH(P). p p p p 0 i j k p p p p 0 i j k Two cases when p is popped: j In neither case can p become a vertex of CH(P). j Proof
23
Claim 2 Graham-Scan maintains the invariant that the points on stack S always form the vertices of a convex polygon in counterclockwise order. Popping a point from S preserves the invariant. The region containing p i The invariant still holds. ProofThe claim holds right after initialization of S when p, p, p form a triangle (which is obviously convex). 0 1 2 Consider a point p being pushed onto S. i p 0 p j p i
24
Correctness of Graham’s Scan Theorem If Graham-Scan is run on a set P of at least three points, then a point of P is on the stack S at termination if and only if it is a vertex of CH(P).
25
Running time The running time of Graham’s Scan is O(n lg n). #operations time / operation total Push n O(1) (n) Pop n 2 O(1) O(n) Sorting 1 O(n lg n) O(n lg n) Why? Finding p 0 1 (n) (n)
26
Jarvis’ March A “package wrapping” technique p 4 p 0(lowest point) p 1 (smallest polar angle w.r.t. p ) 0 p 5 (smallest polar angle w.r.t. p ) 4 Right chain p 6 (smallest polar angle w.r.t. p ) 5 p 2 (smallest polar angle w.r.t. p ) 1 p 3 (smallest polar angle w.r.t. p ) 2 Left chain
27
Running Time of Jarvis’s March Let h be the number of vertices of the convex hull. For each vertex, finding the point with the minimum Polar angle, that is, the next vertex, takes time O(n). Comparison between two polar angles can be done using cross product. Thus O(nh) time in total.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.