Computational Geometry Chapter in Cormen, et al. in PDF

37
UMass Lowell Computer Science Prof. Karen Daniels Computational Geometry Overview from Cormen, et al. Chapter 33

Transcript of Computational Geometry Chapter in Cormen, et al. in PDF

Page 1: Computational Geometry Chapter in Cormen, et al. in PDF

UMass Lowell Computer ScienceProf. Karen Daniels

Computational Geometry

Overview from Cormen, et al.

Chapter 33

Page 2: Computational Geometry Chapter in Cormen, et al. in PDF

Overview

Line Segment Intersection

Convex Hull Algorithms

Nearest Neighbors/Closest Points

Page 3: Computational Geometry Chapter in Cormen, et al. in PDF

Line Segment Intersections

(2D)

Intersection of 2 Line Segments

Intersection of > 2Line Segments

Page 4: Computational Geometry Chapter in Cormen, et al. in PDF

Cross-Product-Based

Geometric Primitives

source: 91.503 textbook Cormen et al.

p0

p2

p1

(1)

p1

p3

p2

(2)

p2

p1

p3

p4

(3)

Some fundamental geometric questions:

Page 5: Computational Geometry Chapter in Cormen, et al. in PDF

Cross-Product-Based

Geometric Primitives: (1)

source: 91.503 textbook Cormen et al.Advantage: less sensitive to accumulated round-off error

1221

21

21

21 det yxyxyy

xxpp

p0

p2

p1

(1)

33.1

Page 6: Computational Geometry Chapter in Cormen, et al. in PDF

Cross-Product-Based

Geometric Primitives: (2)

source: 91.503 textbook Cormen et al.

p0

p2

p1

(2)

33.2

Page 7: Computational Geometry Chapter in Cormen, et al. in PDF

Intersection of 2 Line Segments

source: 91.503 textbook Cormen et al.

p2

p1

p3

p4

(3)

Step 1:

Bounding Box

Test

Step 2: Does each

segment straddle

the line containing

the other?

33.3

p3 and p4 on opposite sides of p1p2

Page 8: Computational Geometry Chapter in Cormen, et al. in PDF

Segment-Segment Intersection

Finding the actual intersection point Approach: parametric vs. slope/intercept

parametric generalizes to more complex intersections e.g. segment/triangle

Parameterize each segment

Intersection: values of s, t such that p(s) =q(t) : a+sA=c+tC

a

b

c

d

LabLcd

a

b

c

d

LabLcd

A=b-a

p(s)=a+sA

q(t)=c+tC

C=d-c

source: O’Rourke, Computational Geometry in C

2 equations in unknowns s, t : 1 for x, 1 for y

Page 9: Computational Geometry Chapter in Cormen, et al. in PDF

Demo

Segment/Segment Intersectionhttp://cs.smith.edu/~orourke/books/CompGeom/CompGeom.html

Page 10: Computational Geometry Chapter in Cormen, et al. in PDF

Intersection of >2 Line Segments

source: 91.503 textbook Cormen et al.

Sweep-Line Algorithmic Paradigm:

33.4

Page 11: Computational Geometry Chapter in Cormen, et al. in PDF

Intersection of >2 Line Segments

source: 91.503 textbook Cormen et al.

Sweep-Line Algorithmic Paradigm:

Page 12: Computational Geometry Chapter in Cormen, et al. in PDF

Intersection of >2 Line Segments

source: 91.503 textbook Cormen et al.

Time to detect if any 2

segments intersect:O(n lg n)

source: 91.503 textbook Cormen et al.

33.5

Note that it exits as soon as one intersection is detected.

Balanced BST stores segments in order

of intersection with sweep line.

Associated operations take O(lgn) time.

Page 13: Computational Geometry Chapter in Cormen, et al. in PDF

Intersection of Segments

Goal: “Output-size sensitive” line segment intersection algorithm that actually computes all intersection points

Bentley-Ottmann plane sweep: O((n+k)log(n+k))= O((n+k)logn) time

k = number of intersection points in output

Intuition: sweep horizontal line downwards

just before intersection, 2 segments are adjacent in sweep-line intersection structure

check for intersection only adjacent segments

insert intersection event into sweep-line structure

event types:

top endpoint of a segment

bottom endpoint of a segment

intersection between 2 segments

swap order

Improved to O(nlogn+k) [Chazelle/Edelsbrunner]source: O’Rourke, Computational Geometry in C

Page 14: Computational Geometry Chapter in Cormen, et al. in PDF

Convex Hull Algorithms

DefinitionsGift WrappingGraham Scan

QuickHullIncremental

Divide-and-ConquerLower Bound in (nlgn)

Page 15: Computational Geometry Chapter in Cormen, et al. in PDF

Convexity & Convex Hulls

A convex combination of points

x1, ..., xk is a sum of the form

1x1+...+ kxk where

Convex hull of a set of points is the

set of all convex combinations of

points in the set.

nonconvex polygon

convex hull of a point set

10 1 ki andi

source: O’Rourke, Computational Geometry in C

source: 91.503 textbook Cormen et al.

Page 16: Computational Geometry Chapter in Cormen, et al. in PDF

Naive Algorithms

for Extreme Points

Algorithm: INTERIOR POINTS

for each i do

for each j = i do

for each k = j = i do

for each L = k = j = i do

if pL in triangle(pi, pj, pk)

then pL is nonextreme O(n4)

Algorithm: EXTREME EDGES

for each i do

for each j = i do

for each k = j = i do

if pk is not left or on (pi, pj)

then (pi , pj) is not extreme O(n3)source: O’Rourke, Computational Geometry in C

Page 17: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 2D Gift Wrapping

Use one extreme edge as an

anchor for finding the next

O(n2)

Algorithm: GIFT WRAPPING

i0 index of the lowest point

i i0repeat

for each j = iCompute counterclockwise angle from previous hull edge

k index of point with smallest Output (pi , pk) as a hull edgei k

until i = i0 source: O’Rourke, Computational Geometry in C

Page 18: Computational Geometry Chapter in Cormen, et al. in PDF

Gift Wrapping source: 91.503 textbook Cormen et al.

33.9

Output Sensitivity: O(n2) run-time is actually O(nh)

where h is the number of vertices of the convex hull.

Page 19: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 3D Gift Wrapping

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html

O(n2) time

[output sensitive: O(nF) for F faces on hull]

Page 20: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 2D QuickHull

Concentrate on points close to hull boundary

Named for similarity to Quicksort

a

b

O(n2)

Algorithm: QUICK HULL

function QuickHull(a,b,S)

if S = 0 return()else

c index of point with max distance from abA points strictly right of (a,c)B points strictly right of (c,b)return QuickHull(a,c,A) + (c) + QuickHull(c,b,B)

source: O’Rourke, Computational Geometry in C

finds one of upper or lower hull

c

A

Page 21: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 3D QuickHull

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html

Page 22: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: >= 2D

Qhull: http://www.qhull.org/

Convex Hull

boundary is

intersection of

hyperplanes, so

worst-case

combinatorial size (not necessarily running

time) complexity is

in:)(

2/dn

Page 23: Computational Geometry Chapter in Cormen, et al. in PDF

Graham’s Algorithm

Points sorted angularly provide “star-shaped” starting point

Prevent “dents” as you go via convexity testing

O(nlgn)

Algorithm: GRAHAM SCAN

Find rightmost lowest point; label it p0.

Sort all other points angularly about p0.In case of tie, delete point(s) closer to p0.

Stack S (p1, p0) = (pt, pt-1); t indexes topi 2while i < n do

if pi is strictly left of pt-1pt

then Push(pi, S) and set i i +1else Pop(S)

source: O’Rourke, Computational Geometry in C

“multipop”

p0

Page 24: Computational Geometry Chapter in Cormen, et al. in PDF

Graham Scan

source: 91.503 textbook Cormen et al.

Page 25: Computational Geometry Chapter in Cormen, et al. in PDF

Graham Scan

source: 91.503 textbook Cormen et al.

33.7

Page 26: Computational Geometry Chapter in Cormen, et al. in PDF

Graham Scan

source: 91.503 textbook Cormen et al.

33.7

Page 27: Computational Geometry Chapter in Cormen, et al. in PDF

Graham Scan

source: 91.503 textbook Cormen et al.

Page 28: Computational Geometry Chapter in Cormen, et al. in PDF

Graham Scan

source: 91.503 textbook Cormen et al.

Page 29: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 2D Incremental

Add points, one at a time

update hull for each new point

Key step becomes adding a single point to an existing hull.

Find 2 tangents

Results of 2 consecutive LEFT tests differ

Idea can be extended to 3D.

O(n2)

Algorithm: INCREMENTAL ALGORITHM

Let H2 ConvexHull{p0 , p1 , p2 }

for k 3 to n - 1 doHk ConvexHull{ Hk-1 U pk }

can be improved to O(nlgn)

source: O’Rourke, Computational Geometry in C

Page 30: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms: 3D Incremental

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html

O(n2) time

Page 31: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms:

2D Divide-and-Conquer

Divide-and-Conquer in a geometric setting

O(n) merge step is the challenge Find upper and lower tangents

Lower tangent: find rightmost pt of A & leftmost pt of B; then “walk it downwards”

Idea can be extended to 3D.

Algorithm: DIVIDE-and-CONQUER

Sort points by x coordinate

Divide points into 2 sets A and B:

A contains left n/2 points

B contains right n/2 points

Compute ConvexHull(A) and ConvexHull(B) recursively

Merge ConvexHull(A) and ConvexHull(B) O(nlgn)

AB

source: O’Rourke, Computational Geometry in C

Page 32: Computational Geometry Chapter in Cormen, et al. in PDF

Algorithms:

3D Divide and Conquer

CxHull Animations: http://www.cse.unsw.edu.au/~lambert/java/3d/hull.html

O(n log n) time !

Page 33: Computational Geometry Chapter in Cormen, et al. in PDF

Lower Bound of O(nlgn)

Worst-case time to find convex hull of n points in algebraic decision tree model is in

(nlgn)

Proof uses sorting reduction:

Given unsorted list of n numbers: (x1,x2 ,…, xn)

Form unsorted set of points: (xi, xi2) for each xi

Convex hull of points produces sorted list!

Parabola: every point is on convex hull

Reduction is O(n) (which is in o(nlgn))

Finding convex hull of n points is therefore at least as hard as sorting n points, so worst-case time is in (nlgn) Parabola for sorting 2,1,3

source: O’Rourke, Computational Geometry in C

Page 34: Computational Geometry Chapter in Cormen, et al. in PDF

Nearest Neighbor/

Closest Pair of Points

Page 35: Computational Geometry Chapter in Cormen, et al. in PDF

Closest Pair

source: 91.503 textbook Cormen et al.

Goal: Given n (2D) points in a set Q, find the closest pair

under the Euclidean metric in O(n lgn) time.

Divide-and-Conquer Strategy:-X = points sorted by increasing x

-Y = points sorted by increasing y-Divide: partition with vertical line L into PL, PR

-Conquer: recursively find closest pair in PL, PR

- L, R are closest-pair distances

- min( L, R )-Combine: closest-pair is either or pair straddles partition line L

- Check for pair straddling partition line L

- both points must be within of L

- create array Y’ = Y with only points in 2 strip

- for each point p in Y’

- find (<= 7) points in Y’ within of p

Page 36: Computational Geometry Chapter in Cormen, et al. in PDF

Closest Pair

source: 91.503 textbook Cormen et al.

Correctness

33.11

Page 37: Computational Geometry Chapter in Cormen, et al. in PDF

Closest Pair

Running Time:

Key Point: Presort points, then at each step form

sorted subset of sorted array in linear time

)lg(3 if)1(

3 if)()2/(2)( nnO

nO

nnOnTnT

Like opposite of MERGE step in MERGESORT…

source: 91.503 textbook Cormen et al.R

L