Solving Problems by Searching
Queue-Based Search
1
Outline
• Agents that Plan Ahead
• Search Problems
• Uninformed Search Methods
– Depth-First Search
– Breadth-First Search
– Uniform-Cost Search
2
Reflex Agents
• Reflex agents:
– Choose action based on
current percept (and
maybe memory)
– May have memory or a
model of the world’s
current state
– Do not consider the
future consequences of
their actions
– Consider how the world IS
• Can a reflex agent be rational?
3
Goal Based Agents
• Goal-based agents:
– Plan ahead
– Ask “what if”
– Decisions based on
(hypothesized)
consequences of
actions
– Must have a model of
how the world evolves
in response to actions
– Consider how the
world WOULD BE
Also called Problem solving agents
4
What is search for?
• Many problems can be formulated as search problems.
• Search problems: finding sequences of actions (Plan) that leads
us to a goal
• Steps:
– Formulate problem
– Search for solution
– Execute
• Model of the world: single agents, deterministic actions, fully
observed state, discrete state space
5
Search Problems
• A search problem consists of:
– A start (initial) state
– The state space: the set of all states reachable from initial state by
any sequence of actions
– A set of actions available in each state
– A successor function
describing action results
and cost per action (step)
– A goal test
• A solution is a sequence of actions (a plan)
which leads from the start state to a goal state
6
Example: Romania
• State space:
– Cities,
• Successor function
– Roads:
Go to adj city with
cost = dist
• Start state:
– Arad
• Goal test:
– Is state ==
Bucharest?
• Solution?
7
What’s in a state space?
A search state keeps only the details needed to support the
search process (abstraction)
• Problem: Pathing
− States: (x, y) location
− Actions: N,S,E,W
− Successor: update
location only
− Goal test: is (x, y) = END
• Problem: Eat-All-Dots
− States: {(x, y), dot booleans}
− Actions: N, S, E, W
− Successor: update location
and possibly a dot boolean
− Goal test: dots all false
8
State space sizes?
• World state:
–
–
–
–
•
Agent positions: 120
Food count: 30
Ghost positions: 12
Agent facing: NSEW
How many
– World states?
120*(230)*(122)*4
– States for pathing?
120
– States for eat-all-dots?
120*(230)
9
State space graphs
• State space graph: A
mathematical
representation of a
search problem
– For every search problem,
there’s a corresponding
state space graph
An tiny search graph
– The nodes are the states
For a tiny search problem
– The successor function is
represented by arcs, the costs will be the weights on arcs, and
action as labels on arcs
• We can rarely build this graph in memory (so we don’t)
• We need algorithms to find solution without building the whole
10
graph
Toy Example: Vacuum World
• State Space of vacuum cleaner agent:
11
Search Trees
• A search tree:
–
–
–
–
–
–
This is a “what if” tree of plans and outcomes
Start state at the ROOT node
EDGES are actions
CHILREN correspond to successors
Nodes contain states, corresponding to PLANS to those states
For most problems, we can never actually build the whole tree
12
Another Search Tree
• Search for solution:
– Expand out possible plans
– Maintain a fringe (frontier) of unexpanded plans
– Try to expand as few tree nodes as possible
13
General Tree Search
• Important ideas:
– Fringe (frontier)
– Expansion
– Exploration strategy
•
Search Strategies
or
Search Algorithms
Main question: which fringe nodes to explore?
14
Example: Tree Search
S
S→p
S→e
S→d
S→e→h
S→e→r
S→e→r→f
S→e→r→f→G
S→e→r→f→c
15
State Graphs vs. Search Trees
Each NODE in the
search tree
is an entire PATH in
the problem graph.
We construct both
on demand – and
we construct
as little as possible.
Uninformed Search Strategies
• No additional information about states
beyond that provided in the problem definition,
• All they do:
– Generate successors,
– Distinguish the goal
Depth-first Search
...
17
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
18
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
19
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
20
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
21
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
22
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
23
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
24
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
25
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
26
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
27
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
28
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
29
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
30
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
31
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
32
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
33
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
34
Depth First Search (DFS)
• Strategy: expand
deepest node first
• Implementation:
Fringe is a
LIFO queue (stack)
35
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
36
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
37
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
38
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
39
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
40
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
41
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
42
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
43
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
44
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
45
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
46
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
47
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
48
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
49
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
50
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
51
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
52
Breadth First Search (BFS)
• Strategy: expand
shallowest node first
• Implementation:
Fringe is a
FIFO queue
53
Search Algorithm Properties
Complete? Guaranteed to find a solution if one exists?
Optimal? Guaranteed to find the least cost solution?
Time complexity? How long it takes to find the solution?
Space complexity? How much memory needed for search?
Variables
nn
Number of states in the problem (huge)
bb
The average branching factor
(the average number of successors of any node)
C*
C*
dd
Cost of optimal (least cost) solution
m
Max depth of the search tree
Depth of the shallowest solution
54
DFS
Algorithm
DFS
Depth First
Search
Complete
Optimal
Time
Space
N
N
Infinite
Infinite
• Infinite paths (loops in the graph) make DFS incomplete…
• How can we fix this?
55
DFS With Cycle Checking
• With cycle checking, DFS is complete.*
Algorithm
DFS
with Path
checking
Complete
Optimal
Time
Space
Y
N
O(bm)
O(bm)
• When is DFS optimal?
– When the shallowest goal is in depth m.
* Or graph Search – next lecture.
BFS
Algorithm
DFS
BFS
with path
checking
Complete
Optimal
Time
Space
Y
N
O(bm)
O(bm)
Y*
N*
O(bd)
O(bd)
• When is BFS Complete?
– The shallowest goal is
d
in finite depth d, and
– The branching factor is finite
bd
• When is BFS optimal?
– The path cost is a non-decreasing
function of the depth of the node,
common scenario: all actions have the same cost
57
Comparisons
• When will BFS outperform DFS?
– Complete and optimal*
– But time and space not good
– Overall, preferred when the goals are shallow
• When will DFS outperform BFS?
– Never good
– Except when memory is the real concern
58
Iterative Deepening DFS (IDS)
Iterative deepening: BFS using DFS as a subroutine:
1.
Do a DFS which only searches
paths of length 1 or less.
2. If “1” failed, do a DFS which only searches
paths of length 2 or less.
3. If “2” failed, do a DFS which only searches
paths of length 3 or less.
….and so on.
Algorithm
Complete
Optimal
Time
Space
Y
N
O(bm)
O(bm)
BFS
Y*
N*
O(bd)
O(bd)
IDS
Y*
N*
O(bd)
O(bd)
DFS
with path
checking
IDS Example
60
Costs on Actions
Notice that BFS finds the shortest path in terms of number of
transitions (or solution steps). It does not find the least-cost path.
We will quickly cover an algorithm which does find the least-cost
61
path.
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
62
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
3
9
1
63
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
3
9
1
16
64
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
11
5
1
16
65
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
11
5
1
16
6
66
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
16
5
11
13
1
7
67
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
16
5
11
13
1
7
68
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
16
5
11
13
1
7
8
69
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
16
5
11
13
1
7
8
11
10
70
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
5
11
13
17
1
11
16
7
8
11
10
71
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
0
9
3
4
6
5
11
13
17
1
11
16
7
8
11
10
72
Costs on Actions
Notice that BFS finds the shortest path in terms of number of
transitions (or solution steps). It does not find the least-cost path.
We will quickly cover an algorithm which does find the least-cost
73
path.
Uniform Cost Search (UCS)
• Strategy: expand
cheapest node first
• Implementation:
Fringe is a Priority queue
(priority: cumulative cost)
74
UCS
Algorithm
Complete
Optimal
Time
Space
Y
N
O(bm)
O(bm)
BFS
Y*
N*
O(bd)
O(bd)
IDS
Y*
N*
O(bd)
O(bd)
UCS
Y*
Y
O(b1+C*/ε)
O(b1+C*/ε)
DFS
with path
checking
• When UCS is complete?
– When the cost of
every step exceeds
some small
positive constant epsilon
75
Uniform Cost Issues
• Remember: explores
increasing cost contours
•
The good: UCS is
complete and optimal!
• The bad:
– Explores options in every
“direction”
– No information about goal
location
76
Informed (Heuristic) Search Strategies
• Use problem specific knowledge
beyond the problem definition itself.
• Use heuristics to import additional knowledge to the problem
• Types
– Greedy best-first search
– A* Search
77
© Copyright 2026 Paperzz