20
Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness, and a good use for complicated data structures from CSE241). Some slides in this lecture come from Carola Wenk, Univ. Texas, San Antonio

Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Embed Size (px)

Citation preview

Page 1: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Last Time: Convex Hulls

Today:Plane Sweep Algorithms,

Segment Intersection,+ (Element Uniqueness,

and a good use for complicated data structures from CSE241).

Some slides in this lecture come from Carola Wenk, Univ. Texas, San Antonio

Page 2: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Geometric IntersectionsBasic problem in

Computational Geometry

• Solid modeling:• Robotics:

– Collision detection and avoidance

• Geographic information systems: – Overlay two subdivisions

(e.g., road network and river network)

• Computer graphics: – Ray shooting/Photon

Mapping to render scenes

From UNC

This environment consists of 25,000 tetrahedra (100,000 triangles) randomly placed within the scene. The flying object is a 747, which consists of nearly 15,000 triangles. (From Universitaat Salzburg).

Photon mapped virtual environment: (w. chang, ucsd).

Page 3: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Line Segment Intersection

• Input: A set S={s1, …, sn} of (closed) line segments in R2

• Output: All intersection points between segments in S

Page 4: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

The basics.

• Two line segments ab and cd• Write in terms of convex combinations:

p(s) = (1-s) a + s b for 0 s 1q(t) = (1-t) c + t d for 0 t 1Intersection if p(s)=q(t)

Equation system (1-s) ax + s bx = (1-t) cx + t dx (1-s) ay + s by = (1-t) cy + t dy

• Solve for s and t. – When is there no solution?– When do the segments intersect?

a

b

Page 5: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Bounds

• How many intersections can n line segments have?– 0 < k < n2

Really, from 0 to (n choose 2)

• Brute force algorithm?– Try out all pairs of line segments– Takes O(n2) time, is optimal in worst case

• Challenge: Develop an output-sensitive algorithm– Runtime depends on size k of the output, 0 k n2

– What is best possible run time to expect?– O( n log n + k) (will talk about on next slide).– Our algorithm will have runtime: O( (n+k) log n)

• (with many intersections, this is worse than brute force).

Page 6: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Complexity

• Why is runtime O( n log n + k) optimal?– Element uniqueness: Given n real numbers, are all of

them distinct?– The element uniqueness problem requires (n log n)

time ( in algebraic decision tree model of computation )

Reduction: • element uniqueness to line segment intersection:

– Take n numbers, convert into vertical line segments. There is an intersection iff there are duplicate numbers.

– If we could solve line segment intersection in o(n log n) time, i.e., strictly faster than (n log n), then element uniqueness could be solved faster. Contradiction.

Page 7: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Design of Plane Sweeps

• Plane sweep algorithms (also called sweep line algorithms) are a special kind of incremental algorithms

• Their correctness follows inductively by maintaining the cleanliness property– Algorithm is “clean” if the problem is “solved” to

the left of the sweep line.

• Common runtimes in the plane are O(n log n):– n events are processed– Update of sweep line status takes O(log n)– Update of event queue: O(log n) per event

Page 8: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Event Q

Sw

eep

Line

Page 9: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Plane sweep algorithm

• Cleanliness property:– All intersections to the left of sweep line l have

been reported

• Sweep line status:– Store segments that intersect the sweep line l,

ordered by y-coordinate.

• Events:– When sweep line status changes

combinatorially• Left segment endpoints (known at beginning)• Right segment endpoints (known at beginning)• Intersection points (computed on the fly)

Page 10: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

General position

Assume that “nasty” special cases don’t happen:– No line segment is vertical– Two segments intersect in at most one

point– No three segments intersect in a

common point

– (problems with this?)

Page 11: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Event Queue

• Need to keep events sorted:– Lexicographic order (first by x-

coordinate, and if two events have same x-coordinate then by y-coordinate)

• Need to be able to remove next point, and insert new points in O(log n) time

• Use a priority queue (heap)

Page 12: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Sweep Line Status• Store segments that intersect the sweep line l,

ordered along the intersection with l .• Need to insert, delete, and find adjacent neighbor in

O(log n) time• Use balanced binary search tree, storing the

order in which segments intersect l in leavesb

c

de

cbed

Page 13: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Event Handling• Left segment endpoint

– Add segment to sweep line status– Test adjacent segments on sweep line l for

intersection with new segment (see Lemma)– Add new intersection points to event queue

a

b

c

de

cbd

cbed

Page 14: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Event Handling2. Intersection point

– Report new intersection point– Two segments change order along l

? Test new adjacent segments for new intersection points (to insert into event queue)

a

b

c

de

cebd

cbed

Note: “new” intersection might have been detected before.

Page 15: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Event Handling3. Right segment endpoint

– Delete segment from sweep line status– Two segments become adjacent. Check for

intersection points (to insert in event queue)

a

b

c

de

ecbd

ecd

Page 16: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Correctness proof.We only report intersections that actually occur.Do we report all intersections?We only check for intersections between segments that are

neighbors in the sweep line data structure. Is that enough?Intersection Lemma

• Lemma: Let s, s’ be two non-vertical segments whose interiors intersect in a single point p. Assume there is no third segment passing through p. Then there is an event point to the left of p where s and s’ become adjacent (and hence are tested for intersection).

• Proof: Consider placement of sweep line infinitesimally left of p. s and s’ are adjacent along sweep line. Hence there must have been a previous event point where s and s’ become adjacent.

ps

s’

Page 17: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Runtime

• Sweep line status updates: O(log n)• Event queue operations: O(log n), as the

total number of stored events is 2n + k, and each operation takes time O(log(2n+k)) = O(log n2) = O(log n)

• There are O(n+k) events. Hence the total runtime is O((n+k) log n)

k = O(n2)

Page 18: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Backing out of general position.

• Suppose a segment is vertical. How can you fix the algorithm?– Function specialVerticalEvent:

• Suppose 3 segments intersect in a point

• Two segments overlap (intersect in more than one point?)

si

sj

Page 19: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

Table of good stuff• Running time

definitions:• O(n log n)

– At most n log n.– It is correct to say that

binary search is O(n log n). (it is also correct to say that it is O(log n).

• o(n log n)– Stricty better than n log n.

• (n log n)– Exactly n log n (up to

constant factors).

Lower Bounds:• Sorting: O(n log n)• Element Uniqueness O(n log n).

Data StructuresHeap:

Extract min: O(1)Insert-element O(log n)Delete element O(log n)

Balanced Binary Tree:Insert: O(log n)Delete: O(log n)Find neighbors: O(log n)

Lexicographic order(like alphabetical order, sort by first element, then by second element, etc.).

Page 20: Robert Pless, CS 546: Computational Geometry Lecture #3 Last Time: Convex Hulls Today: Plane Sweep Algorithms, Segment Intersection, + (Element Uniqueness,

Robert Pless, CS 546: Computational Geometry Lecture #3

• Example problem for sweep line?• Problem: Given PR2, |P|=n, find the distance of the

closest pair in P