 Welcome back, in the last segment we discussed a scheme for dispatching taxis, that is matching taxis to customers. Now in that scheme, that scheme was, first of all that scheme was perfectly fine in the sense that it did the job, but it seems that the scheme can be improved. So let us see why. So here what we did was that after a customer came in, we assigned the waiting drivers but then after that we sort of moved all waiting drivers one step forward. This does not seem, this seems like a little bit of an unnecessary thing. So you will see that we can essentially eliminate this movement. So there might be maybe say 400 drivers waiting, so then we are doing these 400 moves, so why should we do that? So you will see now that we can do without actually moving so many drivers forward. So here is the new idea. So the previous program seems to be copying data a lot. So we are going to avoid copying. So for this we are going to look at what might happen without computers. So this is actually something that you should think about anyway. So if you are trying to solve a problem and practically anything that you do in this course, you are going to do something which you probably would do manually, you are not going to do something which is terribly different from what you do manually. So therefore figuring out look what would I do manually. So in this case would I do the equivalent of that copying manually that is something that you should ask. So what would you do manually? So maybe you might write the names on a blackboard. So what will that look like? So this is my blackboard and then I start writing the names of the arriving drivers from the top. So I wrote that and maybe at some point a customer comes. So what will I do? I will take the first driver over here and give it to the customer, give that driver to the customer and maybe I will erase this name on the blackboard. So I cannot erase when I write it in pen and paper but if it is actually a blackboard I can wipe it off. So I am not going to erase this and push everyone up. I am just going to erase it and say that look now really the front of the cube is over here. If one more customer comes I will erase a little bit more and I will keep going in this manner. But suppose a new driver comes so then I will put down the driver information over here. I will put down the driver information over here, here, here, here all the way till this point. What happens if I come down all the way and a new driver comes? Well in that case I may start from the top. The new driver information would be put at the top. So now this is an interesting way in which the information is given over here. The information starts from over here, goes down and then continues up from the top. So you can think of this as some kind of a circular cube. So it sort of starts over here and comes back over here. So think of that blackboard as sort of a rolling as a circular board. So because it is a circular board we need to worry a little about where is the first person. So we know that the first person is over here. If that person, if this driver is given to a customer then the first person becomes next. But we are really not shifting up the driver names. So that is exactly what we are going to do in our solution. So arriving driver ID is written top to bottom. When board bottom reached we begin from top if the drivers have left. Of course if the drivers have not left then we cannot do anything. We have to tell the new driver that look my cube is full I do not have space to write down your number and so please wait, you please try a little bit later. But usually this will not happen because we will allocate a reasonably large amount of space anyway. So we are going to think of this driver ID as a circular array. What that means is that the next position after driver ID n minus 1. So this is index 0, this is index n minus 1. So after this position the next position is this. So after this position the next position is this. After this the next position is this and so on. But after this normally you might be tempted to say that there is no next position. But no in this case we are going to say that the next position is this. So it is kind of a circular array. So that is basically the idea that we are going to implement. And again the idea is simple, it is not completely simple, it is more complicated than the previous idea and therefore we should make decisions sensibly at the beginning and then stick to those decisions. So that is what we are going to do next. So we are going to say whatever invariants are. So as before we are going to have n waiting denote the number of waiting drivers. So that variable we are still going to have. We have to keep track of how many drivers are waiting and we are also going to have this invariant that n waiting is going to lie between 0 and n. But now we are going to have a new variable called front. The front is the position of the earliest assigned driver, earliest unassigned driver. So if you look at this picture, so let us say these 2 drivers which we had assigned, so position 1, 0 and position 1 these drivers have gone away because they have been given customers. So then this position 2 is the first position where there is an unassigned driver. So really front will be equal to 2 at this time in our entire execution. So front is the index of the earliest unassigned driver. So it is 0 initially but as drivers leave this front increases. But we want front also to be between 0 and n. Well, this time we want front not to equal n because front is not the number, front is an index. So front cannot be, it cannot be n, so it can be at most n minus 1. So front is an index, so it can start at, it can be 0 all the way till n minus 1. And the first valid driver id is at driver id front. So that is what we said. Now this raises an interesting question, where is the next driver id? Should we say that it is at front plus 1? The next driver id, shall we say that it is the element driver id of front plus 1? Well, there is a bit of a problem. If front is n minus 1 as it can well be, because after all we have said that front can be n minus 1. So if it is indeed n minus 1 then front plus 1 of n which is what we are asking over here is n and that is not a valid index. So if front is n minus 1 what would we want this to be, we would want this to be 0. As we said if this is front the next one from here is 0. So therefore we are going to say that the next waiting driver is at front plus 1 mod n. So this plus 1 mod n is like going forward on a circular array. So if we keep doing that, so maybe I should explain this. If front is n minus 1 then front plus 1 is n but n mod n is 0 which is exactly what we wanted. So in this by the same logic the last waiting driver id is not at front plus n waiting minus 1 which is what would be the case if this were not a circular array. But because this is a circular array we have to take a mod n. So these are the things in green are the propositions that we have to care about these are the invariance. So we have to make sure that these statements are always going to hold. So here is a pictorial view of the same thing. What we have said over here is that this is the initial position. So this part is the entire array and this entire array is right now unused. So what I have shown over here are indices 0 all the way till n minus 1. What happens after some drivers have come in and some customers have also come in? Well if so many drivers came in and if so many customers left then this initial position becomes unused. This last position parts are unused and this middle part is occupied by waiting drivers by the id's of the waiting drivers. But now if we continue in this manner what might happen is that more and more drivers will come over here. They may also occupy the top position and maybe more and more drivers will leave. So at some point in execution the situation might resemble this that there are some occupied positions by drivers some occupied position by drivers and the middle portion is unused but that is okay. This is the front so this is the first driver position or the position of the earliest driver waiting in the system and this is the next one this is the next one and so on this is the one after that and this is the last driver that came in and after that there is this empty space. This is just to clarify what these variables represent and just give you a picture of all of that. So how do we process a driver arrival? So first of all if n waiting equals n then that means that our entire array is full of drivers. So we do not have space to put in this new driver so in which case we are going to say well our queue is full please try later. But if it is not n if we do have space so then we get the driver details. So the driver might type in some ID or maybe some phone number whatever convention we have and we are going to place it in the position where we said the next driver ID should be put in and that is front plus n waiting but mod m and whatever we read in is going to be put in in that position and at this point we will increment n waiting because we got a new driver and so there is one additional driver waiting that is it. So front plus n waiting mod n is the index of the empty position after the n end of the queue so this is just a reminder as to why we use this over here we had discussed this earlier that this is the position where the next driver should go. How do we process a customer well if this is kind of the complement of it if complement of driver processing now this time we are going to check if there are any waiting drivers at all if there are no waiting drivers then the customer cannot be served. So we have to send the message tell the customer look try later well of course this program this will be read the output of the program will be read by the controller who is typing the C's and D's and the details of the drivers so the controller will read this message try later and presumably the controller will tell the customer otherwise what happens well otherwise we are going to print a message saying look here is the driver for you and which means which driver ID will we take well according to our invariance the driver ID in the index front is the earliest waiting driver so that driver ID is going to be sent or assigned to that customer and we have to say that this position is no longer containing useful data because we assign that the driver so we have to increment front but remember now this is a circular queue so we increment it but if that increment has gone to n we want it to become 0 and so therefore we take mod n and we decrement n-waiting that is it we are not going to shift up the waiting the waiting drivers data in our queue instead we are going to shift down what is the index of the first and assigned driver that is it alright so this is another solution so a different idea and it is a better idea because it copies it doesn't do really any copying but we have had to have an extra extra variable called front so we had to manipulate one extra variable but we saved a lot of copying so now as an exercise I would like you to go through the programs and check that the invariance are indeed true after each customer or driver arrival and of course the code is there in the book and I would like you to run the code and test for yourself whether the code works correctly so that concludes this segment in the next segment I am going to talk about something called the disk intersection problem but before that we will take a short break