Hey there. Welcome. Welcome. Welcome. My computerfs not quite talking to anything yet here. You guys got an answer for that? Unplug and replug that? Okay, I donft know whatfs -- whether somebody can help make my computer talk to the projector? Yeah. Itfs plugged in like ordinary. Yeah, I -- we do this every day. There we go. Thank you very much. All right, back to your regularly scheduled lecture. Today Ifm gonna keep talking a little bit more about another procedural recursion example and then go on to talk about, kind of, the approach for recursive backtracking, which is taking those procedural recursion examples and kind of twisting them around and doing new things with them. This corresponds with to the work thatfs in chapter six of the reader. And then from here wefre actually gonna do a little detour to pick up an explanation of recursive data in the form of linked lists, and thatfs gonna give us a chance to start talking about C++ pointers, which I know everyone has been eagerly awaiting a chance to get a little bit of mystery of the pointers revealed to them. And so the coverage for that is in chapter 2 in the early sections, 2-2 and 2-3. I [inaudible] covered in the text in sort of 9-5. Therefs actually a handout Ifll give out on Mondayfs lecture that helps to go along with what wefre doing there so therefs a little bit of supplemental material to kind of work through that. And so itfll be our first chance to see points [inaudible] we can really use them to do something, and so I kind of differed talking about them until now because we donft have a good use until we get there, and that wefll be exploring a recursive data type called the link list. Ifll be hanging out at the Turbine Cafe' after class, so if, actually, youfre available this afternoon and want to come and hang out, wefll be there for an hour or two, so if you want to walk over with us or come later when youfre free, that would be great. If not this Friday, hopefully some other one. How many people started the recursion problem set? Got right on it. How many people have already solved a problem or two? Oh, yeah. How many people solved all of them? Okay. Thatfs good though. And how is it going so far? Thumbs up? Lots of joy? Joy in Mudville. All right. So as I say, the handout, you know, therefs six problems, each of them is actually very, very short, and that may lull you into believing that, well, you can start at it right before because itfs not gonna be typing speed thatfs gonna keep you there all night, but it is actually some really dense, complex code to kind of get your head around and think about. And I think, actually, it helps a lot to have started at it, and thought about it, and let it gel for a little time before you have to kind of really work it all the way out. So I do encourage you to get a jump on that as soon as youfve got some time. Okay. This is the last problem I did at the end of Wednesdayfs lecture. Itfs a very, very important problem, so I donft actually want to move away from it until I feel that Ifm getting some signs from you guys that wefre getting some understanding of this because it turns out it forms the basis of a lot of other solutions end up just becoming permutations, you know, at the core of it. So itfs a pretty useful pattern to have [inaudible] list the permutations of a string. So youfve got the string, you know, A B C D, and itfs gonna print all the A C D A B C D A variations that you can get by rearranging all the letters. Itfs a deceptively short piece of code that does whatfs being done here -- is that it breaks it down in terms of there being the permutation thatfs assembled so far, and then the remaining characters that havenft yet been looked at, and so at each recursive call itfs gonna shuffle one character from the remainder onto the thing thatfs been chosen so far. So chose the next character in the permutation, and eventually, when the rest is empty, then all of the characters have been moved over, and have been laid down in permutation, then what I have is a full permutation of the length end. In the case where rest is not empty, Ifve still got some choices to make. The number of choices is exactly equal to the number of characters I have left in rest, and for each of the remaining characters at this point, maybe therefs just C and D left, then Ifm gonna try each of them as the next character in the permutation, and then permute what remains after having made that choice. And so this is [inaudible] operations here of attaching that [inaudible] into the existing so far, and then subtracting it out of the rest to set up the arguments for that recursive call. A little bit at the end we talked about this idea of a wrapper function where the interface to list permutations from a client point of view probably just wants to be, herefs a string to permute the fact that [inaudible] keep this housekeeping of what Ifve built so far is really any internal management issue, and isnft what I want to expose as part of the interface, so we in turn have just a really simple one line translation that the outer call just sets up and makes the call to the real recursive function setting up the right state for the housekeeping in this case, which is the permutation wefve assembled at this point. So Ifd like to ask you a few questions about this to see if wefre kind of seeing the same things. Can someone tell me what is the first permutation thatfs printed by this if I put in the string A B C D? Anyone want to help me? [Student:]A B C D. A B C D. So the exact string I gave it, right, is the one that it picks, right, the very first time, and thatfs because, if you look at the way the for loop is structured, the idea is that itfs gonna make a choice and itfs gonna move through the recursion. Well, the choice it always makes is to take the next character of rest and attach it to the permutation as its first time through the for loop. So the first time through the very first for loop itfs got A B C D to chose, it chooses A, and now it recurs on A in the permutation and B C D to go. Well, the next call does exactly the same thing, chooses the first of what remains, which is B, attaches it to the permutation and keeps going. So the kind of deep arm of the recursion making that first choice and working its way down to the base case, right, right remember the recursion always goes deep. It goes down to the end of the -- and bottoms on the recursion before it comes back and revisits any previous decision, will go A B C D, and then eventually come back and start trying other things, but that very first one, right, is all just the sequence there. The next one thatfs after it, right, is A B D C, which involved unmaking those last couple decisions. If I look at the tree that I have here that may help to sort of identify it. That permute of emptying A B C, the first choice it makes is go with A, and then the first choice it makes here is go with B, and so on, so that the first thing that we can see printed our here will be A B C D, we get to the base case. Once itfs tried this, itfll come back to this one, and on this one itfll say, well, try the other characters. Well, therefs no other characters. In fact, this one also finishes. Itfll get back to here as it unwinds the stack the way the stack gets back to where it was previously in these recursive calls. And now it says, okay, and now try the ones that have, on the second iteration of that for loop, D in the front, and so it ends up putting A B D together, leaving C, and then getting A B D C. And then, so on kind of over here, the other variations, it will print all of the ones that have A in the front. There are eight of them. I believe three, two -- no, six. Six of them that have A in the front, and wefll do all of those. And only after having done all of working out that whole part, which involves kind of the [inaudible] of this whole arm, will eventually unwind back to the initial permute call, and then say, okay, now try again. Go with B in the front and work your way down from reading the A C D behind it. So wefll see all the As then the bunch that lead with B, then the bunch that leads with C, and then finally the bunch that leads with D. It doesnft turn out -- matter, actually, what order it visits them in because, in fact, all we cared about was seeing all of them. But part of what Ifm trying to get with you is the sense of being able to look at that recursive code, and use your mind to kind of trace its activity and think about the progress that is made through those recursive calls. Just let me look at that code just for a second more and see if therefs any else that I want to highlight for you. I think thatfs about what itfs gonna do. So if you donft understand this cost, if therefs something about it that confuses you, now would be an excellent time for you to ask a question that I could help kind of get your understanding made more clear. So when you look at this code, you feel like you believe it works? You understand it? [Student:][Inaudible]. I have a question. Is there a simple change you can make to this code so that it does combinations [inaudible]? Does combinations -- you mean, like, will skip letters? [Student:]Right. Yes. It turns out wefre gonna make that change in not five minutes. In effect, what you would do -- and therefs a pretty simple change with this form. Ifm gonna show you a slightly different way of doing it, but one way of doing it would be to say, well, give me the letter, donft attach it to next right? So right now, the choices are pick one of the letters that remain and then attach it to the permutation. Another option you could do was pick a letter and just discard it, right? And so, in which case, I wouldnft add it into so far. I would still subtract it from here, and Ifd make another recursive call saying, now how about the permutations that donft involve A at all? And I would just drop it from the thing and I would recurs on just the B C D part. So a pretty simple change right here. Ifm gonna show you therefs a slightly different way to formulate the same thing in a little bit, but -- anybody want to own up to having something that confuses them? [Student:][Inaudible] ask how you, like, what you would set up to test it? So [inaudible] one of the, you know, the easiest thing to do here, and I have the code kind of actually sitting over here just in case, right, hoping you would ask because right now I just have the most barebones sort of testing. Itfs like, yeah, what if I just, you know, throw some strings at it and see what happens, right? And so the easiest strings to throw at it would be things like, well what happens if I give it the empty string, right? You know, so it takes some really simple cases to start because you want to see, well what happens when, you know, you give it an empty input. Is it gonna blow up? And itfs, like, the empty input, right, immediately realizes therefs no choices and it says, well, look, therefs nothing to print other than that empty string. What if I give it a single character string? Right? I get that string back. Ifm like, okay, gives me a little bit of inspiration that it made one recursive call, right, and then hit the base case on the subsequent one. Now I start doing better ones. A and B, and Ifm seeing A B B A. Okay, so I feel like, you know, I got this, so if I put a C in the front, and I put the B A behind, then hopefully what I believe is that itfs gonna try, you know, C in the front, and then itfs gonna permute A and B behind it, and then similarly on down. So some simple cases that I can see verifying that it does produce, kind of, the input string as itself, and then it does the back end rearrangement, leaving this in the front, and then it makes the next choice for what can go in the front, and then the back end rearrangement. And kind of seeing the pattern that matches my belief about how the code works, right, helps me to feel good. What happens if I put in something that has double letters in it? So I put A P P L E in there. And all the way back at the beginning, right, I can plot more of them, right, that grows quite quickly right as we add more and more letters, right? Seeing the apple, appel, and stuff like that. Therefs a point where it starts picking the P to go in the front, and the code as itfs written, right, doesnft actually make it a combination for this P and this P really being different. So it goes through a whole sequence of pull the second character to the front, and then permute the remaining four. And then it says, and now pull the third character to the front and permute the remaining four, which turns out to be exactly the same thing. So there should be this whole sequence in the middle, right, of the same Ps repeated twice because we havenft gone over a way to do anything about that, right? So -- but it is reassuring to know that it did somehow didnft get, you know, confused by the idea of there being two double letters. [Inaudible] if I do this -- if I just say A B A, right? Something a little bit smaller to look at, right? A in the front. A in the front goes permuted, B in the front, and then it ends up permuting these two ways that ends up being exactly the same, and then I get a duplicate of those in the front. So right now, it doesnft know that therefs anything to worry about in terms of duplicates? Whatfs a really easy way to get rid of duplicates? Donft think recursively, think -- use a set. Right? I could just stuff them in a set, right? And then if it comes across the same one again itfs like, yeah, whatever. So it would still do the extra work of finding those down, but would actually, like, I could print the set at the end having coalesced any of the duplicates. To actually change it in the code, itfs actually not that hard either. The idea here is that for all of the characters that remain, right, and sometimes what I want to choose from is of the remaining characters uniqued, right? Pick one to move to the front. So if therefs three more Ps that remain in rest, I donft need to try each of those Ps individually in the front, right? Theyfll produce the same result -- pulling one and leaving the other two behind is completely irrelevant. So an easy way to actually stop it from even generating them, is when looking at the character here is to make sure that it is -- that it doesnft duplicate anything else in the rest. And so probably the easiest way to do that is to either pick the first of the Ps or the last of the Ps, right, and to recur on and ignore the other ones. So, for example, if right here I did a find on the rest after I -- do you see any more of these? And if you do, then skip it and just let those go. Or, you know, do the first. Donft do the remaining ones. You can either look to the left of look to the right and see if you see any more, and if so, skip the, you know, early or later ones depending on what your strategy is. Any questions about permute here? [Student:] [Inaudible]. So the [inaudible] can be just -- look down here, right, it just looks like that, right? This is a very common thing. Youfll see this again, and again, and it tends to be that the outer call, right, it tends to have this sort of this, you know, solve this problem, and then it turns out during the recursive calls you need to maintain some state about where you are in the problem. You know, like, how many letters you moved, or what permutation youfve built so far, or where you are in the maze, or whatever it is youfre trying to solve. And so typically that wrapper call is just gonna be setting up the initial call in a way that has the initial form of that state present that the client to the function didnft need to know about. Itfs just our own housekeeping that wefre setting up. Seems almost kind of silly to write down the function that has just line that just turns it into, basically, itfs just exchanging -- setting up the other parameters. I am going to show you the other kind of master patter, and then wefre gonna go on to kind of use them to solve other problems. This is the one that was already alluded to, this idea of a combinations. Instead of actually producing all of the four character strings that involve rearrangements of A B C D, what if I were to [inaudible] in kind of the subgroups, or the way I could chose certain characters out of that initial input, and potentially exclude some, potentially include some? So if I have A B C, then the subsets of the subgroups of that would be the single characters A B and C. The empty string A B and C itself the full one, and then the combinations of two, A B, A C, and B C. Now, in this case wefre gonna say that order doesnft matter. Wefre not -- whereas permutations was all about order, Ifm gonna use -- Ifm gonna structure this one where I donft care. If itfs A B or B A Ifm gonna consider it the same subset. So Ifm just interested in inclusion. Is A in or out? Is B in or out? Is C in or out? And so the recursive strategy wefre gonna take is exactly what I have just kind of alluded to in my English description there, is that Ifve got an input, A B C. Each of those elements has either the opportunity of being in the subset or not. And I need to make that decision for everyone -- every single element, and then I need to kind of explore all the possible combinations of that, right? When A is in, what if B is out? When A is in, what if B is in? So the recursion that Ifm gonna use here is that at each step of the way, Ifm gonna separate one element from the input, and probably the easiest way to do that is just to kind of take the front most element off the input and sort of separate it into this character by itself and then the remainder. Similarly, the way I did with permute, and then given that element I have earmarked here, I can try putting it in the current subset or not. I need to try both, so Ifm gonna make two recursive calls here, one recursive call where Ifve added it in, one recursive call where I havenft added it in. In both cases, right, I will have removed it from the rest, so whatfs being chosen from to complete that subset always is a little bit smaller, a little bit easier. So this is very, very much like the problem I described as the chose problem. Trying to choose four people from a dorm to go to flicks was picking Bob and then -- [inaudible] to Bob and not Bob, and then saying, well, Bob could go, in which I need to pick three people to go with him, or Bob could not go and I need to pick four people to go without Bob. This is very much that same pattern. Two recursive calls. Identify one in or out. The base case becomes when therefs nothing left to check out. So I start with the input A B C -- A B C D letfs say. I look at that first element -- I just need to pick one. Might as well pick the front one, itfs the easy one to get to. I add it to the subset, remove it from the input, and make a recursive call. When that recursion completely terminates, I get back to this call, and I do the same thing again. Remaining input is B C D again but now the subset that Ifve been building doesnft include. So inclusion exclusion are the two things that Ifm trying. So the subset problem, right, very similar in structure to the way that I set up permutations, right, as Ifm gonna keep track of two strings as Ifm working my way down the remainder in the rest, right, things that I havenft yet explored so far is what characters Ifve chosen to place into the subset that Ifm building. If I get to the end where therefs nothing left in the rest, so therefs no more choices to make, then what I have in the subset is what I have in the subset, and I go ahead and print it. In the case that therefs still something to look at I make these two calls, one where Ifve appended it in, where I havenft, and then both cases, right, where I have subtracted it off of the rest by using the subster to truncate that front character off. So the way that permute was making calls, right, was in a loop, and so sometimes itfs a little bit misleading. You look at it and you think therefs only one recursive call, but in fact itfs in inside a loop, and so itfs making, potentially, end recursive calls where end is the length of the input. It gets a little bit shorter each time through but therefs always, you know, however many characters are in rest is how many recursive calls it makes. The subsets code actually makes exactly two recursive calls at any given stage, in or out, and then recurs on that and what is one remaining. It also needs a wrapper for the same exact reason that permutations did. Itfs just that we are trying to list the subsets of a particular string, in fact, therefs some housekeeping thatfs going along with it. Wefre just trying the subset so far is something we donft necessarily want to put into the public interface in the way that somebody would have to know what to pass to that to get the right thing. Anybody have a question about this? So given the way this code is written, what is the first subset that is printed if I give it the input A B C D? A B C D. Just like it was with permute, right? Everything went in. What is the second one printed after that? A B C, right? So Ifm gonna look at my little diagram with you to help kind of trace this process of the recursive call, right, is that the leftmost arm is the inclusion arm, the right arm is the exclusion arm. At every level of the tree, right, wefre looking at the next character of the rest and deciding whether to go in or out, the first call it makes is always in, so at the beginning it says, Ifm choosing about A. Is A in? Sure. And then it gets back to the level of recursion. It says, okay, look at the first thing of rest, thatfs B. Is B in? Sure. Goes down the right arm, right? Is C in? Sure. Is D in? Sure. It gets to here and now we have nothing left, so we have [inaudible] A B C D. With the rest being empty, we go ahead and print it [inaudible] therefs no more choices to make. Wefve looked at every letter and decided whether it was in or out. It will come back to here, and Ifll say, okay, Ifve finished all the things I can make with D in, how about we try it with D out. And so then D out gives us just the A B C. After itfs done everything it can do with A B C in, it comes back to here and says, okay, well now do this arm, and this will drop C off, and then try D in, D out. And so it will go from the biggest sets to the smaller sets, not quite monotonically though. The very last set printed will be the empty set, and that will be the one where it was excluded all the way down, which after it kind of tried all these combinations of in out, in out, in out, it eventually got to the out, out, out, out, out, out, out case which will give me the empty set on that arm. Again, if I reverse the calls, right, Ifd still see all the same subsets in the end. They just come out in a different order. But it is worthwhile to model the recursion in your own head to have this idea of understanding about how it goes deep, right? That it always makes that recursive call and has to work its way all the way to the base case and terminate that recursion before it will unfold and revisit the second call that was made, and then fully explore where it goes before it completes that whole sequence. Anybody want to ask me a question about these guys? These are just really, really important pieces of code, and so Ifm trying to make sure that I donft move past something that still feels a little bit mystical or confusing to you because everything I want to do for the rest of today builds on this. So now if therefs something about either of these pieces of code that would help you -- yeah. [Student:]What would be the simplest way to do that? So probably the simplest way, like, if you said, oh, I just really want it to be in alphabetical order, would be to put them in a set, right, and then have the set be the order for you. [Student:]So you said order doesnft matter? Oh, you did care about how they got ordered. [Student:]So, like, letfs say you didnft want B C D to equal C D B. So in that case, right, you would be more likely to kind of add a permutation step into it, right? So if B C D was not the same thing as A B C, right, it would be, like, well Ifm gonna chose -- so in this case, right, it always -- well the subsets will always be printed as kind of a subsequence. So -- and letfs say if the input was the alphabet, just easy way to describe it, all of the subsets Ifm choosing will always be in alphabetical order because Ifm always choosing A in or not, B in or not. If I really wanted B Z to be distinct from Z B, then really what I want to be doing at each step is picking the next character to go, and not always assuming the next one had to be the next one in sequence, so I would do more like a permute kind of loop thatfs like pick the next one that goes, remove it from what remains and recur, and that I need that separate step we talked about of -- and in addition to kind of picking, we also have to leave open the opportunity that we didnft pick anything and we just kind of left the subject as is, right, so we could [inaudible] or not. And so permute always assumes we have to have picked everything. The subset code would also allow for some of them just being entirely skipped. So we pick the next one, right, and then eventually stopped picking. [Student:][Inaudible] just in your wrapper function then [inaudible] put a for loop or something like that, right? When youfre through changing your string? Yeah, you can certainly do that, like in a permute from the outside too, right. So therefs often very, you know, multiple different ways you can get it the same thing whether you want to put it in the recursion or outside the recursion, have the set help you do it or not, that can get you the same result. So let me try to identify whatfs the same about these to try to kind of back away from it and kind of move just to see how these are more similar than they are different, right, even though the code ends up kind of being -- having some details in it that -- if you kind of look at it from far away these are both problems that are about choice. That the recursive calls that we see have kind of a branching structure and a depth factor that actually relates to the problem if you think about it in terms of decisions, that in making a permutation your decision is what character goes next, right? In the subset itfs like, well, whether this character goes in or not that the recursive tree that I was drawing that it shows all those calls, the depths of it represents the number of choices you make. Each recursive call makes a choice, right? A yes, no, or a next letter to go, and then recurs from there. So I make, you know, one of those calls, and then therefs N minus 1 beneath it that represent the further decisions I make that builds on that. And so, in the case of permutation, once Ifve picked the, you know, N minus 1 things to go, therefs only one thing left, right? And so in some sense the tree is N because I have to pick N things that go in the permutation and then Ifm done. In the subsets, itfs also N, and thatfs because for each of the characters Ifm deciding whether itfs in or out. So I looked at A and decided in or out. I looked at B and decided in or out. And I did that all the way down. That was the life of the input. The branching represents how many recursive calls were made at each level of the recursion. In the case of subsets, itfs always exactly two. In, out, all the way down, restarts at 1, branches to 2, goes to 4, goes to 8, 16, and so on. In the permute case, right, there are N calls at the beginning. I have N different letters to choose from, so itfs a very wide spread there, and that at that next level, itfs N minus 1. Still very wide. And N minus 2. And so the overall tree has kind of an N times N minus 1 times N minus 2 all the way down to the bottom, which the factorial function -- which grows very, very quickly. Even for small inputs, right, the number of permutations is enormous. The number of subsets is to the end in or out, right, all the way across. Also, a very, you know, resource intensive problem to solve, not nearly as bad as permutation, but both of them, even for small sizes of N, start to become pretty quickly intractable. This is not the fault of recursion, right, these problems are not hard to solve because wefre solving them in the wrong way. Itfs because there are N factorial permutations. There are [inaudible] different subsets, right? Anything thatfs going to print to the N things, or N factorial things is going to do N factorial work to do so. You canft avoid it. Therefs a big amount of work to be done in there. But what wefre trying to look at here is this idea that those trees, right, represent decisions. Therefs some decisions that are made, you know, a decision is made at each level of recursion, which then is kind of a little bit closer to having no more decisions to make. You have so many decisions to make, which is the depth of the recursion. Once youfve made all those decisions, you hit your base case and youfre done. The tree being very wide and very deep makes for expensive exploration. What wefre gonna look at is a way that we can take the same structure of the problem, one that fundamentally could be exhaustive, exhaustive meaning tried every possible combination, every possible rearrangement and option, and only explore some part of the tree. So only look around in some region, and as soon as we find something thatfs good enough. So in the case, for example, of a search problem, it might be that wefre searching this space that could potentially cause us to look at every option, but wefre also willing to say if we make some decisions that turn out good enough, that get us to our goal, or whatever it is wefre looking for, we wonft have to keep working any farther. So Ifm gonna show you how we can take an exhaustive recursion problem and turn it into whatfs called a recursive backtracker. So therefs a lot of text on this slide but let me just tell you in English what wefre trying to get at. That the idea behind permutations or subsets is that at every level therefs all these choices and wefre gonna try every single one of them. Now imagine we were gonna make a little bit less a guarantee about that. Letfs design the function to return some sort of state thatfs like I succeeded or I failed. Did I find what I was looking for? At each call, I still have the possibility of multiple calls of in out, or a choice from whatfs there. Ifm gonna go ahead and make the choice, make the recursive call, and then catch the result from that recursive call, and see whether it succeeded. Was that a good choice? Did that choice get me to where I wanted to be? If it did, then Ifm done. I wonft try anything else. So Ifll stop early, quite going around the for loop, quit making other recursive calls, and just immediately [inaudible] say Ifm done. If it didnft -- it came back with a failure, some sort of code that said it didnft get where I want to do, then Ifll try a different choice. And, again, Ifll be optimistic. Itfs a very optimistic way of doing stuff. It says make a choice, assume itfs a good one, and go with it. Only when you learn it didnft work out do you revisit that decision and back up and try again. So let me show you the code. I think itfs gonna make more sense, actually, if I do it in terms of looking at what the code looks like. This is the pseudo code at which all recursive backtrackers at their heart come down to this pattern. So what I -- I tried to be abstract [inaudible] so what does it mean to solve a problem, and whatfs the configuration? That depends on the domain and the problem wefre trying to solve. But the structure of them all looks the same in that sense that if there are choices to be made -- so the idea is that we cast the problem as a decision problem. There are a bunch of choices to be made. Each [inaudible] will make one choice and then attempt to recursively solve it. So therefs some available choices, in or out, or one of next, or where to place a tile on a board, or whether to take a left or right turn and go straight in a maze. Any of these things could be the choices here. We make a choice, we feel good about it, we commit to it, and we say, well, if we can solve from here -- so we kind of update our statement so wefve made that term, or, you know, chosen that letter, whatever it is wefre doing. If that recursive call returned true then we return true, so we donft do any unwinding. We donft try all the other choices. We stop that for loop early. We say that worked. That was good enough. If the solve came back with a negative result, that causes us to unmake that choice, and then we come back around here and we try another one. Again, wefre optimistic. Okay, left didnft work, go straight. If straight doesnft work, okay, go right. If right didnft work and we donft have any more choices, then we return false, and this false is the one that then causes some earlier decision to get unmade which allows us to revisit some earlier optimistic choice, undo it, and try again. The base case is hit when we run out of choices where wefve -- whatever configuration wefre at is, like, okay, wefre at a dead end, or wefre at the goal, or wefve run out of letters to try. Whatever it is, right, that tells us, okay, we didnft -- therefs nothing more to decide. Is this where we wanted to be? Did it solve the problem? And Ifm being kind of very deliberate today about what does it mean [inaudible] update the configuration, or what does it mean for it to be the goal state because for different problems it definitely means different things. But the code for them all looks the same kind of in its skeletal form. So let me take a piece of code and turn it into a recursive backtracker with you. So Ifve got recursive permute up here. So as it is, recursive permute tries every possible permutation, prints them all. What Ifm interested in is is this sequence a letters an anagram. Meaning, it is -- can be rearranged into something thatfs an English word. Okay. So what Ifm gonna do is Ifm gonna go in and edit it. The first thing Ifm gonna do is Ifm gonna change it to where itfs gonna return some information. That informationfs gonna be yes it works, no it didnft. Okay? Now Ifm gonna do this. Ifm gonna add a parameter to this because I -- in order to tell that itfs a word I have to have someplace to look it up. Ifm gonna use the lexicon that actually wefre using on this assignment. And so when I get to the bottom and I have no more choices, Ifve got some permutation Ifve assembled here in -- so far. And Ifm going to check and see if itfs in the dictionary. If the dictionary says that thatfs a word then Ifm gonna say this was good. That permutation was good. You donft need to look at any more permutations. Otherwise Ifll return false which will say, well, this thing isnft a word. Why donft you try again? Ifm gonna change the name of the function while Ifm at it to be a little more descriptive, and wefll call it is anagram. And then when I make the call here [inaudible] third argument. Ifm not just gonna make the call and let it go away. I really want to know did that work, so Ifm gonna say, well, if itfs an anagram then return true. So if given the choice I made -- Ifve got these letters X J Q P A B C, right? Itfll pick a letter and itfll say, well if, you know, put that letter in the front and then go for it. If you can make a word out of having made that choice, out of what remains, then youfre done. You donft need to try anything else. Otherwise wefll come back around and make some of the further calls in that loop to see if it worked out. At the bottom, I also need another failure case here, and that comes when the earlier choices, right -- so I got, letfs say somebody has given me X J, and then it says, given X J, can you permute A and B to make a word? Well, it turns out that you can -- you know this ahead of time. It doesnft have the same vision you do. But it says X J? A B? Therefs just nothing you can do but itfll try them all dutifully. Tried A in the front and then B behind, and then tried B in the front and A behind it, and after it says that, it says, you know what, okay, that just isnft working, right? It must be some earlier decision that was really at fault. This returned false is going to cause the, you know, sort of stacked up previous anagram calls to say, oh yeah, that choice of X for the first letter was really questionable. So imagine Ifve had, like, E X, you know, T I. Ifm trying to spell the word exit -- is a possibility of it. That once I have that X in the front it turns out nothing is going to work out, but itfs gonna go through and try X E I T, X E T I, and so on. Well, after all those things have been tried it says, you know what, X in the front wasnft it, right? Letfs try again with I in the front. Well after it does that it wonft get anywhere either. Eventually itfll try E in the front and then it wonft have to try anything else after that because that will eventually work out. So if I put this guy in like that, and I build myself a lexicon, and then I change this to anagram word. I canft spell. Ifd better pass my lexicon because Ifm gonna need that to do my word lookups. [Inaudible]. And down here. Whoops. Okay. I think that looks like itfs okay. Well, no -- finish this thing off here. And so if I type in, you know, a word that I know is a word to begin with, like boat, I happen to know the way the permutations work [inaudible] try that right away and find that. What if I get it toab, you know, which is a rearrangement of that, it eventually did find them. What if I give it something like this, which therefs just no way you can get that in there. So it seems to [inaudible] itfs not telling us where the word is. I can actually go and change it. Maybe thatfd be a nice thing to say. Why donft I print the word when it finds it? If lex dot contains -- words so far -- then print it. That way I can find out what word it thinks it made out of it. So if I type toab -- now look at that, bota. Who would know? That dictionaryfs full of some really ridiculous words. Now Ifll get back with exit. Letfs type some other words. Query. So itfs finding some words, and then if I give it some word that I know is just nonsense it wonft print anything [inaudible] false. And so in this case, what itfs doing is its come to a partial exploration, right, of the permutation tree based on this notion of being able to stop early on success. So in the case of this one, right, even six nonsense characters, it really did do the full permutation, in this case, the six factorial permutations, and discover that none of them worked. But in the case of exit or the boat that, you know, early in the process it may have kind of made a decision, okay so [inaudible] in this case it will try all the permutations with Q in the front, right? Which means, okay, wefll go with it, and then itfll do them in order to start with, but itfll start kind of rearranging and jumbling them up, and eventually, right, it will find something that did work with putting in the front, and it will never unmake that decision about Q. It will just sort of have -- made that decision early, committed to it, and worked out, and then it covers a whole space of the tree that never got explored of R in front, and Y in the front, and E in the front because it had a notion of what is a satisfactory outcome. So the base case that it finally got to in this case met the standard for being a word was all it wanted to find, so it just had to work through the base cases until it found one, and potentially that could be very few of them relative the huge space. All right. I have this code actually on this slide. So itfs permute, and that is turning into is anagram. And so, in blue, trying to highlight the things that changed in the process, right, that the structure of kind of how itfs exploring, and making the recursive calls is exactly the same. But what wefre using now is some return information from the function to tell us how the progress went, and then having our base case return some sense of did we get where we wanted to be, and then when we make the recursive call, if it did succeed, right, we immediately return true and unwind out of the recursion, doing no further exploration, and in the case where all of our choices gave us no success, right, we will return the call that says, well that was unworkable how we got to where we were. So this is the transformation that you want to feel like you could actually sort of apply again and again, taking something that was exhaustive, and looked at a whole space, and then had -- change it into a form where itfs like, okay, well I wanted to stop early when I get to something thatfs good enough. A lot of problems, right, that are recursive backtrackers just end up being procedural code that got turned into this based on a goal that you wanted to get to being one of the possibilities of the exploration. Anybody have any questions of what we got there? Okay. Ifm gonna show you some more just because they are -- there are a million problems in this space, and the more of them you see, I think, the more the patterns will start to emerge. Each of these, right, wefre gonna think of as decision problems, right, that we have some number of decisions to make, and wefre gonna try to make a decision in each recursive call knowing that that gives us fewer decisions that we have to make in the smaller form of the sub problem that wefve built that way, and then the decisions that we have open to us, the options there represent the different recursive calls we can make. Maybe itfs a for loop, or maybe a list of the explicit alternatives that we have that will be open to us in any particular call. This is a CS kind of classic problem. Itfs one that, you know, it doesnft seem like it has a lot of utility but itfs still interesting to think about, which is if you have an eight by eight chessboard, which is the standard chessboard size, and you had eight queen pieces, could you place those eight queens on the board in such a way that no queen is threatened by any other? The queen is the most powerful player on the board, right, can move any number of spaces horizontally, vertically, or diagonally on any straight line basically, and so it does seem like, you know, that therefs a lot of contention on the board to get them all in there in such a way that they canft go after each other. And so if we think of this as a decision problem, each callfs gonna make one decision and recur on the rest. The decisions we have to make are we need to place, you know, eight queens letfs say, if the board is an eight by eight board. So at each call we could place one queen, leaving us with M minus 1 to go. The choices for that queen might be that one way to kind of keep our problem -- just to manage the logistics of it is to say, well, we know that therefs going to be a queen in each column, right, it certainly canft be that therefs two in one column. So we can just do the problem column by column and say, well, the first thing wefll do is place a queen in the leftmost column. The next call will make a queen -- place a queen in the column to the right of that, and then so on. So wefll work our way across the board from left to right, and then the choices for that queen will be any of the [inaudible] and some of those actually are -- we may be able to easily eliminate as possibilities. So, for example, once this queen is down here in the bottommost row, and we move on to this next column, therefs no reason to even try placing the queen right next to it because we can see that that immediately threatens. So what wefll try is, is there a spot in this column that works given the previous decisions Ifve made, and if so, make that decision and move on. And only if we learned that that decision, right, that we just made optimistically isnft successful will we back up and try again. So let me do a little demo with you. Kind of shows this doing its job. Okay. So [inaudible] Ifm gonna do it as I said, kind of column by column. [Inaudible] is that Ifm placing the queen in the leftmost column to begin, and the question mark here says this is a spot under consideration. I look at the configuration Ifm in, and I say, is this a plausible place to put the queen? And therefs no reason not to, so I go ahead and let the queen sit there. Okay, so now Ifm going to make my second recursive call. I say Ifve placed one queen, now therefs three more queens to go. Why donft we go ahead and place the queens that remain to the right of this. And so the next recursive call comes in and says, if you can place the queens given the decision Ifve already made, then tell me yes, and then Ifll know all is good. So it looks at the bottommost row, and it says, oh, no, that wonft work, right? Therefs a queen right there. It looks at the next one and then sees the diagonal attack. Okay. Moves up to here and says, okay, thatfs good. Thatfll work, right? Looks at all of them and itfs okay. So now it says, okay, well Ifve made two decision. Therefs just two to go. Ifm feeling good about it. Go ahead and make the recursive call. The third call comes in, looks at that row, not good, looks at that row, not good, looks at that row, not good, looks at that row, not good. Now this one -- there werenft any options at all that were viable. We got here, and given the earlier decisions, and so the idea is that, given our optimism, right, we sort of made the calls and just sort of moved on. And now wefve got in the situation where we have tried all the possibilities in this third recursive call and there was no way to make progress. Itfs gonna hit the return false at the bottom of the backtracking that says, you know what, there was some earlier problem. There was no way I could have solved it given the two choices -- or, you know, whatever -- we donft even know what choices were made, but there were some previous choices made, and given the state that was passed to this call, therefs no way to solve it from here. And so this is gonna trigger the backtracking. So that backtracking is coming back to an earlier decision that you made and unmaking it. Itfs a return false coming out of that third call that then causes the second call to try again. And it goes up and it says okay, well where did I leave off? I tried the first couple of ones. Okay, letfs try moving it up a notch and see how that goes. Then, again, optimistic, makes the call and goes for it. Canft do this one. That looks good. And now wefre on our way to placing the last queen, feeling really comfortable and confidant, but discovering quickly, right, that there was no possible. So it turns out this configuration with these three queens, not solvable. Something must be wrong. Back up to the most immediate decision. She knows it doesnft unmake, you know, earlier decisions until it really has been proven that that canft work, so at this point it says, okay, well letfs try finding something else in this column. No go. That says, okay, well that one failed so it must be that I made the wrong decision in the second column. Well, it turns out the second column -- that was the last choice it had. So in fact it really was my first decision that got us off to the wrong foot. And now, having tried everything that there was possible, given the queen in the lower left in realizing none of them worked out, it comes back and says, okay, letfs try again, and at this point it actually will go fairly quickly. Making that initial first decision was the magic that got it solved. And then we have a complete solution to the queens. We put it onto eight, and let it go. You can see it kind of checking the board, backing up, and you notice that it made that lower left decision kind of in -- itfs sticking to it, and so the idea is that it always backs up to the most recent decision point when it fails, and only after kind of that one has kind of tried all its options will it actually back up and consider a previous decision as being unworthy and revisiting it. In this case that first decision did work out, the queen being in the lower left. It turns out there were -- you know, you saw the second one had to kind of slowly get inched up in the second row. Right? It wasnft gonna work with the third row. It tried that for a while. Tried the fourth row for a while. All the possibilities after that, but eventually it was that fifth row that then kind of gave it the breathing room to get those other queens out there. But it did not end up trying, for example, all the other positions for the queen in the first row, so it actually -- it really looked at a much more constrained part of the entire search tree than an exhaustive recursion of the whole thing would have done. The code for that --whoops -- looks something like this. And one of the things that Ifll strongly encourage you to do when youfre writing a recursive backtracking routine, something I learned from Stuart Regis, who long ago was my mentor, was the idea that when -- trying to make this code look as simple as possible, that one of the things you want to do is try to move away the details of the problem. For example, like, is safe -- given the current placement of queens, and the row youfre trying, and the column youfre at, trying to decide that therefs not some existing conflict on the board with the queen already being threatened by an existing queen just involves us kind of traipsing across the grid and looking at different things. But putting in its own helper function makes this code much easier to read, right? Similarly, placing the queen in the board, removing the queen from the board, there are things they need to do. Go up to state and draw some things on the graphical display. Put all that code somewhere else so you donft have to look at it, and then this algorithm can be very easy to read. Itfs like for all of the row. So given the column wefre trying to place a queen in, wefve got this grid of boolean that shows where the queens are so far, that for all of the rows across the board, if, right, itfs safe to place a queen in that row and this column, then place the queen and see if you can solve starting from the column to the right, given this new update to the board. If it worked out, great, nothing more we need to do. Otherwise we need to take back that queen, unmake that decision, and try again. Try a higher row. Try a higher row, right. Again, assume itfs gonna work out. If it does, great. If it doesnft, unmake it, try it again. If we tried all the rows that were open to us, and we never got to this case where this returned true, then we return false, which causes some previous one -- wefre backing up to a column behind it. So if we were currently trying to put a queen in column two, and we end up returning false, itfs gonna cause column one to unmake a decision and move the queen up a little bit higher. If all of the options for column one fail, itfll back up to column zero. The base case here at the end, is if we ever get to where the column is past the number of columns on the board, then that means we placed a queen all the way across the board and wefre in success land. So all this code kind of looks the same kind of standing back from it, right, itfs like, for each choice, if you can make that choice, make it. If you solved it from here, great, otherwise, unmake that choice. Herefs my return false when I ran out of options. Therefs my base case -- it says if I have gotten to where therefs no more decisions to make, Ifve placed all the queens, Ifve chosen all the letters, whatever, did I -- am I where I wanted to be? Therefs no some sort of true or false analysis that comes out there about being in the right state. How do you feel about that? You guys look tired today, and I didnft even give you an assignment due today, so this canft be my fault, right? I got a couple more examples, and Ifm probably actually just gonna go ahead and try to spend some time on them on Monday because I really donft want to -- I want to give you a little bit more practice though. So wefll see. Wefll see. Ifll do at least one or two more of them on Monday before we start talking about pointers and linked lists, and so I will see you then. But having a good weekend. Come and hang out in Turbine with me.