1. Home
  2. Explore
  3. Computer Science
  4. State-Based Models: MDPs & Games

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

State-Based Models: MDPs & Games

State-Based Models and Search

Introduction and Motivation

Search problems require reasoning about sequences of actions, where each action influences future possibilities. A classic motivating example is the farmer, cabbage, goat, and wolf puzzle: a farmer must transport all three items across a river using a boat that can carry only the farmer plus one item. Constraints: the goat eats the cabbage if left alone, and the wolf eats the goat. The goal is to find a sequence of crossings that gets everything to the other side. The answer (seven crossings) highlights the need to consider future consequences — a reflex-based approach (choosing an action based only on the current state) would fail.

This contrasts with reflex-based models (e.g., classifiers, neural networks) that output a single label or action without considering how that action changes future states. Search problems instead output a sequence of actions, and the quality of an action depends on its downstream effects.

What Is a Search Problem?

A search problem is defined by:

  • A start state — where the agent begins.

  • A set of actions available from each state (actions(s)).

  • A cost function cost(s, a) — the immediate cost of taking action a in state s.

  • A successor function successor(s, a) — the state reached after taking action a.

  • An end condition (IsEnd(s)) — whether the state is a goal state.

The solution to a search problem is a sequence of actions that leads from the start state to an end state, minimizing total cost.

Examples

  • Route finding: from point A to B, actions = turn left, go straight, etc.; cost = distance or time.

  • Robot motion planning: joints translate and rotate; objective = fastest or safest path.

  • Puzzles (Rubik’s Cube, 15-puzzle): actions = move pieces; goal = desired configuration.

  • Machine translation: start with empty string, actions = append words; objective = fluent English preserving meaning.

Tree Search Algorithms

Tree search explores the possibility tree rooted at the start state. The tree has branching factor bbb (number of actions per state) and depth ddd (length of action sequences). Many tree-search algorithms suffer from exponential time in bbb and ddd, but differ in space usage and constraints on costs.

Backtracking Search

  • Mechanism: Recursively explore every branch. When a goal is found, update the best solution if the cost is lower. Continue until all branches are exhausted.

  • Costs allowed: any values (positive, negative, zero).

  • Time: O(bd)O(b^d)O(bd) — must explore entire tree.

  • Space: O(d)O(d)O(d) — only need to store the current path (depth) to reconstruct the solution.

  • This is the simplest, most exhaustive search.

Depth-First Search (DFS)

  • Constraint: all action costs must be zero (c=0c = 0c=0 for all edges). Thus the first solution found is optimal.

  • Mechanism: Same as backtracking, but stop as soon as a goal is reached.

  • Time: worst-case O(bd)O(b^d)O(bd) (if the only goal is at the deepest branch), but often faster in practice.

  • Space: O(d)O(d)O(d).

  • Use case: problems where any solution is acceptable (e.g., solving Rubik’s cube).

Breadth-First Search (BFS)

  • Constraint: all action costs are the same positive constant (c>0c > 0c>0).

  • Mechanism: Explore level by level (all nodes at depth 0, then depth 1, etc.). The first goal found at depth ddd is optimal because all paths of depth ddd have the same cost c×dc \times dc×d.

  • Time: O(bd)O(b^{d})O(bd) where ddd is the depth of the shallowest goal (often much smaller than the tree’s full depth).

  • Space: O(bd)O(b^{d})O(bd) — must store all nodes in the current frontier to continue exploration.

  • BFS is useful when solutions are likely to be shallow.

DFS Iterative Deepening (IDDFS)

  • Combines benefits of BFS and DFS: performs DFS up to increasing depth limits, like a dog on a lengthening leash.

  • Constraint: Same as BFS – all costs equal constant.

  • Mechanism: For depth limit L=1,2,3,…L = 1, 2, 3, \dotsL=1,2,3,…, run a full DFS that stops at depth LLL (ignoring deeper nodes). If a goal is found, return it. Since costs are uniform, the first goal found is optimal.

  • Time: O(bd)O(b^{d})O(bd) (dominated by the final DFS at the goal depth ddd; earlier shallower searches add at most a factor of ddd, which is negligible compared to the exponential).

  • Space: O(d)O(d)O(d) (only path storage, like DFS).

  • Drawback: If the goal is extremely deep, time is still exponential.

Summary of tree search algorithms (all have exponential worst-case time in tree depth):

  • Backtracking: any cost, O(b^d) time, O(d) space.

  • DFS: zero cost, O(b^d) time (worst-case), O(d) space.

  • BFS: constant cost, O(b^d) time (shallower goals), O(b^d) space.

  • IDDFS: constant cost, O(b^d) time, O(d) space.

Dynamic Programming

Dynamic programming (DP) dramatically improves search by exploiting overlapping subproblems. The key insight: the optimal future cost from a state depends only on that state, not on how the state was reached. If the same state appears in multiple branches, we can compute its future cost once and reuse it (memoization).

Recurrence Relation

Let FutureCost(s) be the minimum total cost from state sss to any end state.

FutureCost(s)={0if IsEnd(s)min⁡a∈actions(s)[cost(s,a)+FutureCost(successor(s,a))]otherwise\text{FutureCost}(s) = \begin{cases} 0 & \text{if IsEnd}(s) \\ \min_{a \in \text{actions}(s)} \left[ \text{cost}(s,a) + \text{FutureCost}(\text{successor}(s,a)) \right] & \text{otherwise} \end{cases}FutureCost(s)={0mina∈actions(s)​[cost(s,a)+FutureCost(successor(s,a))]​if IsEnd(s)otherwise​

This recurrence defines an optimal substructure: the optimal path from sss is an action followed by the optimal path from the resulting state.

Assumption: Acyclic Graph

For DP to work, the state space must be acyclic (no cycles). Otherwise, FutureCost would depend on itself in an infinite loop. The recurrence imposes a natural topological order — we must compute FutureCost for states that come later (further from start) before earlier ones.

Complexity

  • Time: O(n)O(n)O(n), where nnn is the number of distinct states. This is polynomial, not exponential.

  • Space: O(n)O(n)O(n) for the memoization table.

DP is not magic; it works only when the state is chosen to be a sufficient summary of the past. The state must contain all information needed to make optimal future decisions.

Choosing the Right State

The size of the state space directly determines DP’s efficiency. We must design minimal sufficient states — remove redundant information.

Example: traveling from city 1 to city N, can only move forward, cost cijc_{ij}cij​.

  • Original problem: state = current city, size NNN. Works fine.

  • New constraint: cannot visit three odd cities in a row.

    • Naive state: (previous city, current city) → size N2N^2N2.

    • Better state: (previous city odd?, current city) → size 2N2N2N.

    • Even better: (counter of consecutive odd cities, current city) → counter only needs values 0,1,2 (since 3+ is forbidden), size 3N3N3N.

  • Another constraint: must visit at least three odd cities total.

    • State: (number of odd cities visited so far, current city). But number can be capped at 3 (since beyond that it doesn’t affect future decisions). Using min(odd_count, 3) reduces size from N×N/2N \times N/2N×N/2 to 3N3N3N.

Always choose the smallest state representation that still allows optimal planning.

Uniform Cost Search (UCS)

UCS (essentially Dijkstra’s algorithm) works on graphs that may contain cycles, as long as all costs are non-negative.

Idea

Instead of assuming a topological order, UCS enumerates states in order of increasing past cost (cost from the start). It maintains three sets:

  • Explored: states for which the optimal past cost is known.

  • Frontier: states that have been reached with some cost, but not yet confirmed optimal.

  • Unexplored: states not yet seen.

Algorithm Steps

  1. Start with the start state in the frontier with cost 0.

  2. While the goal is not in explored:

    • Pop the state with the lowest past cost from the frontier.

    • If that state is the goal, we are done.

    • Add it to explored.

    • For each neighbor s′s's′ reachable via an action:

      • If s′s's′ is not in explored or frontier, add it to frontier with cost = (past cost of current state) + edge cost.

      • If s′s's′ is already in frontier with a higher cost, update its cost to the lower value (relaxation).

  3. The found path to the goal is optimal.

Example

Consider graph with nodes A (start), B, C, D (goal). Edges: A→B (1), A→C (100), B→C (1), B→D (100), C→D (1).

  • Initial: frontier = {A:0}, explored = ∅.

  • Pop A (cost 0): move to explored; add B:1, C:100 to frontier.

  • Pop B (cost 1, lowest in frontier): B explored; update frontier: C cost becomes 2 (via B), D added with cost 101.

  • Pop C (cost 2): C explored; update D cost to 3 (via C).

  • Pop D (cost 3): goal reached; optimal path is A→B→C→D, total cost 3.

Properties

  • Time: O(nlog⁡n)O(n \log n)O(nlogn) with a priority queue (e.g., binary heap), where nnn is number of states.

  • Space: O(n)O(n)O(n) for storing frontier and explored sets.

  • Requires costs to be non-negative (otherwise, negative cycles could lead to infinitely decreasing costs).

UCS is the standard algorithm for shortest path in graphs with non-negative edge weights and is optimal for such problems.


The transition from exponential tree search to polynomial dynamic programming or uniform cost search depends on the structure of the problem and the choice of state representation. Understanding these trade-offs is essential for designing efficient solutions to sequential decision-making tasks.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent