Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-211 Fundamental Data Structures and Algorithms Klaus Sutner April 27, 2004 Computational Geometry.

Similar presentations


Presentation on theme: "15-211 Fundamental Data Structures and Algorithms Klaus Sutner April 27, 2004 Computational Geometry."— Presentation transcript:

1 15-211 Fundamental Data Structures and Algorithms Klaus Sutner April 27, 2004 Computational Geometry

2 Announcements  HW7 the clock is ticking...  Final Exam on Tuesday May 4, 5:30 pm  review session Thursday, April 29

3 Recall: Convex Hull

4 A Hull Operation Suppose P is a set of points in the plane. The convex hull of P is the least set of points Q such that: - P is a subset of Q, - Q is convex. Written CH(P). This pattern should look very familiar by now (reachability in graphs, equivalence relations,...)

5 Convex Combinations Abstractly it is easy to describe CH(P): is the set of all points X =  i P i where 0  i and  i = 1. X is a convex combination of the P i. So the convex combinations of A and B are the line segment [A,B]. And the convex combinations of three points (in general position) form a triangle. And so on.

6 So? Geometrically this is nice, but computationally this characterization of the convex hull is not too useful. We want an algorithm that takes as input a simple polygon P and returns as output the simple polygon CH(P). P CH(P)

7 An Algorithm Hence we can find the convex hull of a simple polygon: for all n points we eliminate non-extremal points by trying all possible triangles using the membership test for convex polygons. In the end we sort the remaining extremal points in counterclockwise order. Unfortunately, this is O(n 4 ). Could it be faster? When do we get n 4 ?

8 CH Algorithms Let us assume that the input is given as a set (list) P of n points p 1,p 2,...,p n in the plane. We need to compute the extremal points and ouput them in some natural order (say, traversing the boundary of the hull counterclockwise, starting at the south-east corner). However, the input may not be ordered in any particular way.

9 Application: Membership Test The output convention makes algorithmic sense: we can then test in logarithmic time whether a point lies in the convex region. Q Q

10 Membership Test c't Find central point p, then use binary search to identify the sector that Q belongs to: between rays [p,p i ) and [p,p i+1 ). Check if Q lies in the corresponding triangle. p Q Q p i+1 pipi

11 Computation and Physics Note that there is a simple analogue algorithm to solve the convex hull problem: use a board, nails and a rubber band. Is linear (analog) time.

12 Some CH Algorithms

13 Jarvis' March Suppose we already have an extremal point p 0 on the boundary of the convex hull. Assume wlog that p 0 is the origin and that all points lie in the upper half plane. Why is this OK? p0p0

14 Gift Wrapping Then find the other extremal points as follows: tie a string to p 0 and then wrap it all around P. extremal p 0 p 1 p 2 p 3 p 4 p0p0 p1p1 p2p2 p3p3 p4p4

15 Complexity This can be implemented in O( (n+e) n ) steps where e is the number of extremal points (so e = O(n)). p0p0 p1p1 p2p2 p3p3 p4p4

16 Graham Scan Here is another idea: order the points with respect to their polar coordinates. As origin use either an interior point or, say, the lowest extreme point. p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 p6p6

17 Graham-Scan p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 The march around the points in polar order. Keep track of the last two edges. We would like to see a sequence of left turns.

18 Graham-Scan p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p7p7 If there is a right turn, the previous point was not extremal and has to be discarded (back out).

19 Graham-Scan Stack p 0 p 1 p 2 p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 Stack p 0 p 1 p 3 p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p7p7

20 Graham-Scan p6p6 Stack p 0 p 1 p 3 p0p0 p1p1 p2p2 p3p3 p4p4 p5p5 p7p7 Stack p 0 p 1 p 3 p 4 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2

21 Stack p 0 p 1 p 3 p 4 p 5 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2 Graham-Scan Stack p 0 p 1 p 3 p 4 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2

22 Graham-Scan Stack p 0 p 1 p 3 p 6 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2 Stack p 0 p 1 p 3 p 4 p 5 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2

23 Graham-Scan Stack p 0 p 1 p 3 p 6 p0p0 p1p1 p3p3 p4p4 p5p5 p6p6 p7p7 p2p2 Stack p 0 p 1 p 3 p 6 p 7 p0p0 p1p1 p3p3 p4p4 p6p6 p7p7 p2p2 p5p5

24 Graham-Scan Stack p 0 p 1 p 3 p 6 p 7 p0p0 p1p1 p3p3 p4p4 p6p6 p7p7 p2p2 p5p5 Stack p 0 p 1 p 3 p 6 p 7 p0p0 p1p1 p3p3 p4p4 p6p6 p7p7 p2p2 p5p5

25 Graham-Scan Stack p 0 p 1 p 3 p 6 p 7 p0p0 p1p1 p3p3 p4p4 p6p6 p7p7 p2p2 p5p5

26 Running Time Graham Graham scan can be implemented in O( n log n ) steps. - O(n) steps to find the anchor point (or O(1)). - O(n log n) steps to sort the points. - O(1) for the local tests. - O(n) steps in the traversal: touch every point at most twice.

27 Jarvis and Graham Jarvis' March and Graham's Scan are rather similar. Both are somewhat related to sweep-line techniques. Sometimes called “rotational sweep”.

28 Correctness Graham Best shown using a loop invariant: The CH of the points so far inspected is spanned by the points in the stack. The algorithm does precisely what is necessary to maintain this invariant. p0p0 p1p1 p3p3 p4p4 p5p5 p7p7 p2p2

29 Exercise Give a correctness proof for Jarvis’ algorithm. p0p0 p1p1 p2p2 p3p3 p4p4

30 Lower Bound for CH O( n log n ) is optimal for convex hull. Show that Sorting can be reduced to Convex Hull. Consider n positive real numbers x i. Define p i = (x i,x i 2 ). Easy to see: are all extremal. Traversing the boundary of CH(P) produces a sorted list.

31 Polar Coordinates It is important not to waste time in the coordinate computations. Cartesian (x,y)  polar (r, ) r = Sqrt( x 2 + y 2 )  = arctan( y/x ) arctan expensive and not needed! Instead find quadrant of point, then within each quadrant use slopes for angle comparisons.

32 Quick Hull Divide-and-Conquer is supposedly a universal algorithm design tool. Does it apply to convex hulls? We need to break the points into two groups, get the hulls of both sets and then construct the total hull from the pieces. To get O(n log n) the overhead must be linear.

33 Quick Hull Idea Find two points p and q such that [p,q] is a chord in the convex hull of P. Split the points into “above” and “below” the baseline [p,q].

34 Upper Half Find the point r furthest from the baseline. Discard all points in the triangle (p,q,r).

35 Repeat In the end only the extremal points are left over.

36 Comments The divide step is clearly linear (in the number of points on that side). Combining the solutions is trivial here: we can just join the lists of extremal points. This is very similar to Quicksort.

37 Analysis When does this approach work well? When does it work poorly? What is the running time in general? Can we force fast behavior?

38 Recall: Motion Planning Our motion planning problem is slightly related to convex hulls: Given a collection P of obstacles in space-time, we want to compute the c-hull of P: c-hull(P) = { p position | robot at p is doomed } c here is the maximum speed, so -hull(P) = P. Note that this is also a hull operator, just like CH: P subset of c-hull(P) c-hull( c-hull(P) ) = c-hull( P ) Smallest such set with certain closure properties.

39 c-Hull But the geometry of the c-hull is more complicated than the convex hull. E.g. think about rotations. Still can use a sweep-line algorithm to compute the c- hull in O(n log n) steps.

40 Obstacle Avoidance Once the c-hull has been computed we can avoid the obstacles by “sliding along” the hull (though there may be better paths).

41 Exercise Figure out which closure property defines the c-hull. It should be analogous to the condition for CH: p,q in set implies [p,q] in set.


Download ppt "15-211 Fundamental Data Structures and Algorithms Klaus Sutner April 27, 2004 Computational Geometry."

Similar presentations


Ads by Google