2025-Lecture02-P1-ProblemSolvingBySearch
2025-Lecture02-P1-ProblemSolvingBySearch
BY SEARCH
2
Problem-solving agents
Goal Problem
formulation formulation
Search Execution
5
Problem-solving process
• Goal formulation: Goals organize behavior by limiting the
objectives and hence the actions to be considered.
• E.g., in the touring holiday example, the goal is reaching Bucharest.
7
Search problems and solutions
• A search problem can be defined formally as follows.
• A set of possible states that the environment can be in,
called the state space
• E.g., the cities, {Arad, Zerind,..}, in the touring holiday example
• The initial state that the agent starts in
• E.g., in the above example, the agent begins at Arad.
• A set of one or more goal states
• E.g., in the above example, the agent wants to arrive at Bucharest.
• There can be one or several goal states, or the goal is defined by a
property that applies to many states (potentially an infinite number).
8
Search problems and solutions
• A finite set of actions that are executable in state 𝑠, 𝐴𝑐𝑡𝑖𝑜𝑛(𝑠)
• E.g., 𝐴𝑐𝑡𝑖𝑜𝑛(𝐴𝑟𝑎𝑑) = {𝑇𝑜𝑆𝑖𝑏𝑖𝑢, 𝑇𝑜𝑇𝑖𝑚𝑖𝑠𝑜𝑎𝑟𝑎, 𝑇𝑜𝑍𝑒𝑟𝑖𝑛𝑑}
9
Search problems and solutions
• An action cost function, 𝐴𝑐𝑡𝑖𝑜𝑛 - 𝐶𝑜𝑠𝑡(𝑠, 𝑎, 𝑠’) , gives the
numeric cost of applying action 𝑎 in state 𝑠 to reach state 𝑠’.
• The cost function should reflect the agent’s performance measure.
• E.g., for route-finding agents, the cost might be the length in miles or
the time to complete the action.
10
The state space
• The state space can be represented as a graph.
• The vertices are states and the directed edges between
them are actions.
11
Formulating problems
• The formulation of the problem is a model—an abstract
mathematical description—and not the real thing.
13
Formulating problems
• A good abstraction eliminates as much detail as possible.
• Meanwhile, it retains validity and ensures that the abstract
actions are easy to carry out.
14
Example problems
• Standardized problems
• Real-world problems
Example problems
Standardized problems
• Illustrate or exercise various problem-solving methods.
• Descriptions are concise and exact.
• Suitable as a benchmark for comparing the performance of algorithms.
Real-world problems
• Bring solutions to practical issues
• Formulations are idiosyncratic and not standardized.
16
The Vacuum-cleaner world
• States: determined by the agent location and the availability of dirt
• 2 × 22 = 8 possible world states (𝑛 × 2𝑛 for environment with 𝑛 cells)
• Initial state: Any state can be designated as the initial state.
• Actions: Left, Right, and Suck
• Larger models may include Upward and Downward.
• Transition model: The actions have their expected effects.
• Note that moving Left in the leftmost square, moving Right in the
rightmost square, and Sucking in a clean square have no effect.
• Goal states: The states in which every cell is clean.
• Action cost: Each step costs 1.
17
The Vacuum-cleaner world
19
The 8-puzzle problem
• Note the parity property that partitions the state space—any given goal
can be reached from exactly half of the possible initial states.
20
The 8-queens problem
• Incremental formulation: add a queen
step-by-step to the empty initial state
• Complete-state formulation: start with
all 8 queens on the board and move
them around
• The action/path cost is trivial because
only the final state counts.
21
The 8-queens: Incremental formulation
• States: Any arrangement of 0 to 8 queens on the board.
• Initial state: No queens on the board
• Actions: Add a queen to any empty square.
• Transition model: Return the board with a queen newly added to
the specified square
• Goal states: 8 queens are on the board, none attacked.
• 64 ∙ 63 ⋯ 57 ≈ 1.8 × 1014 possible sequences to investigate
22
The 8-queens: Incremental formulation
• A better formulation would prohibit placing a queen in any square
that is already attacked.
• States: All possible arrangements of 𝑛 queens (0 ≤ 𝑛 ≤ 8), one
per column in the leftmost 𝑛 columns, no queen attacking another.
• Actions: Add a queen to any square in the leftmost empty column
such that it is not attacked by other queens
23
Donald Knuth’s 4 problem (1964)
• Starting with the number 4, a sequence of
factorial, square root, and floor operations
will reach any desired positive integer.
620,448,401,733,239,439,360,000 steps
26
Robot navigation
• Robot navigation generalizes the route-finding problem.
• The search space for a circular robot moving on a flat surface is
essentially two-dimensional.
• The robot has arms and legs that must also be controlled → the
space becomes many-dimensional, one dimension per joint angle.
• Advanced techniques are required just to make the essentially continuous
search space finite.
• Real robots must also handle errors in their sensor readings and
motor controls, with partial observability, and with other agents.
27
Quiz 01: The Towers of Hanoi
•• States: Any arrangement
Formulate the Towersofofdisks in three
Hanoi pegs such
problem with that anypegs
three disk is on
and
top of another larger disk.
three disks.
• Initial state:
• Actions: Take the upper disk from one of the rods and sliding it onto
another rod, on top of the other disks that may already be present on
that rod, without violating the rule of disk sizes.
• Transition model: Return a state given a state and an action
• Goal state:
• Best-first search
• Search data structures
Search algorithms
• A search algorithm takes a search problem as input and
returns a solution, or an indication of failure.
• A search tree shows various paths from the initial state,
trying to find a path that reaches a goal state.
• Node = a state in the state space. Edge = action. Root = initial state.
A S
C A B
S
B C C
State space G Search tree
G G
30
State space vs. Search tree
• The state space describes the set of states in the world, and
the actions that allow transitions from one state to another.
• The search tree describes paths between these states,
reaching towards the goal.
• There may be multiple paths any given state, but each node in the
tree has a unique path back to the root.
31
Search tree: An example for Romania map
A partial search tree for finding a route from Arad to Bucharest. Nodes that have been
expanded are lavender with bold letters; nodes on the frontier that have been
generated but not yet expanded are in green; the set of states corresponding to these
two types of nodes are said to have been reached. Nodes that could be generated next
are shown in faint dashed lines.
32
Search tree: Terminologies
• A node is called expanded if we apply available actions onto
that state, leading to the generation of successor nodes.
• Those successors are added to a frontier, available for
expansion at any given point.
• Any state that has had a node generated for it has been
reached.
• Reached nodes = Expanded nodes + Nodes in the frontier.
33
Loopy path can’t be
an optimal path, so
search should not
continue from
there.
34
Search tree: An example for Romania map
A sequence of search trees generated by a graph search on the Romania problem. At each
stage, we have expanded every node on the frontier, extending every path with all
applicable actions that do not result in a state that has already been reached.
35
Graph-search separation property
• The frontier separates two regions of the state-space graph.
• An interior region is where every state has been expanded,
and an exterior region for states not yet been reached.
36
Graph-search separation property
38
function BEST-FIRST-SEARCH(problem, f) returns a solution node or failure
node NODE(STATE=problem.INITIAL)
frontier a priority queue ordered by f, with node as an element
reached a lookup table, with one entry with key problem.INITIAL and value node
while not IS-EMPTY(frontier) do
node POP(frontier)
if problem.IS-GOAL(node.STATE) then return node
for each child in EXPAND(problem, node) do
s child.STATE
if s is not in reached or child.PATH-COST < reached[s].PATH-COST then
reached[s] child
add child to frontier
return failure
40
Search data structures
• The frontier can be implemented with a priority queue (UCS
and A*), FIFO queue (BFS), or LIFO queue (DFS).
• The reached states can be stored in a lookup table (e.g., a
hash table).
• Each element has its state as key and node for that state as value.
41
Redundant paths
• A redundant path can be either a loopy or suboptimal path,
which are both unavoidable.
• Following these paths may turn a tractable problem into an
intractable one.
42
Approaches to redundant paths
• Tree-search algorithms: No check of redundant paths.
• There are some problem formulations where it is rare or impossible
for two paths to reach the same state.
• Graph-search algorithms: Remember all previously reached
states and keep only the best path to each state.
• Appropriate for situations where there are many redundant paths,
and the table of reached states will fit in memory.
“Tree-search” refers to the fact that the state space is treated as if it were a tree, with
only one path from each node back to the root. The state space is still the same graph no
matter how we search it.
43
Approaches to redundant paths
• We can compromise and check for cycles, but not for
redundant paths in general.
• Idea: Follow up the chain of parents to see if the state at the
end of the path has appeared earlier in the path.
• This can be done with no need for additional memory.
• Some implementations follow this chain all the way up to
eliminate all cycles.
• Others trace only a few links (e.g., → parent → grandparent
→ great-grandparent), and thus just detects short cycles.
44
Measuring problem-solving performance
• Completeness: Does it always find a solution if one exists?
• Optimality: Does it always find a least-cost solution?
• Time complexity: How long does it take to find a solution?
• Space complexity: How much memory is needed to perform the
search?
45
Quiz 02: The Towers of Hanoi
• Draw the first two levels of the search tree for the Towers of
Hanoi problem with three pegs and two disks (identical
repeated states on a path can be ignored).
46
47