Download presentation
Presentation is loading. Please wait.
Published byJanice Angelina Marshall Modified over 9 years ago
1
Lecture 3: Reduce to known problem Convex Hull (section 33.3 of CLRS). Suppose we have a bunch of points in the plane, given by their x and y coordinates. The convex hull is the "smallest" convex subset that contains all the points. Here "convex" means that the line segment joining any two points inside the set stays completely inside. To make life easier, we'll assume that no three points are colinear. One way to "compute" the convex hull is to bang nails into a board at all the positions, put a rubber-band around them, and let go. But failing a simulation of the physics, we want to find a subset of the given points that, when connected, forms a convex polygon that contains all the given points. Convex hulls are one of the most basic objects in computational geometry. They are used everywhere in computer graphics and computer-aided design. For example, since it's pretty easy to determine whether a point lies inside a given convex polygon, convex hulls can be used to determine exactly which object on a screen you're pointing to with your mouse.
2
Convex hull It's not hard to see that a line segment joining two points is one of the edges of the convex hull if and only if all the other points fall to one side of the line joining the two points. This suggests the following naïve approach: for each pair of points, form the line joining them and check if all the other points lie on one side of this line. But how do we tell if a point lies on one side of a line? One way is to determine the equation of the line, say Ax + By + C = 0, and then substitute the coordinates of the point. The sign of the result says which side of the line you are on. If all the signs are the same, then all the points are on one side of the line.
3
Given 4 points: (3,2), (1,1), (1,3), (3,1) To draw a line between (1,1) and (3,2), we substitute them into y=kx + b Thus solving: 1=k+b 2=3k+b We get k=1/2, and b=1/2, thus y=1/2 x + 1/2 - x + 2y -1 = 0 Putting (1,3), (3,1) in this: -1 + 2*3 – 1 = 4 - 3 + 2*1 – 1 = -2
4
Convex hull -- continues Here is another way that is cool. Let the line segment join P 1 = (x 1, y 1 ) to P 2 = (x 2, y 2 ), in that order, and let the point P 3 = (x 3,y 3 ). Then compute the determinant of the matrix [ x 1 y 1 1 ] M = [ x 2 y 2 1 ] [ x 3 y 3 1 ] which is x 1 y 2 + y 1 x 3 + x 2 y 3 - x 3 y 2 - y 3 x 1 - x 2 y 1. Then the sign of this is 0 if P 3 is to the left of the directed line. Remember, det(A) = ∑ j=1 n A i,j (-1) i+j M i,j where M i,j is the determinant of the matrix resulting from A by removing i-th row and j-th column.
5
Convex hull -- continues So we can tell whether a point is to one side of a line or the other in O(1) steps. Thus, the naive algorithm, which forms lines by all pairs of points, and then compares each line to all the other points, costs O(n 3 ) time. Can we do better? Yes, using the "gift-wrapping" method (also called Jarvis' march). Start at some extreme point (e.g., the topmost point). Test each point in turn to find the one that makes the largest angle of a leftmost turn. There is a nice demo http://www.cs.princeton.edu/courses/archive/spr10/cos226/demo/ah/Jar visMarch.html. http://www.cs.princeton.edu/courses/archive/spr10/cos226/demo/ah/Jar visMarch.html How do we find the angle? We use the fact that if u and v are vectors, then the angle θ between them is given by u · v = || u || || v || cos θ where · means the dot product of the two vectors and || u || denotes the length of the vector. How much time does this take? Well, luckily we don't need to compute the arc cosine of the angle, we can just compare the cosine of the angle instead. Actually, even the (cosine of the angle) squared is enough, so we don't even have to do square roots. So the total cost is O(nh), where h is the number of points on the boundary of the convex hull. This is O(n 2 ) at worst.
6
Convex hull continues Can we do even better? Yes. Again, we reduce to a known problem: sorting. We start by finding the topmost point P. Now connect P with every other point. All these lines form some angle with the horizontal line through P, so sort the remaining points by this angle. Now process the points starting with one that form the smallest angle. Add each point in turn with the next higher angle. If doing so causes us to make a left turn, continue. Otherwise, we made a right turn, and the point in the middle can't be part of the hull. So discard it, and join the previous point to the new point. Backtrack, discarding points as you go until you end up making a left turn again. This is called "Graham's scan" and there is a nice applet demonstrating it http://www.cs.princeton.edu/courses/archive/spr10/cos226/demo/ah/Gr ahamScan.html http://www.cs.princeton.edu/courses/archive/spr10/cos226/demo/ah/Gr ahamScan.html How much time does this take? Well, the sorting takes O(n log n). Once sorted, we have to process each point, and in the worst case, processing a point might backtrack through almost all the previous points. So it looks like it might take O(n 2 ) time. But in fact, when we backtrack, the points are discarded, so we never have to look at them again. So each point gets considered at most twice: once when we add it, and another time when we (possibly) discard it. So the total cost is just O(n). The total cost for Graham's scan is therefore O(n log n).
7
Lower bound on Convex hull We have an O(n log n) algorithm for convex hull based on sorting. (Tim Chan: O(n log h) where h is number of points on the convex hull.) Is it best possible? We can prove it is best possible by using the Ω(n log n) lower bound on sorting. We need to show that, given a routine to solve convex hull, we could use it to sort. Here's how to do it: given a list of points, we can project each point onto a parabola, by converting k to (k,k 2 ). Thus, if we knew the convex hull, we can list its vertices in order, hence we know how to sort the original list.
8
Next topic: #2 recursion Warm up question: on a line of length n, you start from one end, and each time you can go forward by 1 step or 2 steps. How many different ways you can take to walk to the end of the line? We can write a recurrence: F(n) = F(n-1) + F(n-2) i.e. go forward 1 step F(n-1) go forward 2 steps F(n-2) This has a name: Fibonancci numbers: 0,1,1,2,3,5,8,13,21,34,55,89, … In general F(n) = Θ(a n ), a=(1+√5)/2.
9
Number of PetalsFlower 3 petals (or 2 sets of 3) lily (usually in 2 sets of 3 for 6 total), iris 5 petals buttercup, wild rose, larkspur, columbine (aquilegia), vinca 8 petals delphinium, coreopsis 13 petals daisy, ragwort, marigold, cineraria 21 petals daisy, aster, black-eyed susan, chicory 34 petals plantain, daisy, pyrethrum 55 petals daisy, the asteraceae family 89 petals daisy, the asteraceae family Many things are Fibonacci-like in nature.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.