CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line...

36
CS4522 Advanced Algorithms Batch 09, L4S1 N. H. N. D. de Silva Dept. of Computer Science & Eng University of Moratuwa Lecture 10 Computational Geometry

Transcript of CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line...

Page 1: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

CS4522 Advanced Algorithms

Batch 09, L4S1

N. H. N. D. de Silva

Dept. of Computer Science & Eng

University of Moratuwa

Lecture 10

Computational Geometry

Page 2: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Today’s Outline

• Computational Geometry

– Introduction

– Line Segments and their properties

– Sweeping (intersecting line segments)

– Computing the Convex Hull

• Graham’s Scan

• Jarvis’s March

4 Nov 2012 N. H. N. D. de Silva

Page 3: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Introduction

• Computational Geometry

– Algorithms to solve geometric problems

– Wide range of application areas

• Computer graphics, VLSI, CAD, molecular

modeling, metallurgy, textile layout, GIS,…

– Inputs

• E.g., points, line segments, vertices of a polygon,..

– Outputs

• Whether lines intersect, convex hull,…

5 Nov 2012 N. H. N. D. de Silva

Page 4: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Introduction

• We consider problems in 2D plane

• Objects represented by points {p1, p2, p3,…}

• Each pi = (xi, yi) where xi, yi are real nums

6 Nov 2012 N. H. N. D. de Silva

Page 5: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Terminology, Notations

• Say p1= (x1, y1), p2 = (x2, y2) are 2 points

• p3 = (x3, y3) is a convex combination of p1, p2 such that for some 0 ≤ α ≤ 1

x3 = α x1 + (1- α) x2

y3 = α y1 + (1- α) y2

• Intuitively

– p3 is any point on the line passing through p1

and p2; p3 lies on or between p1 and p2

7 Nov 2012 N. H. N. D. de Silva

Page 6: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Terminology, Notations

• Line segment is the set of convex combinations of p1, p2

• p1, p2 are the endpoints of segment

• If ordering of p1, p2 matters

– We can have directed segment

– If p1=(0,0), origin, then is the vector

8

21 pp

21pp

21pp 2p

21 pp

Nov 2012 N. H. N. D. de Silva

Page 7: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Line Segment Properties

• Questions to explore

– Given 2 directed segments and , is

clockwise from w.r.t p0?

– Given 2 line segments and , if we

traverse and then , do we make a left

turn at point p1?

– Do line segments and intersect?

9

10 pp 20 pp 10 pp

20 pp

10pp

21pp

10pp

21pp

21pp

43pp

Nov 2012 N. H. N. D. de Silva

Q1

Q2

Q3

Page 8: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Line Segment Properties

• Can answer each question in O(1) time

– Using only add, subtract, multiply, compare

– No need of division, trigonometric functions

• Which are computationally expensive, have

problems due to round-off errors

• [think of the straight-forward method to determine

whether two line segments intersect; this require

division; very sensitive to precision of division on

computers when 2 segments are nearly parallel]

10 Nov 2012 N. H. N. D. de Silva

Page 9: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Cross Product

• Cross product is a core operation we need

• Consider vectors p1 and p2

• The cross product as a determinant

• [Strictly speaking cross product is a 3D concept;

but we will treat it simply as the value x1y2 – x2y1]

11

121221

21

21

21 det ppyxyxyy

xxpp

Nov 2012 N. H. N. D. de Silva

Page 10: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

If p1 = (x1, y1) and p2 = (x2, y2) then

p1 p2 = x1 x2 = x1y2 – x2y1

y1 y2

If p1 p2 > 0, then p1 is clockwise from p2 (w.r.t. the origin);

if p1 p2 < 0, then p1 is counterclockwise from p2;

If p1 p2 = 0, then p1 and p2 are colinear (pointing in either same or

opposite directions)

Cross Product

Nov 2012 N. H. N. D. de Silva 12

Page 11: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Q1: Segment Clockwise?

• Can answer Q1 using the cross product

– Given 2 directed segments and , is

clockwise from w.r.t p0?

– Translate cross product to use p0 as the origin

– Compute the cross product

– If it is positive, then is clockwise from

else it is counter-clockwise

Nov 2012 N. H. N. D. de Silva 13

10 pp 20 pp 10 pp

20 pp

))(())(()()(010202010201

yyxxyyxxpppp 10 pp 20 pp

Page 12: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Q2: Turning Left?

• Given 2 line segments and , if we

traverse and then , do we make a left

turn at point p1?

• Equivalent: which way the angle p0p1p2 turns?

– With cross products, can answer without

computing the angle

– See next slide

Nov 2012 N. H. N. D. de Silva 14

10pp

21pp

10pp

21pp

Page 13: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Q2: Turning Left?

Nov 2012 N. H. N. D. de Silva 15

Page 14: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Q3: Segment Intersection

• Do line segments and intersect?

• Definition

– A segment straddles a line L if point p1

lies on one side of L and point p2 lies on the

other side

– Boundary case: p1 or p2 lies directly on L

Nov 2012 N. H. N. D. de Silva 16

21pp

43pp

21pp

Page 15: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Q3: Segment Intersection

• Two line segments intersect iff either or both conditions below hold

– Each segment straddles the line containing

the other

– An endpoint of one segment lies on the other

segment (boundary case)

Nov 2012 N. H. N. D. de Silva 17

Page 16: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 18

Page 17: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 19

In lines 1-4 of the main algorithm (previous slide),

relative orientation di of each endpoint pi with

respect to the other line segment is computed.

Page 18: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 20

Page 19: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Intersecting Segment Pairs

• Given a set of line segments, is there a pair that intersects? (Does any exist?)

• Uses “sweeping” (left-to-right) technique

– Complexity O(n lg n), n= # of segments

• Assumptions

– No segment is vertical

– No three segments intersect at a single point

• Can order segments (“total pre-order”)

Nov 2012 N. H. N. D. de Silva 21

Page 20: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Sweeping with Sweep Lines

Nov 2012 N. H. N. D. de Silva 22

Page 21: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Moving the Sweep Line

• Sweeping algorithms manage 2 data sets

– Sweep-line status

• Gives the relationship among objects that the

sweep line intersects; status changes occur only at

event points

– Event-point schedule

• A sequence of points (event points), ordered L-to-

R based on x-coordinates. As sweep progresses LR, when sweep line reaches the x-coordinate of

an event point, sweep halts; event point

processed; then resumes

Nov 2012 N. H. N. D. de Silva 23

In our algorithm, all event points determined before sweep

Page 22: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 24

Page 23: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 25

Page 24: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Convex Hull

• Convex Hull CH(Q) of a set Q of points

– The smallest convex polygon P for which

each point in Q is either on the boundary of P

or in its interior

– Convex polygon: given any 2 points on its

boundary or in its interior, all points on the line

segment between them are contained in the

boundary or interior

Nov 2012 N. H. N. D. de Silva 26

Page 25: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Convex Hull

• Intuitively

– A point in Q is a nail sticking out from a board

– Convex hull is the shape of a tight rubber

band that surrounds all the nails

• Assume: all points in Q are unique, Q has

at least 3 points that are not colinear

Nov 2012 N. H. N. D. de Silva 27

Page 26: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Example

Nov 2012 N. H. N. D. de Silva 28

Page 27: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Finding the Convex Hull

• Two algorithms

– Both outputs the vertices of the convex hull in

counter-clockwise order, use rotational sweep

(other methods exist)

• Graham’s Scan

– Takes O(n lg n) time given n points as input

• Jarvis’s March

– Runs in O(nh) time, h is the # vertices in CH

Nov 2012 N. H. N. D. de Silva 29

Page 28: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Graham’s Scan

• Maintains a stack S of candidate points

– Each point is pushed to the stack once

– Eventually pops points not in the CH(Q)

– Finally, S from bottom to top contains vertices

in CH(Q) in the counter-clockwise order

• |Q| ≥ 3

• Requires functions

– Top(S), Next-to-Top(S)

Nov 2012 N. H. N. D. de Silva 30

Page 29: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 31

Page 30: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 32

Example (Fig 33.7 in CLRS)

Page 31: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 33

Example …contd

Page 32: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 34

Page 33: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Javis’s March

• Uses package (gift) wrapping technique

– Intuitively

• Simulates a wrapping a taut piece of paper around

the set Q, starting with the lowest point p0,

continue around keeping it taut

• Runs in O(nh) time

– h is the # vertices in CH(Q)

– When h is O(lg n), Jarvis’s March is faster than Graham’s scan

Nov 2012 N. H. N. D. de Silva 35

Page 34: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Nov 2012 N. H. N. D. de Silva 36

Page 35: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

References

• The lecture slides are based on the slides

prepared by Prof. Sanath Jayasena for this class

in previous years.

• Mainly: CLRS book, 3e

– Part VII: Selected Topics

– Chapter 33: Computational Geometry

• Slides from Alon Efrat

• Applet by Rashid Bin Muhammad • http://www.personal.kent.edu/~rmuhamma/Compgeometry/M

yCG/ORourke/ORourkeCompGeom.htm

37 Nov 2012 N. H. N. D. de Silva

Page 36: CS4522 Advanced Algorithmsnisansa/Classes/01...Sweeping algorithms manage 2 data sets ± Sweep-line status Gives the relationship among objects that the sweep line intersects; status

Conclusion

• We discussed a few problems and algorithms in computational geometry – Line segments, cross products

– Line segment intersections

– Convex hull

• Graham’s Scan

• Jarvis’s March

• Next time

– Last lecture: Parallel/Multi-threaded

algorithms

38 Nov 2012 N. H. N. D. de Silva