280 likes | 1.23k Views
p. p. R. R. 1. 2. q. q. Concave. Convex. 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 . . An Example. 4. 1. 2. 3.
E N D
p p R R 1 2 q q Concave Convex 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.
An Example 4 1 2 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.
p 5 p p p 9 p 6 7 1 p p 2 p p 4 3 10 8 p Output: p , p , p , p , p , p . 5 9 2 8 10 7 The Convex Hull Problem Input: a set P = { p , p , …,p } of points 1 2 n Output: a list of vertices of CH(P) in counterclockwiseorder. Example
All other points in P lie to the same side of the line passing through p and q Edges of a Convex Hull For every edge both endpoints p, q P. q p
p to the left of qr. q to the left of rp. r to the left of qp. Floating Arithmetic is not Exact! Nearly colinear points p, q, r. q r p All three accepted as edges! Not robust – the algorithm could fail with small numerical error.
Polar Angle y q polar angle p x r
p p 6 9 p p 7 4 p 11 p p 8 p 5 10 p 3 p p 2 1 p 0 handling degeneracies An Example of Graham Scan Sort by polar angle. (with the minimumy-coordinate) How to break a tie? Labels are in the polar angle order. (What if two points have the same polar angle?)
S p 2 p 1 p 0 Stack Initialization p p 6 9 p p 7 4 p 11 p p 8 p 5 10 p 3 p p 2 1 p 0
p p 6 9 p p 7 4 p 11 S p p 8 p 5 10 p p 3 3 p 1 p p 2 1 p 0 p 0
p p 6 9 p p 7 4 p 11 S p p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
p p 6 S 9 p p 7 4 p 11 p p p 5 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
p p 6 S 9 p p 7 4 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 8 p 6 9 p p p 7 4 7 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 6 9 p p p 7 4 7 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 10 p 6 9 p p 4 9 p p 11 7 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 11 p 6 9 p p 4 9 p p 11 7 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
Finish S p p 11 p 6 9 p p 4 9 p p 11 p 7 p 8 6 p p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
p k Non-vertices of CH(P) determined so far are popped finish p p p j j l start Every point in P is pushed onto S once p p p p i i i s p 2 p 1 p p p p p 0 0 0 0 0 Graham’s Scan candidates for vertices of CH(P) vertices of CH(P) in the counter- clockwise order.
The Graham Scan Algorithm Graham-Scan(P) let p be the point in P with minimumy-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) Push(p , S) Push(p , S) fori 3 ton 1 do whilep makes a nonleft turn from the line segment determined by Top(S) and Next-to-Top(S) doPop(S) Push(S, p ) returnS 0 1 2 n–1 0 0 1 2 i i
p k p j p i p p i k p j p 0 p 0 In neither case can p become a vertex of CH(P). j Proof of Correctness Claim 1Each point popped from stack S is not a vertex of CH(P). Two cases when p is popped: Proof j
The 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 j The region containing p i p i p 0 Claim 2Graham-Scan maintains theinvariant that the points on stack S always form the vertices of a convex polygon in counterclockwise order. Proof Popping a point from S preserves the invariant. The invariant still holds.
Correctness of Graham’s Scan TheoremIf 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).
Finding 1 (n) (n) p 0 Running time #operations time / operation total Sorting 1 O(n lg n) O(n lg n) Push n O(1) (n) Pop n 2 O(1) O(n) Why? The running time of Graham’s Scan isO(n lg n).
(smallest polar angle w.r.t. p ) (smallest polar angle w.r.t. p ) (smallest polar angle w.r.t. p ) (smallest polar angle w.r.t. p ) (smallest polar angle w.r.t. p ) 4 5 2 0 1 p p p p p p p 1 3 6 0 4 2 5 (lowest point) Jarvis’ March A “package wrapping” technique Right chain Left chain
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. ThusO(nh)time in total.