State-Based Models: MDPs & Games


State-Based Models: MDPs & Games

Corey
So we are finally moving from reflex-based models into state-based models. That means we are going to think about problems where the output isn’t just a single label, but a whole sequence of actions.

Anna
Right, and Dorsa started with that classic farmer, cabbage, goat, and wolf puzzle. That’s a perfect example: you want to get everything across the river, but you have constraints—like you can’t leave the goat with the cabbage or the wolf with the goat. It’s simple, but it forces you to think ahead.

Corey
Exactly. And the key takeaway from that puzzle wasn’t just the answer—seven crossings—but how you went about finding it. That process is what search problems are all about. You’re not just reacting; you’re considering the future consequences of your actions.

Anna
And that’s the big difference from what we did earlier. With a neural network, you give an input and get a label. Here, you take a state and need a whole plan. Like, if the farmer takes the goat first, then he has to come back, maybe take the wolf, but then the goat is alone with the cabbage… you have to explore possibilities.

Corey
So let’s formalize that. A search problem has a start state, a set of actions from each state, a cost for taking an action, a successor function that tells you where you end up, and an end condition. That’s the model part—separate from the algorithm that solves it.

Anna
We saw a concrete example: the tram problem. You start at block 1, want to get to block N. You can walk to the next block (cost 1) or take a tram that doubles your block number (cost 2). That’s a small state space, but it lets us compare algorithms.

Corey
The simplest algorithm is backtracking search. It just explores every possible sequence of actions until it finds the cheapest path. But that’s exponential in the depth of the tree—order b^d time, where b is the branching factor and d is depth. The space is only d because you only keep one branch in memory.

Anna
Then we have depth‑first search, which is like backtracking but stops as soon as it finds a solution. That only works if all costs are zero—like solving a Rubik’s cube where any solution is good. In theory it’s still worst‑case exponential, but in practice it can help.

Corey
Breadth‑first search is better when all costs are the same constant. You explore layer by layer. If you find a solution at depth d, it’s optimal because any deeper solution would cost more. Time is b^d, but space becomes b^d too—you have to remember all nodes at the current layer.

Anna
Then there’s iterative deepening DFS, which combines the space advantage of DFS with the layer‑by‑layer approach of BFS. You run DFS up to depth 1, then depth 2, and so on. If you find a solution at a shallow depth, you’ve saved a lot of time, and space is only d. But if the solution is deep, you’re doing redundant work.

Corey
All those tree search algorithms are still exponential in time. To do better, we need dynamic programming. The key insight is that the future cost from a state only depends on the state itself, not on how you got there. So you can memoize—store the optimal cost for each state the first time you compute it.

Anna
That turns the exponential tree into a graph, and the runtime becomes polynomial in the number of states. But it only works if the graph is acyclic—no cycles—because you need to compute costs in order. For the tram problem, it’s perfect: from block s you only go to s+1 or 2s, so you can compute backwards.

Corey
And the state representation matters a lot. Take the route‑finding example with the constraint that you can’t visit three odd cities in a row. If you define state as (previous city, current city), that’s n² states. But you only need to know whether the previous city was odd or even—so state becomes (odd flag, current city), which is only 2n states.

Anna
You can go even further. For the constraint “visit at least three odd cities,” you don’t need to count beyond three. So state can be (min(count, 3), current city), which gives 3n states instead of n²/2. Choosing the minimal sufficient state is how you keep dynamic programming efficient.

Corey
But what if the graph has cycles? Dynamic programming can’t handle that because there’s no natural order. That’s where uniform cost search comes in. It works like Dijkstra’s algorithm: it enumerates states in order of increasing past cost, assuming non‑negative edge costs.

Anna
You keep three sets: explored (where you know the optimal path), frontier (states you’ve seen but aren’t sure about), and unexplored. You always pop the state with the smallest past cost off the frontier, put it into explored, and update its neighbors. That way you guarantee that when you move a state to explored, you’ve found the cheapest path to it.

Corey
The example we walked through—from A to D via B and C—shows exactly that. You start with A on the frontier with cost zero. Then you pop A, explore its neighbors B and C, and so on. Eventually you pop D with cost 3: A→B→C→D. And because you always expand in order of cost, that’s guaranteed optimal.

Anna
So let’s recap: we’ve moved from reflex‑based models to search problems, where the output is a sequence of actions. The naive tree search methods are exponential, but with dynamic programming—using memoization on a well‑chosen state space—we can get polynomial time on acyclic graphs. And uniform cost search handles cycles with non‑negative costs.

Corey
And next week we’ll extend these ideas to MDPs and games, where uncertainty and multiple agents come into play. But for now, we’ve got the foundations of modeling and inference for search problems.

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