State-Based Models: MDPs & Games
Transcript
Hi everyone, I'm Dorsa, uh, and this week I'll be teaching the state-based models and the plan is for the next couple of weeks for me to, to teach the state-based models MDPs, uh, and games and then and after that Percy, we'll come back and talk about the later, some of the later topics. So a few announcements. Uh, so homework 3 is out. So just make sure to look at that. And then the grades for homework 1 will be coming out soon. So just yeah, be aware of that. All right. So, so let's talk about state-based models, let's talk about search. So just to start, I was thinking maybe we can start with this question. Uh, if you can, let me reset this. So basically, okay, let me tell you what the question is and then think about it, and then after that I will get this working. So, so the question is you have a farmer and the farmer has a cabbage, a goat, and a wolf, and it's on one side of the river. Everything is on one side of the river.
So you have this river. We have a farmer. We have the farmer with a cabbage, with a goat, and with a wolf, okay. And the farmer wants to go to the other side of the river and take everything with, with, with himself, um, and- but the thing is the farmer has a boat and in that boat can only fit two things. So the farmer can be in it with, with one of these other things, okay? So the question is how many crossings can, can the farmer do to take everything on the other side of the river? And there are a bunch of constraints, the constraint is if you leave the cabbage and goat together the goat is going to eat the cabbage. So you can't really do that. If you leave wolf with the goat, the wolf is going to eat the goat, you can't really do that. How many crossings should you take to take everything to the other side? Think about it, talk to your neighbors, I'll get this working. Everyone clear on the question? Okay.
So the link doesn't work because, uh, we can't connect to Internet, but all right so. Okay. So how many people think it is four? Four crossings. Five, five crossings. Six six. Some people think six. Seven? More people. No solution? No solution. Okay. So the point is actually not like what the answer is, we'll come back to this question and try to solve it, but I think the important points to, to think about right now is how you went about solving it.
So, so what were you thinking and what was the process that you were thinking when you were trying to solve, solve this problem. And that is kind of the commonality that search problems have and, and we want to think about those types of problems where it's, it's more challenging to answer these types of questions and let's say reflex based type of questions. So, so that's kind of just a motivating example that we'll come back later. And here's an XKCD on this. So basically one potential solution is the farmer takes the goat, goes to the other side, comes back, takes the cabbage, goes to the other side and just leaves the wolf because why would he need a wolf, why would a farmer need a wolf. So [LAUGHTER] if you answered four, you probably were thinking about this. [LAUGHTER] And I guess it has like an interesting point in it because sometimes maybe you should change the problem. Your model is completely wrong.
Maybe, maybe sometimes you should rethink and go back to your model and try to fix that. But anyways. So we'll come back to this question. So all right. So this was our guideline for the class, and, and we have already talked about the reflex-based model. So we have talked about machine learning and how that can get applied, and now we want to start talking about state-based models. This week, we're going to talk about search problems, next week, MDPs, and then the week after we're going to talk about games. If you remember the kind of the guideline that, that we had for the class was, uh, we were thinking about these three different paradigms of, of modeling, all right, we talked about this already. So modeling, inference, and learning. So for, for reflex-based models we talked about this already, right? So what would the model be, well, it can be a linear predictor or it can be a neural network. So, so that was a model.
And then we talked about inference but in the case of reflex-based models it was really simple, it was just function evaluation. You had, you had your neural network and you would just go about evaluating it and that was inference. And we also spent some time talking about learning. So how would we use like let's say gradient descent to try to fit the parameters of the model, okay. So similar thing with search-based models. You want to talk about these three different paradigms that we have in the class, and, and the plan is to talk about models and inference today and then on Wednesday we'll talk about learning. We kind of have the same sort of format next week too. So we're going to start talking about modeling and inference on Mondays, Wednesdays are going to be about learning. So, so just to give you an idea of what the plan is. All right. So, so what are search problems?
Let's start with a few motivating examples. So, so one potential example one can think of is, is route finding. So you might have a map and you want to go from point A to point B on the map, and you have an objective. So you want to maybe find the shortest path or the fastest path or most scenic path. That is your objective and the things you can do is you can take a bunch of actions. So you can do things like go straight, turn left, turn right, and then the answer for the search problem is going to be a sequence of actions. If, if you want to go from A to B with the shortest path, the answer that one would give is maybe turn right first and then turn left and then right again or any, any of these sequences. Okay so, so this is just a canonical example of what a search problem is. There are a few other examples. So for example you can think of robot, robot motion planning. So if you have a robot that wants to go from point A to point B,
then it might want to have different objectives for doing that. So again the question might be what is the fastest way of doing it or what is the most energy efficient way of getting the robot to do that or, or what is the safest way of doing it. Like another question that we are interested in is what is the most expressive or, or legible way of robot doing it so, so people can understand what the robot really wants. So you might have again various types of objectives you can formalize that, and then the actions that, that you can take in the case of the robot motion planning is the robot is going to have different joints, and each one of the joints can translate and can rotate. So translation and rotation are the type of actions that you can take. So, so in this case I have a robot with seven, seven joints and then I need to tell what each one of those joints should do in terms of translation and rotation. That's your robot? This is my robot, yes. [LAUGHTER] It's a fetch robot.
[LAUGHTER] All right. So, so let's look at another example. So games is, is a fun example. So you might, uh, think about something like Rubik's cube or, or this 15-puzzle, and again what do you wanna do as a search problem? Well, you wanna, you wanna end up in configuration that's desirable, right? So you wanna end up in a configuration where, where you have this type of ah, configuration on Rubik's cube or, or the 15 puzzle. So that, that is the goal, that's the objective. And then the action is you can move pieces around here. So, so the sequence of actions might be how you're moving these pieces around to get that particular configuration of the 15 puzzle, okay. So again another example of what a search problem is. Um, machine translation is, is an interesting one if it's not necessarily the most natural thing you might think about when you think about search problems, but what it is actually you can think about it as a search problem again.
So imagine you have a phrase in a different language and you want to translate it to English. So what is the objective here? Well you can think of the objective as going to fluent English and preserving meaning. So, so that is the objective that one would have in machine translation. Um, and, and then the type of actions that you're taking is you're appending words. So you start with the and then you're appending blue to it and you're appending house to it. So, so as you're appending the- these different, different words, those are the actions that you're taking. So, so in some sense you can have any complex sequential task and, and the sequence of actions that you would get to get to your objective is there's going to be the answer for, for your search problem and you can pose it as a search problem, okay? All right. So, so what is different between let's say reflex-based models and, and search problems?
So, so if you remember, reflex-based models the idea was you'd have an input x and then we wanted to find this f for example a classifier that, that would output something like, like this y which is labeled, it's a plus 1 or minus 1. So, so the common thing in, in these reflex-based models was we were outputting this, this one label, this one in this case action being minus 1 or plus 1. Again in search problems, the idea is I'm given an input, I'm given a state, and then given that I have that state, what I wanna output is a sequence of actions. So I do want to think about what happens if I take this action like how is that going to affect the future of my actions. Okay. So, so the key idea in search problems is you need to consider future consequences of, of the actions you take at the current state. Yes. Is this like not equivalent to like just outputting one thing and then like rerunning the function, on like the updated state? So if you rerun it.
So, so the question is, yeah, is it not the same as like I'm rerunning it, I output a thing and then I rerun it again. And you could do that, but that ends up being a little bit of a- that would be some- similar to a greedy algorithm where like let's say I want to get to the door and I want to find, find the fastest way and right now if I just look at like my current state maybe I think the fastest way of getting there is going this way. But if I actually think about a horizon and I think about how this action is going to affect my future I might come up with a different sequence of actions. Okay? All right. Okay. So and, and you've already seen this paradigm so let's start talking about modeling and inference during this class. So this is the, the plan for today. So we're going to talk about three different algorithms for, for doing inference for search problems. So, so we're going to talk about tree search which is
the most naive thing one could do to solve some of these search problems, but that's the simplest thing we can start with. And then after that you want to look at improvements of that doing dynamic programming or, or uniform cost search. So, um, the difference between search-based problem and reflex-based problem, the very fact that in a reflex-based problem, the output that you gave does not influence a string, and it doesn't search? Yeah. Tha- that's true. Yeah so, so the output that you get in search problem it is an action that actually influences your future. Yeah, that's a good way of actually thinking about it. Yes. All right. So, so let's talk about tree search. So let's go back to our favorite example. Um, okay so we have the farmer, cabbage, goat, and wolf. So let's think about all possible actions that one can take, when we have this farmer, cabbage, goat, and wolf. Okay. So, so a bunch of things we can do is a farmer
can go to the other side of the river with the boat alone. So, uh, this triangle here just means like going to the other side of the, uh, the river. The farmer can take the cabbage. So C is for cabbage G is for, ah, goat, W is for wolf. So another possible action is the farmer takes a cabbage or the farmer takes the goat or the farmer takes a wolf and goes to the other side of the river. We also have a bunch of other actions. The farmer can come back. The farmer can come back with the cabbage, come back with the goat, come back with the wolf. So I'm basically numering- enumerating all possible actions that, that one could ever do. And sure none of- like not- some of these might not be possible in particular states but I'm just creating this library of actions things that are possible. Okay. So then when we think about the, ah, this as a search problem, we could create a search tree. Which, which basically starts from an initial state of where things are and
then we can kind of think about where we could go from that initial state. So the search tree is more of, ah, what if- what if tree which, which allows you to think about what are the possible options that, that you can take. So, um, conceptually what- what it looks like is you're starting with your initial state, where everything is on one side of the river. So those two lines are the riv- the river the blue lines. Um, and you can take a bunch of actions, right like one possible action is you can take the cabbage and go to the other side of the river and you end up in that state. And that state is not a good state. I am making that red. Well, why is that. Because the wolf is going to eat the goat. That's not that great. Okay. Um, and, and every action, every crossing let's say ma- let's say every crossing takes cost of one. So that one that you see on the edge is the cost of that action. Okay. So that didn't really work that well.
What else can I do? Well, I can, I can do another action. I can, I can- from the initial state, I can take the goat and go to the other side of the river, that ends up in this configuration. From there the farmer could come back, take the cabbage, go to the other side, end up in this configuration, the farmer can come back. That's again, not a great state because cabbage and goat are left on the other side of the river, goat is going to eat the cabbage. That's not great. What else can I do? Well, the farmer can come back with the goat. And then once the farmer comes back with the goat, the farmer leaves the goat, takes the wolf, goes to the other side, comes back gets the goat again. And then boom, you're done. Okay. So- so how many steps does this take? Well one two three four, five, six, and seven. So- so the ones who answer seven that was the right answer. Um, and that is kind of the idea of getting to this end state. Yes. So to be specifically, ah,
not include the option that the going back to the previous state even though that's a valid next step just because we know that there's something- So you could have this giant tree where you go to different states but we can actually have like a counter that tells you if I have visited that state and if you have visited that state maybe you don't want to go there again because, because you have already explored all the possible actions from there. You're not done with this tree though, right? Like I've, I've found that this good state here, but maybe there's a better way of, like getting there. I don't know yet. I haven't explored everything. So, so what I can do is, I can actually explore all these other things that, that one could do. And I'm not gonna go over them. But there is another solution, and turns out that other solution also takes seven steps. So it's not necessarily a better solution, but,
but you've got it for all of that because there could be another solution later on that. That is, uh, better than the seven steps. Okay. All right. Yes. Are these slides up? They are, they should be. Okay. Slides are up. Okay. Um, all right. So, so this is how the search tree looks like. Yeah. I'm just asking [inaudible] Oh, that's a very good point. Thank you for- [LAUGHTER] thank you, so for SCPD students I'll try to repeat the questions. I always forget this.
Um, I'll try to repeat the question. The question was, ah, was the slides, uh, the slides aren't up, they're up, they should be up. So okay. All right. So, uh, going back to our search problem. Ah, so we can try to formalize this search problem. So, so let's actually think about it more formally. So what are the things that we need to keep track of. So, so we have a start state. So let's defined a start to be the start state. In addition to that we can, we can define this function called actions which returns all possible actions from states. So actions as a function of state. If I'm in a state, that basically tells me what are the actions I can take from there. I can, I can define this cost function. So this cost function, takes a state and action and tells me what is the cost of that and in this example, the cost of crossing the river was just one but you can imagine having different costs values. Ah, we can have a successor function that basically takes a state and action and,
and tells us where we end up at. So if I'm in state S and I take action A where would I end up at? And that's the successor function. And then we're going to define an IsEnd function, which basically checks if you're in an end state where we don't have any other possible actions that you can take. Yes. So these are the [inaudible] I got a call? You can, you can think of it as, yeah, as a way of like finite state machine type of, type of, uh, way of looking at it. Yeah. So like we- we use a similar type of formalism, uh, for MVPs and games too. So this is good idea to get like all these formalisms right. But start state, transitions, costs. Those sort of things. Okay. Yes. What's the [inaudible] like [inaudible]. Ah, say it again so. A cost [inaudible] like. Cost? Position and action, and action already concerns the state. So then- so- so the action, okay, so action depends on state. So you start from start state where you haven't taken any actions right,
and then from that start state then you can think about all possible like right up there. So you're under that start state, and there you can think about all possible actions you can take, and then those actions depend on current state but they don't depend on the future state, right. So based on like the current state, everything is on one side of the river. I can think about all possible actions I can take and where I know- where I end up at. And then, after that like the next action depends on that. Yeah, that's it. So it's a sequential thing. Okay. Yes. You have all the information on the actions and the cost that you could do beforehand, how is this conceptually different than like a min cost flow convex optimization? You can think of it. Okay. So- so how- how is it different from a kind of convex optimization type of role? So- so we have- we have an objective here and then you can think
of what that objective is and based on what that objective is, we can have different methods for solving it, right? So- so you can basically formulate this as an optimization problem where you saw- you look for the solution to a search problem as an optimization problem too that's perfectly, a perfect way of doing it. And, and we're going to talk about various types of methods for- for solving this problem today. Okay. All right. So- so let's look at another example. So, um, this is, um, transportation problem. Now I'll just move this. So um okay. So basically, what we wanna do is we have street blocks from 1 through N. So 1, 2, 3, 4, so on. So these are street blocks and N is here. And what we wanna do is we basically want to travel from, from 1 to, to some N number. And we have two possible actions. So at any state, let's say I'm in state S. At any state, I can either walk, and if I walk I end up in S plus 1. So if I'm in 3, I'm going to end up in 4.
And walking takes one minute. Or I can take this magic tram. And this magic tram, takes any state S to 2 times S. So if I'm in 3, then I am going to end up in 6 by taking the magic tram. And the magic tram always takes two minutes, doesn't matter from where to where. So, so if I'm in 2, I will end up in 4, if I'm in 5 I can end up in 10 by taking the tram. Okay. So, so I have two possible actions in any of these states. And what I want to do is, I want to go from 1 to N and then
I want to basically do that in the shortest, uh, time possible. Okay. So with the- with the least amount of costs. That's the problem, makes sense? Okay. All right. So, so this is kind of like, what the search problem is. So what we wanna do is first off, you want to just formalize it. Uh, and I'm gonna do that here. I'm not gonna do live solutions because I'm not Percy, and I did that once and it was a disaster. So [LAUGHTER] we are going to, uh, yeah I taped these in 2018. Uh but uh basically
we're going to go over it together. So, so let's just do that. Um, so we're going to define the search problem, this tram problem. So we're gonna define a class for transportation problems. So we're going to separate our search problems from our algorithms because remember modeling is separate from inference. So let's just have a constructor for this transportation problem. It takes N, because we have N blocks. Okay. So N is the number of blocks. Okay. All right.
So, so then you have- we still have a start state. We're starting from 1 so block 1. And then we need to define IsEnd state. So IsEnd state basically checks if you've reached N or not. Because, because we have to get to the Nth block. Okay. All right. So what else do we need? So we have a successor function. We also have a cost function. I'm gonna put both of them together, because, because that is just easier. So the successor and cost function, I'm saying let's just give it state S. And then given a state it's going to return this triple of action, new state, cost. So I give it a state, let's say initial state, and then it just returns all possible actions, within new states I can end up at and how much does that cost. Okay/. So what are my options? Well, if I'm state S, I can walk to s plus 1 that costs 1. If I'm in state S, I can take the tram, I can end up in 2S, and that costs 2. Okay. So that's how I'm creating my triples.
And, and I need to check if I don't pass the Nth block. Remember, like we have N blocks so we don't want to pass the Nth block. Okay. So, so that's just to make sure that we don't pass it. So we are still below the Nth block. And, and this is what my successor and cost function will return that, the triples. Okay. So let's just return that. Okay. So that is my transportation problem. Let's make sure it does the thing the way we want it. So let's say we have 10 blocks, and now I wanna print my transportation- my successor and costs function. Let's say I'm returning successor and cost for 3. What should I get? So from 3, I can have two actions, right. I can either walk or I can take the tram. If I walk, uh, it costs 1. If I take the tram, it costs 2. I'll end up in 4 or 6. Let's just try. I don't know 9. If I'm in state 9, I can only do one thing, I can walk, right? Because remember, the, the block is- number of blocks is 10 and I can't go beyond that.
So- all right. Um, okay. So that was, um, [NOISE] yeah, let's go back here. So that was just defining, uh, the search problem, [NOISE] okay? And, and I haven't told you guys like how to solve it, right? This is- we are just doing the modeling right now. So we just modeled this problem. We just coded it up. Modeling it means, what is this- what are, what are the actions, what is a successor function, what is a cost function, defining an is end function,
saying what, what the initial state is, okay? So, so now I think we are ready to think about the algorithms in terms of, like, going and solving these types of search problems, okay? So the simplest algorithm we want to talk about is, is backtracking search. So the idea of backtracking search is- maybe I can draw a tree here, is you're starting from an initial state and then you have a bunch of possible actions. And then you end up in some state and you have a bunch of other possible actions. [NOISE] Let's say you have two actions possible. And this can become- [NOISE] this exponentially blows up so I'm going to stop soon. [LAUGHTER] All right. So, so we create this tree and this tree has some branching factor. That's the number of actions you have at, at every, at every state. And then it also has some depth. [NOISE] So that is how many levels you go down. [NOISE] So let me just define that with D, okay? And now there are solutions down in these notes, right?
So, so we wanna figure out what those solutions are. And backtracking search just does the simplest thing possible. What it does is, it starts from this initial state and it's going to go all the way down here. And if it doesn't find a solution, it's gonna go back here and then try again and try again. And it's gonna go over all of the tree because there might be a better solution down here too. So it needs to actually go over all of the tree, okay? So I'm gonna have a table of algorithms because we're gonna talk about a few of them here. Algorithms, [NOISE] what sort of costs they allow, in terms of time, how bad they are, in terms of space, how bad they are. So if you've taken an algorithms course, like, some of these are probably familiar. So, er, all right. So we talked about backtracking search, [NOISE] backtracking search. That is basically this algorithm that goes through pretty much everything, and it allows any type of cost.
So I can have [NOISE] any cost, right? I can have pretty much any cost I want on these edges because I'm going over all of the tree. It doesn't matter what these costs are, okay? So, um, how-, how bad is this in terms of, in terms of time? So in terms of time, I'm going over the full tree. By going over the full tree, then, then this, this is going to have this exponential blowup where I'm looking at order of b to the d, where b is, again, my branching factor and d is the depth of the tree, okay? Cause in terms of time, this is not a good algorithm. Like, in terms of time, I have to go over everything in the tree. And that's the size of my tree, okay? And in terms of space, in terms of space, what I mean is, I need to figure out what was, what was the sequence of actions I needed to take to get to some solution. So let's say that my solution is down here. If my solution is down here, then for me,
in or- like, I need to store a bunch of things to know how I got here, and the things I need to store are the appearance of this node and that is depth of D. So in terms of space, this algorithm takes order of D, okay? Because, because that is, like, the things that I need to store in my memory to be able to recover, like, the solution when I get there. Yes. [NOISE]. Question. Because we need to look at everything, shouldn't this space be big or here D to the D as well? Because until you get to that, you need, you need to have the space to have everything, right? You can prove that, but [NOISE] no. So actually, we'll talk about breadth-first search later, which does require you have a larger space. So, so the reason you can forget it is the only history that I need to keep track of is this particular branch, right? I don't need to figure out, like, I don't need to keep track of, like, actually the history of all these other nodes.
I can, I can throw it- [NOISE] those out. But for something else like breadth-first search where we'll talk about in a few slides, you actually need to keep track of, like, the history of everything else. So, so let me get back to that in a few slides. But for this one, basically the idea is, um, yeah, like, I wanna know how I got there. To, to know how I got there, I just need to know the parents. Yes. [inaudible] like the minimum cost to reach a point or is it to find whether, like, you can or cannot reach a certain point in your search. So it depends on what your objective is. Like, it really depends on what the search problem is asking. So, so in the case of that farmer-goat example, uh, the search problem is asking, you wanna move everything to the other side of the river. So you have that criteria. And you wanna find the minimum cost one, so you also have that other cri- criteria. So it really depends on what the search problem is asking.
And some of these nodes might be solutions. Some of them might not be solutions. So, so it really depends, okay? All right. So, so let's just look at these on the slide. So the memory is order of D. It's actually small. It's nice. In terms of time, this is not a great algorithm, right? Because even if your branching factor is 2, if the depth of the tree is 50, then this is gonna blow up, like, immediately. So a lot of these tree search algorithms that we're gonna talk about, like, they have the same problem. So, so they pretty much have the same time complexity. We're going to just look at very minimal improvements of them. And then after that, we'll talk about, uh, dynamic programming and uniform cost search, which are polynomial algorithms that are much better than these, okay? All right. So let's actually- let's go back to the tram example and let's try to write up what backtracking search does. So- all right. So we defined our model.
Our model is the search problem, this particular transportation search problem. It could be anything else. Um, and now we're going to kind of have this main section wi- where we're going to put in, like, our algorithms in it. And we're gonna write them as general as possible so, so we can apply them to other types of search problems, okay? So let's define backtracking search. It takes a search problem. It can take the transportation problem, okay? All right. So- and then we're going to- basically in backtracking search, what we're doing is we're recursing on every state given that you have a history of, of getting there and the total cost that it took us to, to get there, okay? So, so at the state, having gotten some history and some accumulated costs so far, we are going to basically recurse on that state and look at the children of that state, okay? So, so we're going to explore the rest of the subtree from, from that particular state, okay?
All right. So how do we do that? [NOISE] Well, we gotta make sure that we're not in an end state. Or if you're in an end state, like, we can actually update the best solution so far, okay? So let's put that for to do. So, so, so the bunch of things that we need to do. We need to figure out if you're in an end state. If we are, well, we got to, we gotta update our best solution. If you're not in an end-state, then we're going to recurse on children, okay? All right. So we can do that later.
And then in general, this recurse function is, is going to, uh, we're going to call it on on the, on the start state. So let's actually do that too. So, so what backtracking search does is it calls this recurse function on the initial state that we have with history of none, right? Like, we don't have any history yet, and, and cost is 0 so far because we haven't really gone anywhere. So, so we start with a start state. We call recurse on it, okay? [NOISE] And how do we recurse on children? Well, we have defined this, this successor and cost function. So by calling that successor and cost function on state, then we can get action, new state, and cost. So, so we get this triple of action, new state, and cost, okay? And then we can basically recurse on the new state. Um, I'm not putting the histories right now in this code. So, so we need to keep track of the history too, but, but let's just not worry about the history. Oh, I guess I'm putting it in this one. [LAUGHTER].
In the later ones I will not put them. But, but basically the history is keeping track of, like, how you got there. And to- total cost is going to be [NOISE] what, what you've got so far plus the cost of this, this new state, action pair, okay? Okay. So we need to keep track of the best solution so far. So I'm just going to find a dictionary here just to make sure that we keep track of it and for Python scoping reasons. Okay. And then the place we're going to update our best solution so far is that to do that is left, right? So, so if you're in an end state, then we can actually update the best solution so far, okay? And what do we want in our best solution? Well, we wanna know what the cost is. So, so we can start with cost of infinity. And anything below infinity is better. [NOISE] And then we're going to start with a history of empty, but we're going to fill up that history too, okay?
So that's the initialization of best solution so far. Then, we're going to update that, right? If you're in an end-state, if the total cost that we have right now is smaller than the best solution so far, then we're going to update that best solution. And, and you're going to update its history with whatever its history is, okay? All right. And, and that's it, that's backtracking search, okay? So let's just make sure it does the thing. So maybe- so to do that,
[NOISE] we are going to- actually, no, we gotta return the best solution so far. Mm-hmm. All right. So now we have defined a transportation problem. Now, what I want to do is, I want to call backtracking search on the transportation problem, okay? So that all sounds good. I need to write a print function also to- to be able to print things. So I'm gonna just write a generic print function that we can call on any of these types of problems.
So let's- let's define a print solution function that just like, prints things the way we want them. So we get the solution, and we're gonna just unpack that cost and history and just print the cost and history nicely. Okay. All right. So I can- I can use this print solution for pretty much all the other algorithms, we'll talk about today too. Okay. And we're gonna talk about how we get there- to the history. So now I have my print function, I have my backtracking search algorithm,
I've defined my transportation problem. I can just call it on this transportation problem with 10 blocks. So as you guys can see here, so the total cost is 6. So what this means is for going from city 1 to city, city 10, then this is the best solution. I- I gotta walk walk, walk, walk, and then after that ta- take the tram. Because like I end up in 5, and then after that it's actually worth taking the tram and paying the cost 50. Um, let's try it out for 20. What do you think is the answer for 20? So [LAUGHTER] similar to before, walk, walk, walk until we get to 5, then we take the tram, then we take the tram again. The cost is 8. And then if, if it is 100, it's a little bit more interesting if you have 100. So you are walking and then you're taking the tram and you get to 24 and you what- you have that in one step to get to 25 which is the good state because then you can just multiply that by 2.
So you walk for that one step and take the tram again, okay. So what if I want to try out a much larger number of blocks? So is this gonna work? No, because, because remember, that time was order of b to the d. That wasn't that great. So let's try that. Well, we got maximum recursion then, we can fix that. So [LAUGHTER] let's try fixing that. [LAUGHTER] So you can, you can set your recursion limit to be whatever. So you can try that. Is this gonna work?
[LAUGHTER] Now, it's just gonna take a long time, right. So, so it's not going to give you an answer [LAUGHTER] And it's gonna just take a long time. So all right. [LAUGHTER] Actually, how do I view? Okay. Let's go back here. All right. So that was backtracking search, right? So all it was doing was just going over all of this tree and it was taking exponential time as you saw and we just tried it out on that transportation problem that we defined.
So we just defined a search problem, we used this really simple search algorithm to find solutions for that, and- and then that's what we have so far. So, so now what we want to do is, we want to- we want to come up with a few better improvements of this backtracking search. Again, don't get your hopes up, it's not that big of an improvement. But, but we can do some- something better. So, so the first improvement you want to make is by using this algorithm called depth-first search, as some of you might have heard of it. DFS or depth-first search, okay? So the restriction that DFS put in, is, is that your cost has to be 0. So your cost has to be, let me leave that. Um, let me actually draw a line between them. So you don't get. Okay, so, so we are talking about DFS now, and the restriction is the cost has to be 0. So, so what DFS does, is it basically does exactly the same thing as backtracking search, but once it finds a solution down here then it is done.
It basically doesn't like explore the rest of the tree. And the reason it can do that is the cost of all these edges is 0. So if the cost of all these edges are 0, then if I find a solution I found a solution. I don't need to like find this better solution. Because, because that, that is good enough like anything that I find also has a cost of 0, so I might as well just return the solution. Like, an example of that is if you have Rubik- Rubik's cube uh, like if you find a solution then you have found a solution, right? There are a million different ways of like getting to a solution, but like you just want one. And then if you find one, then you're happy, you're done. Okay. So as you can see, this is a very, very slight improvement to backtracking search. Um, what happens is in terms of, in terms of space it's still the same thing. So it's order of D. So in terms of space nothing has changed. It's pretty good, it's order of D. In terms of time,
in practice it is better, right? Because in practice if I find a solution, I can just be done, don't worry about the rest of the tree. But, but in, in general, if you want to talk about it in theory then the worst case scenario is just trying out all of the trees, so you write it as worst case scenario, it's order of b to the d. So, so nothing has really changed in terms of- in terms of exponential blow up. Yes. I've been thinking of how you draw that tree, it seems that you imply that the sub problems do not overlap, right? Because you're kind of [inaudible] but in fact the sub-problem could overlap. So you- somebody with a training problem, you can get to the same place through different history but the rest is the same. Yeah, so you can- so, so the question is yeah, do sub-problems overlap here or they don't. So you could actually have it in a setting where sub-problems do overlap, but you could actually add this,
this extra like constraint that says if I visited the state, then don't add it to the tree. So, so you have that option or you have the option of like going down to tree with some, like particular depths and not trying out everything. In the setting that we have here, yeah, like we're basically trying out all possible. Like, I'm talking about the most uh, like, general form where you're going over all the states and all possible actions that could come out of it, okay? All right. So that was DFS. Okay. So the idea of DFS again as you're doing backtracking search and then you're just stopping when you find a solution because- because cost is 0, okay? So in terms of s- space order of D, in terms of time, it's still order of b to the d, okay? All right. So that was DFS. We have another algorithm called breadth-first search BFS. And this is useful when cost is some constant but it doesn't need to be 0, it's just some, some, some positive constant.
So what that means is all these edges have the same cost and that cost is just C. So I have the same cost pretty much everywhere, okay? So the idea of breadth-first search, is we can- we can go layer by layer. Like, like we're not going to try out the depth. Instead what we can do is, we can go layer by layer, try out this layer and see if we find a solution here. Remember the tree doesn't need to go all the way down here. The tree could end here or like at any of these and any of these nodes.

Ask anything
We've read this whole study set, so questions about it get the best answers.