Sweep Line

• Event C: Intersection point – Report the point. – Swap the two intersecting line segments in the sweep line status . – For the new upper segment: test it against its predecessor for an intersection. – Insert intersection point (if found) into event queue. – Similar for new lower segment (with successor). • Complexity: – k such events – O(lg n) each – O(k lg n) total.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: and Data Structures 550 Example

a3 b4 Sweep Line status: s3 e1 s4 s0, s1, s2, s3 Event Queue: a2 a4 b3 a4, b1, b2, b0, b3, b4 s2 b1 b0 s1 b2 s0 a1 a0

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 551 Example

a3 b4 Actions: s3 Insert s4 to SLS e1 s4 Test s4-s3 and s4-s2. a2 a4 b3 Add e1 to EQ s2 b1 Sweep Line status: b0 s1 b2 s0, s1, s2, s4, s3 s0 a1 a0 Event Queue: b1, e1, b2, b0, b3, b4

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 552 Example

a3 b4 Actions: s3 e1 s4 Delete s1 from SLS Test s0-s2. a2 a4 b3 Add e2 to EQ s2 Sweep Line status: e2 b0 b1 s1 b2 s0, s2, s4, s3 s0 a1 a0 Event Queue: e1, e2, b2, b0, b3, b4

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 553 Example

a3 b4 Actions: s3 e1 s4 Swap s3 and s4 in SLS Test s3-s2. a2 a4 b3 Sweep Line status: s2 s0, s2, s3, s4 e2 b0 b1 s1 b2 Event Queue: s0 a1 a0 e2, b2, b0, b3, b4

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 554 Complexity Analysis

• Total time complexity: – O((n+k)lgn). – Best case: k = O(1), then this is much better than the naive algorithm. – Even for k ≈ n, this is still the case. – Worst case: k ≈ n 2, then this is slightly worse than the naive algorithm. • Total space complexity: – Event queue: min- • O(n+k) – Sweep line status: red-black • O(n). – Total: O(n+k).

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 555 6.2 Convex Hull

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 556 Thursday March 12, 2015 557 ConvexConvex HullHull

• The convex hull of a set of points is the smallest convex polygon containing the set of points. • A convex polygon is a non-self-intersecting polygon whose internal angles are all convex (i.e., less than 180 degrees) • In a convex polygon, a segment joining any two vertices lies entirely inside the polygon.

segment not contained!

convex nonconvex

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 557 Thursday March 12, 2015 558 ConvexConvex HullHull

• Think of a rubber band snapping around the points • Remember to think of special cases – Collinearity: A point that lies on a segment of the convex is not included in the convex hull

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 558 Thursday March 12, 2015 559 ApplicationsApplications

• Motion planning – Find an optimal route that avoids obstacles for a robot •Bounding Box – Obtain a closer bounding box in computer graphics • Pattern Matching – Compare two objects using their convex hulls

obstacle

Jacobs University end Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 559 Thursday March 12, 2015 560 FindingFinding aa ConvexConvex HullHull

One algorithm for determining a convex polygon is one which, when following the points in counterclockwise order, always produces left-turns

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 560 Thursday March 12, 2015 561 CalculatingCalculating OrientationOrientation

• The orientation of three points (a, b, c) in the b plane is clockwise, counterclockwise, or collinear c CW – Clockwise (CW): right turn – Counterclockwise (CCW): left turn a – Collinear (COLL): no turn c • The orientation of three points is characterized by the sign of the determinant ∆(a, b, c) b CCW xa ya 1 a

Δ(a,b,c) = xb yb 1 c b xc yc 1 COLL a function isLeftTurn(a, b, c): return (b.x - a.x)*(c.y - a.y)– (b.y - a.y)*(c.x - a.x) > 0 Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 561 Thursday March 12, 2015 562 ExamplesExamples

Using the isLeftTurn() method: b (0.5,1)

(0.5-0) * (0.5-0) - (1-0) * (1-0) c (1, 0.5) CW = -0.75 (CW) a (0,0)

c (0.5,1) (1-0) * (1-0) - (0.5-0) * (0.5-0) b (1, 0.5) CCW = 0.75 (CCW) a (0,0) c(2,2) (1-0) * (2-0) - (1-0) * (2-0) b(1,1) = 0 (COLL) a(0,0) COLL

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 562 Thursday March 12, 2015 563 OrientationsOrientations andand convexconvex hullhull

•Let a, b, c be three consecutive vertices of a polygon, in counterclockwise order – b’ is not included in the hull if a’, b,’ c’ is non-convex, i.e. orientation(a’, b’, c’) = CW or COLL – b is included in the hull if a, b, c is convex, i.e. orientation(a, b, c) = CCW, and all other non-hull points have been removed

c’ c

b’ a’ b

a

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 563 Thursday March 12, 2015 564 GrahamGraham ScanScan AlgorithmAlgorithm

• Find the anchor point (the point with the smallest y value) • Sort points in CCW order around the anchor • We can sort by increasing angle of the line segments from the anchor to the points with the x-axis

7 4 5 6 3

8 2 Anchor 1

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 564 Thursday March 12, 2015 565 GrahamGraham ScanScan AlgorithmAlgorithm

• The polygon is traversed in sorted order •A sequence H of vertices in the hull is maintained. •For each point a, add a to H. • While the last turn is a right turn, remove the second-to-last point from H – In the image below, p,q,r forms a right turn. –Thus q is removed from H. – Similarly, o,p,r forms a right turn. –Thus p is removed from H. r r o r o p p n

q H H H

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 565 Thursday March 12, 2015 566 GrahamGraham ScanScan

function graham_scan (pts): // Input: Set of points pts // Output: Hull of points find anchor point sort other points in CCW order around anchor hull = [] for p in pts: add p to hull while last turn is a “right turn” remove 2nd to last point add anchor to hull return hull

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 566 Thursday March 12, 2015 567 AnalysisAnalysis

function graham_scan(pts): // Input: Set of points pts // Output: Hull of points find anchor point // O(n) sort other points in CCW order around anchor // O(n lg n) hull = [] // O(1) for p in pts: // O(n) add p to hull // O(1) while last turn is a “right turn” // O(1) // Each point is looked at, at most, twice: // 1x left-turn, 1x right-turn. remove 2nd to last point // O(1) add anchor to hull // O(1) return hull Overall run time: O(n lg n) Space complexity: O(n) [often a stack is used] Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 567 Thursday March 12, 2015 568 IncrementalIncremental AlgorithmAlgorithm

• What if we already have a convex hull, and we just want to add one point q? • Get the angle from the anchor to q and find points p and r, the hull points on either side of q •If p,q,r forms a left turn, add q to the hull •Check if adding q creates a concave shape – If you have right turns on either side of q, remove vertices until the shape becomes convex – This is done in the same way as the static Graham Scan

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 568 Thursday March 12, 2015 569 IncrementalIncremental AlgorithmAlgorithm Original Hull: Want to add point q: Find p and r: q q p, q, r

p H

o, p, q r n H H q forms ao left q forms a right q forms a right turn, so add q: turn, so remove p turn, so remove o s n, o, q n o n

p s

H H H r r r s Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 569 Thursday March 12, 2015 570 IncrementalIncremental AlgorithmAlgorithm

Since m, n, q is a left Now we look at the Since q, r, s is a left turn, we’re done with thatq other side: q turn, we’re done! side. q

n m

H sH r r s H • Remember that you can have right turns on either or both sides, so make sure to check in both directions and remove concave points!

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 570 Thursday March 12, 2015 571 AnalysisAnalysis

•Let n be the current size of the convex hull, stored in a around the anchor for efficiency •To check if a point q should be in the hull, we insert it into the tree and get its neighbors (p and r on the prior slides): O(lg n) • We then traverse the ring, possibly deleting d points from the convex hull: O((1 + d) lg n) • Therefore, incremental insertion is O(d log n) where d is the number of points removed by the insertion

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 571 A Divide-and-Conquer Approach

• First, sort all points by their x coordinate. • Then divide and conquer: – Find the convex hull of the left half of points. – Find the convex hull of the right half of points. – Merge the two hulls into one.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 572 A Divide-and-Conquer Approach

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 573 Merging Hulls

Idea: • Find the two lines that are upper and lower tangent to the two hulls. • Then, remove points that are cut off.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 574 Finding upper tangent line • Start with the right-most point of the left hull and the left-most point of the right hull. • While the line is not an upper tangent to both hulls – While the line is not an upper tangent to the left hull • Move to next point of left hull counter-clockwise. – While the line is not an upper tangent to the right hull • Move to next point of right hull clockwise.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 575 Finding lower tangent line • Start with the right-most point of the left hull and the left-most point of the right hull. • While the line is not an lower tangent to both hulls – While the line is not an lower tangent to the left hull • Move to next point of left hull clockwise. – While the line is not an lower tangent to the right hull • Move to next point of right hull counter-clockwise.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 576 Checking Tangentness • How can we check whether a line is an upper or lower tangent? • Let‘s check upper tangentness for left hull:

– pr,pl,pl-ccw should be a left turn • Other cases are checked analogously

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 577 Lower Tangent Example

• Initially, T=(4, 7) is only a lower tangent for A. • The A loop does not execute, but the B loop increments b to 11. • But now T=(4, 11) is no longer a lower tangent for A, so the A loop decrements a to 0. • Again, T=(0, 11) is not a lower tangent for B, so B is incremented to 12. • T=(0, 12) is a lower tangent for both A and B, so T is returned.

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 578 Analysis

• Preprocessing: Sort the points by x-coordinate. –O(n lg n) • Let T(n) be the time complexity for n sorted points. • Divide: Split the set of points into two subsets of (almost) same size. –O(1) • Conquer: Recursive calls of algorithm for two subproblems of about half the size. –2 T(n/2) • Merge: Combine the two hulls by finding upper and lower tangent and removing points in between. –O(n)

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 579 Analysis

• Recurrence: T(n) = 2 T(n/2) + c n • Solution (Case 2 of Master theorem) T(n) = O(n lg n) • Altogether, pre-processing and divide-and-conquer approach lead to O(n lg n).

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 580 7. Outro

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 653 Algorithmic concepts

• Divide & Conquer • • Greedy Approaches

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 654 Efficient algorithms •Sorting •Searching • Power of a number • Fibonacci numbers • Matrix multiplication •VLSI layout • Longest Common Subsequence • Activity Selection problem •Knapsack problem •BFS and DFS • Minimum Spanning Trees • Shortest Paths •Maximum Flow • Line-segment Intersections •Convex Hull • Voronoi Diagrams

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 655 Runtime analysis

• Upper bounds •Lower bounds •Tight bounds • Solving recurrences

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 656 Data structures

•Array • Stack •Queue • • Trees •BST • Red-black Trees •Heap •Hash Tables •Graphs

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 657 Memory analysis

•Also asymptotic bounds • Additional memory •In-situ algorithms

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 658 The End

Jacobs University Visualization and Computer Graphics Lab

CH08-320201: Algorithms and Data Structures 659