State space and transitions
A state space is the set of possible situations represented by a problem model. Search explores possible actions to find a sequence leading from an initial state to a goal. In this chapter, a virtual courier starts in room A and needs to reach room C. A corridor is a permitted move, not a command to a real robot. We will describe the problem before choosing an algorithm.
Five pieces of a search problem
- States: A, B, C and D are the four room labels.
- Initial state: A.
- Actions and transition model: choose an outgoing corridor; the resulting state is its destination.
- Goal test: return true when the current room is C.
- Cost: each corridor has an integer energy cost; a route costs the sum of its corridors.
The map is static, fully known and deterministic. Room D is isolated. A and B connect in both directions with cost 2; A connects to C with cost 5; B connects to C with cost 1. C has no outgoing corridors. The set of model states includes D even though it is not reachable from A.
Represent permitted transitions in Python
A directed graph contains nodes (rooms) and directed edges (corridors). A dictionary of dictionaries represents outgoing edges: the outer key is the current room; the inner keys are destinations and their values are costs. An empty inner dictionary means no outgoing moves. All referenced rooms are declared, and costs are nonnegative integers. There are no parallel edges in this simplified model.
def successors(graph, state): return list(graph[state])
def is_goal(state, goal): return state == goal
graph = {"A": {"B": 2, "C": 5}, "B": {"A": 2, "C": 1}, "C": {}, "D": {}}print(successors(graph, "A"))print(successors(graph, "C"))print(is_goal("C", "C"))The output is ['B', 'C'], [], then True on separate lines. list(graph[state]) lists the outgoing destinations in insertion order. The goal test does not depend on whether a room has outgoing corridors. An unknown room is outside the input contract; it would raise a KeyError here.
Trace a proposed sequence of actions
To check a sequence, start at the initial room and apply each move in order. Return None when a move is not permitted. The following function accepts a known start and a list of proposed destinations, not a full path containing the start. An empty sequence makes no move.
def follow(graph, start, moves): state = start for destination in moves: if destination not in graph[state]: return None state = destination return state
graph = {"A": {"B": 2, "C": 5}, "B": {"A": 2, "C": 1}, "C": {}, "D": {}}print(follow(graph, "A", ["B", "C"]))print(follow(graph, "C", ["A"]))print(follow(graph, "D", []))The first route ends at C, the second returns None because the reverse corridor does not exist, and the third remains at D. Ending in a known room is not sufficient for success: the final state must pass the goal test. A legal sequence may fail to reach the goal.
Choose enough state information
If a door requires a key, the room alone may be insufficient: two couriers in B may have different permitted actions. Represent the state as (room, has_key) instead. Keep fixed map data in the model, and keep changing facts in the state. A model that omits relevant facts may report an impossible plan; a model that stores irrelevant history may inflate the search space. Do not assume a corridor is reversible merely because the rooms are connected.
Practice before search
Sketch the four rooms and directed corridors. Write the initial state and goal test for a new task starting in D and targeting C. Explain why no legal first action exists, while the zero-move task D to D succeeds. Next, distinguish a state graph from a tree of candidate paths.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
State space and transitions — understanding check
Apply search foundations to the fictional directed room graph.
The courier must reach C using the declared four-room map. Which room is its specified initial state?
Why does the state space include isolated room D?
Which move is illegal on the declared map?
If a reusable key changes which doors may be crossed, which state representation preserves that information?
What does an empty action list do from D?