Document

advertisement

Algorithm Design and Analysis (ADA)

242-535, Semester 1 2014-2015

16. Computational

Geometry Topics

• Objective o an examination of four important CG topics o just a taster of a very large research area

242-535 ADA: 16. CG Topics 1

Overview

1. Intersection of Multiple Line Segments o the sweeping algorithm

2. Finding the Convex Hull o Graham Scan, Jarvis' March, QuickHull

3. Finding the Closest Pair of Points o divide-and-conquer

4. The Art Gallery Problem

242-535 ADA: 16. CG Topics 2

1. Intersection of Multiple

Line Segments

1.1. Multiple Line Segments

1.2. A Brute-Force Algorithm

1.3. The Sweeping Algorithm

1.4. Implementing Sweeping

242-535 ADA: 16. CG Topics 3

1.1. Multiple Line Segments

Input : a set of n line segments in the plane.

Output : all intersections, and for each intersection the involved segments. .

1.2. A Brute-Force Algorithm

Look at each pair of segments, and check if they intersect.

If so, output the intersection. n(n-1)/2 comparison are needed in the worst case, so the running time is O(n 2 )

But the lines are sparsly distributed in practice:

Most segments do not intersect, or if they do, only with a few other segments

Need a faster algorithm that deals with such situations!

1.3. The Sweeping Algorithm

Avoid testing pairs of segments that are far apart.

Idea : imagine a vertical sweep line passes through the given set of line segments, from left to right.

Sweep line also known as the "Bentley-Ottmann" Algorithm and the Sweep Line Algorithm

Non-Degeneracy Assumptions

No segment is vertical.

// this means that the sweep line will always hit a segment at a point.

If an input segment is vertical, then it is rotated clockwise by a tiny angle.

Sweep Line Status

The set of segments intersecting the sweep line.

It changes as the sweep line moves, but not continuously .

Updates of status happen only at event points . endpoints intersections

A

G

C

T event points

Ordering Segments

(

A total order over the segments that intersect the current position of the sweep line: later

C > D

B > C > D

A and E not in

B ( B drops out of the ordering) the ordering)

A C

E

D

At an event point, the sequence of segments changes:

Update the status.

Detect the intersections.

K

Status Update (1)

Event point is the left endpoint of a segment.

L

M

O

N new event point

K, M, N K, L, M, N

A new segment L intersecting the sweep line

Check if L intersects with the segment above ( K ) and the segment below ( M ).

Intersection(s) are new event points.

Status Update (2)

Event point is an intersection.

K

L

M

O

N

O, L, M, N O, M, L, N

The two intersecting segments

( L and M ) change order.

Check intersection with new neighbors ( M with O and

L with N ).

Intersection(s) are new event points.

K

Status Update (3)

Event point is a lower endpoint of a segment.

L

M

O

The two neighbors ( O and L ) become adjacent.

Check if they ( O and L ) intersect.

Intersection is new event point.

N

O, M, L, N O, L, N

1.4. Implementing Sweeping

The algorithm manages two kinds of data:

1. The sweep line status gives the relationships among the objects intersected by the sweep line.

2. The event point queue is a sequence of event points that defines the halting positions of the sweep line.

242-535 ADA: 16. CG Topics 13

Event Point Queue Operations

Manages the ordering of event points:

• by x -coordinates

• by y -coordinates in case of a tie in x -coordinates

Supports the following operations on a segment s .

 fetching the next event

 inserting an event

//

//

O

O

(log

(log m m )

) m = # event points currently being managed

Every event point p is stored with all segments starting at p .

Data structure: a balanced binary search tree

(e.g., red-black tree).

Sweep Line Operations

The sweep line status (T) requires the following operations:

INSERT( T, s ): insert segment s into T .

DELETE( T, s ): delete segment s from T .

ABOVE( T, s ): return the segment immediately above segment s in T .

BELOW( T, s ): return the segment immediately below segment s in T .

Use a balanced binary search tree for T (e.g. Red-black trees): O(log n) for each operation

242-535 ADA: 16. CG Topics 15

Segments intersect Pseudocode

ANY-SEGMENTS-INTERSECT(S)

1 T = Ø

2 sort the endpoints of the segments in S from left to right, breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y-coordinates first

3 for (each point p in the sorted list of endpoints) {

4 if ( p is the left endpoint of a segment s) {

5 INSERT(T, s)

6 if (ABOVE(T, s) exists and intersects s) or

(BELOW(T, s) exists and intersects s)

7 return TRUE

}

8 if ( p is the right endpoint of a segment s) {

9 if (both ABOVE(T, s) and BELOW(T, s) exist) and

(ABOVE(T, s) intersects BELOW(T, s))

10

11 return TRUE

DELETE(T, s)

}

}

12 return FALSE

242-535 ADA: 16. CG Topics 16

Execution Example

242-535 ADA: 16. CG Topics e d b the intersection of segments d and b is detected when segment c is deleted

17

Running Time

• If the segment set contains n segments, then

ANY-SEGMENTS-INTERSECT runs in time O(n log n) o Line 1 takes O(1) time. o Line 2 takes O(n log n) time, using merge sort or heapsort. o The for loop of lines 3–11 iterates at most once per event point, and so with 2n event points, the loop iterates at most

2n times. o Each iteration takes O(log n) time, since each tree operation takes O(log n) time and each intersection test takes O(1) time (by using the intersection function from part 15). o Total cost = O(1) + O(n * (log n + 1)) = O(n log n)

242-535 ADA: 16. CG Topics 18

2. Finding the Convex Hull

2.1. Convex & Concave Sets

2.2. The Convex Hull

2.3. The Graham Scan

2.4. Jarvis’ March

2.5. QuickHull

2.6. Lower Bound of O(n log n)

242-535 ADA: 16. CG Topics 19

2.1. Convex and 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 . p p

R

1

R

2 q

Convex q

Concave

2.2. The Convex Hull

The convex hull CH( P ) of a set of points P is the smallest convex region that contains all of P .

Rubber band

When P is finite, its convex hull is the unique convex polygon whose vertices are from P and that contains all points of P .

The Convex Hull Problem

Input : a set P = { p , p ,

, p n

} of points

Output : a list of vertices of CH( P ) in counterclockwise order.

Example p

5

CH(P) = [ p p p

9

2 p

6 p

8 p

3 p

1 p

4 p

7

5

, p

9

, p

2

, p

8

, p

10

, p

7

] p

10

Not all the points in P are in CH(P)

Edges of a Convex Hull

For every edge both endpoints p , q

P .

All other points in P lie to the same side of the line passing through p and q q p all points on this side of the q – p edge

Floating Arithmetic is not Exact

Nearly colinear points p , q , r . q r p p to the left of qr . q to the left of rp . r to the left of qp .

All three accepted as edges!

The algorithm is not robust

– it could fail due to small numerical error.

2.3. The Graham Scan

p

9 p

7 p

6 p

4 p

11 p

8 p

10 p

5 p

3 p

2 p

1 sort by polar angle p

0

The center point has the minimum y -coordinate

How to break a tie?

Labels are in the polar angle order.

(What if two points have the same polar angle?) handling degeneracies

A Turning Algorithm

• Consider each of the points in the sorted sequence.

• For each point, is moving from the two previously considered points to this point a "left turn" or "right turn"?

• " Right turn ": this means that the second-to-last point is not part of the convex hull and should be removed. o this process is continued for as long as the set of the last three points is a "right turn"

• " Left turn ": the algorithm moves to the next point in the sequence.

242-535 ADA: 16. CG Topics 26

X

Turning in Action

C is a left turn compared to A – B; add C

D is a right turn compared to B - C; remove C

242-535 ADA: 16. CG Topics

D is a left turn compared to A - B; add D

27

The Graham Scan Algorithm

Graham-Scan( P ) let p0 be the point in P with minimum y -coordinate let  p1 , p2 , … , pn-1  be the remaining points in P sorted in counterclockwise order by polar angle around p0

Stack s = new Stack(); s.push(p0) s.push(p1) s.push(p2) for i = 3 to n

1 while ( pi makes a nonleft turn from the line segment determined by s.top() and s.nextToTop() ) s.pop() s.push(pi) return s the convex hull points are stored on a stack

Stack Usage

p

11 p

10 p

9 p

8 p

7 p

6 p

5 p

2 p

4 p

3 p

1 p

0

S p

2 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

5 p

4 p

2 p

3 p

1 p

0

S p

3 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

2 p

5 p

3 p

4 p

1 p

0

S p

4 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

2 p

5 p

3 p

4 p

1 p

0

S p

5 p

4 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

2 p

5 p

3 p

4 p

1 p

0

S p

6 p

4 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

2 p

5 p

3 p

4 p

1 p

0

S p

8 p

7 p

6 p

4 p

1 p

0

p

11 p

10 p

9 p

8 p

7 p

6 p

2 p

5 p

3 p

4 p

1 p

0

S p

4 p

1 p

0 p

7 p

6

p

11 p

10 p

9 p

8 p

6 p

7 p

5 p

3 p

4 p

2 p

1 p

0

S p

4 p

1 p

0 p

10 p

9 p

6

p

11 p

10 p

9 p

8 p

6 p

7 p

5 p

3 p

4 p

2 p

1 p

0

S p

4 p

1 p

0 p

11 p

9 p

6

Finish

p

11 p

10 p

9 p

8 p

6 p

7 p

5 p

3 p

4 p

2 p

1 p

0

S p

4 p

1 p

0 p

11 p

9 p

6

Proof of Correctness

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

Proof p k pk is a right turn compared to pi - pj

Two cases when pj is popped: p k p i

X p j pk is a 0 angle turn compared to pi - pj p

0 p

0 p j

X p i

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

The points on stack S always form the vertices of a convex polygon in counterclockwise order (an invariant ).

Proof •

The claim holds at S initialization when p0, p1, p2 form a triangle (which is obviously convex)

Popping a point from S preserves the invariant.

Consider a point pi being pushed onto S . p j

The region containing pi p i

The invariant still holds.

p

0

Running Time

#operations time / operation total

Finding p

0

1

( n )

( n )

Sorting 1 O ( n log n ) O ( n log n )

Push n O (1)

( n )

Pop

 n

2 O (1) O ( n )

Why?

The running time of Graham’s Scan is

O(n log n) .

2.4. Jarvis’ March

A “package/gift wrapping” technique using two "chains"

The Operation of Jarvis’ March

• We choose the first vertex as the lowest point p0, and start the right chain .

o The next vertex, p1, has the smallest polar angle of any point with respect to p0. o Then, p2 has the smallest polar angle with respect to p1. o The right chain goes as high as the highest point p3.

• Then,we return to p0 and build the left chain by finding smallest polar angles with respect to the negative x-axis.

242-535 ADA: 16. CG Topics 43

Running Time of Jarvis’ 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)

The comparison between two polar angles can be done using the cross product.

Thus O(nh) time in total.

2.5. QuickHull

• Concentrate on points close to hull boundary

• Named for similarity to Quicksort o O(n log n) a

A

Set QuickHull(a, b, S) if S = 0 return {} else c = index of point with max distance from a-b

A = points strictly right of (a, c)

B = points strictly right of (c, b) return QuickHull(a, c, A) + {c} +

QuickHull(c, b, B)

242-535 ADA: 16. CG Topics b c finds one of upper or lower hull

45

2.6. Lower Bound of O(n log n)

• The worst-case time to find the convex hull of n points using a decision tree model is

O (n log n)

• Proof based on sorting: o Given an unsorted list of n numbers: (x

1 o Form an unsorted set of points: (x i

,x

2

,…, x n

)

, x i

2 ) for each x i o Convex hull of these points produces a sorted list!

• a parabola: so every point is on the convex hull o Finding the convex hull of n points is therefore at

least as hard as sorting n points, so worst-case time is in O (n log n)

242-535 ADA: 16. CG Topics

Convex hull of red pts

46

3. Finding the Closest Pair of Points

• There are a set of n points P = { p

1,

…p n

}.

• Find a pair of points p, q such that |p – q| is the minimum of all |p i

– p j

|

• Easy to do in O(n 2 ) time o for all pi ≠ pj, compute ║pi - pj║ on all the pairs and choose the minimum, which involves n(n-1)/2 comparisons

• We will aim for O(n log n) time

242-535 ADA: 16. CG Topics 47

Divide and Conquer

Divide: o Compute the median of the x-coordinates o Split the points into a left half PL and right half PR, each of size n/2 median line

Conquer: compute the closest pairs for PL and PR

Combine the results (the hard part)

PL PR

48 242-535 ADA: 16. CG Topics

Combine

• dL = closestDist(PL) dR = closestDist(PR) d = min( d1, d2 )

• Observe: o Need to check only pairs which cross the dividing line o Only interested in pairs within distance < d of each other

• It's enough to look at only the points in the 2d wide strip around the median line

242-535 ADA: 16. CG Topics

PL median line

PR

49

Scanning the Strip

• Sort all points in the strip by their y-coords, forming q1…qk, k ≤ n.

• Let yi be the y-coord of qi dmin = d for i = 1 to k { j = i - 1 while (yi - yj < d){ if( ║ qi-qj ║ < d) dmin = ║ qi-qj ║ j = j-1

}

}

• Report dmin (and the corresponding pair)

242-535 ADA: 16. CG Topics 50

Running Time

Combine: O(n log n) because we sort the y-coords.

But, we can: o Sort all points the y-coords at the beginning, outside of the main loop since the Divide stage preserves the y-order of points o Then this combine stage only takes O(n)

• We get T(n)=2T(n/2)+O(n), so T(n) is O(n log n) divide the data by half and calculate the closest pair on PL and PR

242-535 ADA: 16. CG Topics combine

51

4. The Art Gallery Problem

camera

How many cameras are needed to guard a gallery and where should they be placed?

Simple Polygon Model

Model the art gallery as a region bounded by some simple polygon (no self-crossing). Regions with holes are not allowed.

convex polygon one camera an arbitrary n -gon ( n vertices)

Bad news: finding the minimum number of cameras for a given polygon is NP-hard (exponential time).

Triangulation

To make things easier, we decompose a polygon into pieces that are easy to guard. Draw diagonals between pairs of vertices. diagonals

Guard the art gallery by placing a camera in every triangle …

Triangulation : decomposition of a polygon into triangles by a maximal set of non-intersecting diagonals.

No. of Cameras

Theorem : Every simple polygon has a triangulation.

Any triangulation of a simple polygon with n vertices consists of n – 2 triangles.

n – 2 cameras can guard the simple polygon.

Note : a camera sitting on a diagonal guards two triangles.

 the no. of cameras can be reduced to roughly n/2

Note : a vertex is adjacent to many triangles so placing cameras at vertices can reduce the number even more…

3-Coloring of Vertices

Idea: Select vertices, such that every triangle has at least one of those vertices.

Assign each vertex a color: pink, green, or yellow.

Any two vertices connected by an edge or a diagonal must be assigned different colors.

Thus the vertices of every triangle will be in three different colors .

Choose the smallest color set, and place cameras at all the vertices using that color

  n/3

 cameras.

The Dual Graph

A dual graph G has a node inside every triangle and an edge between every pair of nodes whose corresponding triangles share a diagonal.

The dual graph G is actually a tree (marked in red on this slide), and it is possible to build one using DFS .

Download