 Hello and welcome to this next lecture on data structures and algorithms. We will continue our discussion of geometric algorithms. In the last discussion, we tried answering questions such as whether a point P1 turns clockwise or counterclockwise from a point P2 with respect to some point P0 and we had a very simple cross product or determinant based formula for answering such questions based on which we also answered slightly trickier questions such as whether two line segments P1, P2 and P3, P4 intersect at all. Today, we will discuss another flavor of computational geometry algorithms where the task is to generate a new geometric form from a given set of points or a structure and the specific form, we talk about today is the convex hull. So, the convex hull of a set of points Q is the smallest convex polygon P for which each point in Q is either on the boundary of P or in its interior. Let us illustrate with an example. So, let us say these were the points. So, the convex hull which is the tightest smallest convex polygon could be obtained by enclosing these points with a rubber band stretch around all of them. So, you want to enclose all of them, but as tightly as possible. So, you probably need to ensure that you select the topmost point and the leftmost point. The rest can be successively covered. You can find that these two points should be covered. So, should these two points and these two. One can imagine the rubber band that has been stretched to just enclose these points to be the convex hull. Now, this can be computed using a technique called rotational sweep. We will discuss one flavor through the well-known Graham's scan algorithm. So, Graham's scan does the following given a set of points Q. It starts with a point P 0 which has the lowest y coordinate which is the point which is at the bottom. So, as already motivated on the previous slide, you might want to first include the points that are either extreme lift or on the extreme right or on the extreme top or on the extreme bottom. What Graham's scan does are as what one version of Graham's scan does is starts with the bottom most point. We leave it as homework to develop variants of Graham's scan that start with the leftmost point or the rightmost point or the topmost point. For the sake of the remaining discussion, we will start with this bottom most point the point with the lowest y coordinate. In case there is a tie which is in case there are two points which have the same smallest y coordinate which could be the case here. You will break the tie by selecting the one with the smallest x coordinate as well. So, let us stick to the story. What would you do next? Well, we would like to look at the orientation of all the other points with respect to the point that is already chosen that is with the new point what would we like to do next? Well, we can do the following. We can look at the orientation of the remaining points, sort them in some order of orientation and thereafter decide how to construct the convex hull in incremental manner. In particular, we will resort to a counter clockwise inclusion of points. Let me explain this a little bit more carefully. So, we are going to sort the remaining points of Q by their polar angle relative to P0 in counter clockwise order by comparing cross products. What does this mean? So, let us say the point that we have already included here P0 which is the point with the smallest y coordinate is here and let us look at P1 here, P2 here, P3 and so on. What we are going to do is compare cross products which is P1 with the smallest x coordinate minus P0 cross product with P2 minus P0 P1 minus P0 cross product with P3 minus P0 and so on. So, very implicitly we have chosen as our frame of reference the point to the lowest y coordinate and the point to the lowest x coordinate P0 and P1 respectively and we are going to look at cross products of Pi minus P0 with P1 minus P0 and we are going to sort those remaining points. What would we expect? Well, we will expect that any point Pi minus P0 its cross product with P1 minus P0 is P1 minus P0 cross product Pi minus P0 this we expect to be positive if and only if P1 is clockwise from Pi with respect to P0 and the value of this cross product something that we have ignored so far will give you how much P1 is clockwise from Pi with respect to P0. So, if you have where to compute this cross product for all points Pi and we of course expect to sort the points and if we were to sort these points based on increasing values of this cross product then we would actually get the points sorted in counter clockwise order with from P1. So, the larger the magnitude more is Pi counter clockwise rotated from P1 with respect to P0. So, again recall that P1 is the point with smallest x coordinate what you do next well now we will be interested in inspecting points in the order in which they are sorted. So, we are going to include P1 we look at P2 and then look at P3 for inclusion when would we include P2 and when would we consider including P3 given that P0 and P1 are already included. So, we look at the angle suspended at P2 from P3 and from P1 what we find here is with respect to P2 P3 is counter clockwise from P1. On the other hand if P2 had been up here then one could find that P1 is actually counter clockwise from P3 with respect to P2 or rather P3 is clockwise from P1 with respect to or rather P3 is clockwise from P1 with respect to P3. So, P2 will be excluded if the new point happens to be counter clockwise from the previous point with respect to the point P2. So, in the specific case given this counter clockwise rotation we will drop P2 and move on to P3 to decide whether to include P3 we will of course also need to look at P4 and see if P4 is rotated clockwise from P1 with respect to P3. If P4 is rotated clockwise from P1 with respect to P3 you will include P3 else we will exclude it. So, the way we keep track of the last three points or the three points on which we need to make comparisons is through the stack. So, we will at any point of time maintain in the stack points which we think could belong to the convex hull. So, stack S is subset of points which could potentially belong to the convex hull of Q. We know for sure that P0 belongs to the convex hull and so does P1. So, what needs to be inspected is P2 and we will compare P2 for its inclusion by looking at the angle suspended at P2 by the new point P3 which is a query point. So, again recap we have P2, we have P1 and we have P3 an angle of the sort which indicates a counter clockwise rotation of P3 from P1 with respect to P2 will mean that P2 should get excluded. On the other hand if P2 were such that P3 is rotated clockwise from P1 with respect to P2 then P2 should also get included. This is summarized here if the angle formed by points P i we start with i equals 3 and the first two points on the stack in that order is in the clockwise direction then pop the top post element from the stack else push P i into the stack. So, finally the stack contains all the points from bottom to top exactly the vertices of the convex hull Q. We illustrate this. So, here is a complete example we have included P0 and P1 should be include P2 as discussed we are going to look at the angle suspended by P3 at P2 and look for the rotation of P3 from P2 we find that P3 is counter clockwise rotated from P1 with respect to P2 and therefore, P2 can be dropped. So, this angle is not working in favor of P2 we drop P2 include P3 in the stack look at P4. So, now the stack S has the following elements S is P0 P1 P3 we know that P0 and P1 should be included. So, we always look at the top element as a candidate for inclusion or exclusion in conjunction with the new point that is being considered which is P4, but we will use the previously included point P1 this is the second last element on the stack to check for whether P3 should be included or not and that is what we will do here. The angle suspended at P3 we find that P4 is rotated clockwise from P1 with respect to P3. So, this means P3 will be included what about P4 well we find that P4 simply cannot be a part of the convex hull because it is actually in the interior of the convex hull by virtue of this P5 being counter clockwise rotated from P3 with respect to P4. So, we drop P4 include P3 move on to P5 we next check if P5 should be included and we find that indeed P6 is oriented clockwise from P3 with respect to P5 whether to include P6 or not we will know soon, but we proceed and then we check for P7 and again we find that P7 is oriented clockwise from P5 with respect to P6. So, we continue our journey look at P8 we seem to be heading wrong are we going to correct this well yes pretty soon. So, at P8 we still do not detect the problem, but when we consider P9 and look at the angle suspended by P9 at P8 with respect to P7 we find that now suddenly P9 is oriented counter clockwise with respect to P7 at P8. So, this means we need to pop P8 drop P8 and in fact when you go to P10 we are going to drop a whole lot of the other points we are going to drop P7 P6 P5 and so on and actually connect P3 to P10 directly. So, we keep popping points out of the stack as long as they result in a counter clockwise motion because we found new point that can be included to be consistent in the counter clockwise inclusion and as we find points that lead to clockwise motion around the point we keep them in the stack for verification later on and that is what happened here we included P10 in the stack we have dropped all the other points. Now, we look at P11 should P10 continue remaining on the stack well P11 is oriented clockwise from P3 with respect to P10 because P3 is still if we see the second last element in the stack. So, one might want to recall the stack now contains P0 P1 P3 and P10 the new point that we are looking for inclusion is P11 we know for sure that P0 P1 and P3 are part of the convex hull query is about P10 and P11 and yes there is a clockwise motion around P10 strong enough case for P10 will be included well P11 will also be included as long as there is a case for its exclusion and in the next step we find that P11 should be excluded because P12 is oriented counter clockwise from P10 with respect to P11. So, we are going to drop P11 and the last point P12 will be tested for inclusion with respect to P0 and yes we find that P0 is oriented clockwise from P10 with respect to P12. So, P12 will be included. So, here is the algorithm of finding convex hull we start with the reference point P0 with the lowest y coordinate in some sense we also have P1 as the next reference point we push P0 and P1 and then the subsequent points P2 onwards will be queried for. So, we start with P2 and the next point P3 and we check if P3 is oriented clockwise from P1 with respect to P2 and here is the depiction again we have already included P1 here is P2 for being queried here is P3 should be include P2 or not well we will include P2 and continue looking at P3 for inclusion if P3 is oriented clockwise from P1 with respect to P2 we could now generalize this to not just P1 and P2, but the last two elements of the stack. So, what was P3 is now basically PI what was P2 is the top of the stack S and what was P1 is now the second last element of the stack we retrieve that second last element by this call next to top of S we refer to the counterclockwise orientation of PI with respect to the next to top as a case for dropping the top of S we refer to this as a non-left turn. So, what the non-left turn means is that PI is oriented counterclockwise from the second last element with respect to the top of the stack we keep doing this keep popping elements as we saw in the illustration we kept dropping elements P9, P6 and P5 when we encountered P10 and we keep doing this till we encounter a clockwise orientation at this point we basically insert PI in S and we continue the complexity of this algorithm is the complexity of sorting and that turns out to be n log n there are other algorithms that give you complexity of the order of n times h where h is the size of the convex hull. So, if the convex hull turns out to be very small grams can turns out to be expensive because it is n log n. However, if it turns out that most of the points belong to the convex hull then grams can turns out to be a very good choice. Thank you.