Convex Sets & Concave Sets A planar region R is called convex if and only if for any pair of...

Preview:

Citation preview

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

R1

p

q

R2

Concave

An Example

1 2

3

4

Convex Hull

The convex hull CH(Q) of a set Q is the smallest convex regionthat 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.

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

pp

p

p

p

p

p

p

pp

9

210

5

76 1

3 4

Edges of a Convex Hull

For every edge with both endpoints p, q P.

pq

All other points in P lie

to the same side of the line passing through p and q

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

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.

Polar Angle

ppolar angle

x

y

q

r

An Example of Graham Scan

p 0

pp

pp

pp

p

pp

p

12

3

4

p5

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

Stack Initialization

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

2

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

3

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 5

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

p 7

p 8

p

pp

pp

pp

p

pp

p

0

12

3

4

p5

6

7

8

9

10

11

p

p

p

S

0

1

4

p 6

p 7

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p10

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p11

Finish

p

pp

p

p

pp

p

pp

p

0

12

3

4

p5

6

78

9

10

11

p

p

p

S

0

1

4

p 6

p 9

p11

Graham’s Scan

p

p

p

p

0

j

i

k

p

p

p

0

i

lEvery pointin P ispushed ontoS once

Non-verticesof CH(P)determinedso far are popped

vertices of CH(P) in the counter-clockwiseorder.

candidatesfor verticesof CH(P)

p0

p

p1

2

startp

p0

s

finishp

p

p0

j

i

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) Push(p , S) 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

01 2 n–1

012

i

i

0

Proof of Correctness

Claim 1 Each point popped from stack S is not a vertex of CH(P).

pp

p

p

0

i

jk

p

p

p

p0

i

j

k

Two cases when p is popped: j

In neither case can p become a vertex of CH(P). j

Proof

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 regioncontaining p i The invariant still holds.

Proof 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

p0

pj

pi

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).

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?

Findingp0

1 (n) (n)

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

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 minimumPolar angle, that is, the next vertex, takes time O(n).

Comparison between two polar angles can be doneusing cross product.

Thus O(nh) time in total.

Recommended