Sub topic 14
How big is the state space?
This chapter assumes you can write a breadth-first search over an implicit graph, as in the earlier chapters. Here you will learn to estimate how many states a model has before you run it. That estimate tells you whether exhaustive search is feasible.
Why size matters
Breadth-first search visits each reachable state once and stores it in the parent dictionary. Time grows with the number of states plus edges. Memory grows with the number of states. If a model has 10 billion states, no amount of clean code will make it finish. Count first, then decide.
Count by multiplying
A state is a combination of variable values. If the variables are independent, the number of states is the product of the number of values each one can take. This is an upper bound, because some combinations may be unreachable.
For the two-jug puzzle, a ranges over 0 to 3 and b over 0 to 5:
ahas 4 values.bhas 6 values.- The bound is 4 × 6 = 24 states.
That is tiny. A grid with R rows and C columns has at most R * C positions, so a 1000 × 1000 grid has one million cells. That is also fine for BFS.
Where growth gets out of hand
Extra state variables multiply the count. Suppose you add a "keys held" set to a grid puzzle with 10 distinct keys. Each key is held or not, which gives 2^10 = 1024 combinations. A 100 × 100 grid then has up to 10,000 × 1024 = 10,240,000 states.
Several moving pieces cause the same effect. Three independent tokens on a 50 × 50 board give 2,500^3, about 15.6 billion, states.
def state_bound(*value_counts):
total = 1
for n in value_counts:
total *= n
return total
print(state_bound(4, 6)) # two jugs: 24
print(state_bound(100 * 100, 2 ** 10)) # grid with 10 keys: 10240000
print(state_bound(2500, 2500, 2500)) # three tokens: 15625000000Edges and branching
Edges also cost time. If each state has at most b moves, the edge count is at most b times the state count. A grid with 8-direction movement does about twice the neighbour checks of 4-direction movement. This changes the constant, not the growth, so the number of states remains the main concern.
Turning the estimate into a decision
Use the bound to pick a plan:
| Estimated states | Typical action |
|---|---|
| Up to a few million | Plain BFS or Dijkstra in Python is fine. |
| Tens of millions | Possible, but watch memory. Store compact states, such as ints or tuples of ints. |
| Hundreds of millions or more | Shrink the state, use A* with a good heuristic, or use a different method. |
Ways to shrink a state space:
- Remove variables that do not affect future moves.
- Use symmetry, for example treat two identical tokens as an unordered pair.
- Prune states that can never lead to the goal.
- Search from both start and goal.
The bound counts all combinations, while BFS only touches reachable ones. A real run may use far fewer states. Do not rely on that without testing on a smaller instance.
Exercise
A puzzle has a 20 × 20 grid, one player, and 8 doors that are each open or closed. What is the upper bound on the number of states? Is plain BFS practical?
Show answer
The player has 400 positions and the doors have 2^8 = 256 combinations, so the bound is 400 × 256 = 102,400 states. Plain BFS is easily practical.
Exercise
You change the puzzle to have two distinguishable players on the same 20 × 20 grid, plus the 8 doors. Compute the new bound, and say what you would do if the two players were identical and interchangeable.
Show answer
The bound is 400 × 400 × 256 = 40,960,000 states, which is borderline for Python. If the players are identical, store them as a sorted pair or a frozenset. This roughly halves the count, to about 20.5 million, which makes BFS more comfortable.