Lec03 Dfs Bfs Ucs
Lec03 Dfs Bfs Ucs
Outline
• Agents that plan ahead
• Search problems
• State space graphs and search trees
• Uninformed search methods:
o Depth-First Search (DFS)
o Breadth-First Search (BFS)
o Uniform-Cost Search (UCS)
• Summary and discussion
Planning Agents
o A state space S
o An initial state s0
(The state in which an agent exists initially) N -9
o Actions A(s) in each state
E -9
o Transition model Result(s,a)
o A goal test G(s)
▪ S has no dots left
o Action cost c(s,a,s’)
▪ +1 per step; -10 food; -500 win; +500 die; -200 eat ghost
A search state keeps only the details needed for planning (abstraction)
a G
b c
e
d f
S h
p r
q
a G d e p
b c
b c e h r q
e
d f a a h r p q f
S h We construct the
tree on demand – p q f q c G
p q r
and we construct as q c G a
little as possible.
a
Quiz: State Space Graphs vs. Search Trees
Consider this 4-state graph: How big is its search tree (from S)?
S G
b
Quiz: State Space Graphs vs. Search Trees
Consider this 4-state graph: How big is its search tree (from S)?
a s
a b
S G
b G a G
b a G b G
… …
Important: Those who don’t know history are doomed to repeat it!
Quiz: State Space Graphs vs. Search Trees
Consider a rectangular grid: How many states within 𝑑 steps of start?
= 2𝑑2
= 4𝑑
(a) (b)
Tree Search
Search Example: Romania
Creating the Search Tree
Arad
Arad
Arad
Arad
Arad
• Main variations:
o Which leaf node to expand next
o Whether to check for repeated states
o Data structures for frontier, expanded nodes
Systematic Search
frontier
reached = unexplored
expanded U frontier expanded
d e p
b c e h r q
a a h r p q f
p q f q c G
q c G a
a
Search Algorithm Properties
Search Algorithm Properties
• Complete: Guaranteed to find a solution if one exists?
• Optimal: Guaranteed to find the least cost path?
• Time complexity?
1 node
𝑏
• Space complexity? … 𝑏 nodes
𝑏2 nodes
• Cartoon of search tree: 𝑚 tiers
o 𝑏 is the branching factor
o 𝑚 is the maximum depth
o Solutions at various depths 𝑏𝑚 nodes
• Is it complete?
o 𝑚 could be infinite
𝑏𝑚 nodes
o Preventing cycles may help (more later)
• Is it optimal?
o No, it finds the “leftmost” solution, regardless of
depth or cost
Breadth-First Search
Breadth-First Search
a G
Strategy: expand a shallowest
b c
node first
e
d f
Implementation: Frontier is a S h
FIFO queue p q r
d e p
Search
b c e h r q
Tiers
a a h r p q f
p q f q c G
q c G a
a
Breadth-First Search (BFS) Properties
• What nodes does BFS expand?
o Processes all nodes above shallowest solution
o Let depth of shallowest solution be 𝑠
o Search takes time 1 + 𝑏 + 𝑏2 + ⋯ + 𝑏𝑠 is 𝑂(𝑏𝑠)
1 node
𝑏
• How much space does the frontier take? … 𝑏 nodes
o Has roughly the last tier, so 𝑂(𝑏𝑠) 𝑠 tiers
𝑏2 nodes
• Is it complete? 𝑏𝑠 nodes
o 𝑠 must be finite if a solution exists, so yes!
• Is it optimal?
o No, since it does not consider costs when 𝑏𝑚 nodes
determining which node to replace on the
frontier
o Yes for special case where all edge costs are
equal (e.g., 1), it reduces to uniform cost search
(below)
Quiz: DFS vs BFS
Quiz: DFS vs BFS
S 0
d 3 e 9 p 1
b 4 c e 5 h 17 r 11 q 16
11
Cost a 6 a h 13 r 7 p q f
contours
p q f 8 q c G
q 11 c G 10 a
a
Uniform Cost Search (UCS) Properties
• What nodes does UCS expand?
o Processes all nodes with cost less than cheapest solution!
o If that solution costs 𝐶 ∗ and arcs cost at least , then the
“effective depth” is roughly 𝐶 ∗ /
𝐶 ∗ Τ𝜀 𝑏
o Takes time 𝑂 𝑏 (exponential in effective depth) … 𝑔 1
𝐶 ∗ / 𝑔 2
• How much space does the frontier take?
“tiers” 𝑔 3
o Roughly, all nodes at the level of the cheapest solution, so
the space complexity of UCS is estimated as roughly the
𝐶 ∗ Τ𝜀
nodes of the last tier, so 𝑂 𝑏
• Is it complete?
o Assuming 𝐶 ∗ is finite and > 0, yes!
• Is it optimal?
o Yes! (Proof next lecture via A*)
Video of Demo Empty UCS
Video of Demo Maze with Deep/Shallow Water ---
BFS or UCS? (part 1)
Video of Demo Maze with Deep/Shallow Water ---
BFS or UCS? (part 2)
Summary and Discussion