0% found this document useful (0 votes)
47 views47 pages

Lec03 Dfs Bfs Ucs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views47 pages

Lec03 Dfs Bfs Ucs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Search

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

• Planning agents decide based on evaluating


future action sequences
• Must have a model of how the world evolves
in response to actions
• Usually have a definite goal
• Optimal: Achieve goal at least cost
Example: Move to Nearest Dot and Eat It
Example: Precompute Optimal Plan, Execute It
Search Problems
Search Problems
• A search problem consists of:

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 solution is an action sequence that reaches a goal state


• An optimal solution has least cost among all solutions
Search Problems are Models
Example: Traveling in Romania
Example: Traveling in Romania
• State space:
o Cities
• Initial state:
‫٭‬Start
o Arad
• Actions:
o Go to adjacent city
• Transition model:
o Reach adjacent city
• Goal test:
o s = Bucharest?
• Action cost: × Finish
o Road distance from s to s’
• Solution?
What’s in a State Space?
The world state includes every last detail of the environment

A search state keeps only the details needed for planning (abstraction)

• Problem: Pathing • Problem: Eat-All-Dots


o States: (x,y) location o States: {(x,y), dot Booleans}
o Actions: NSEW o Actions: NSEW
o Transition: update x,y value o Transition: update x,y and
o Goal test: is (x,y)=destination possibly a dot Boolean
o Goal test: dots all false
State Space Sizes
• World state:
o Agent positions: 120
o Food count: 30
o Ghost positions: 12
o Agent facing: NSEW
• How many
o World states?
120 × (230) × (122) × 4
o States for pathing?
120
o States for eat-all-dots?
120 × (230)
State Space Graphs and Search Trees
State Space Graphs
• State space graph: A mathematical representation
of a search problem:
o Nodes are (abstracted) world configurations
o Arcs represent transitions (labeled with actions)
equivalently successors (action results)
o The goal test gives a set of goal nodes (maybe only
one)
• In a state space graph, each state occurs only
once!
• We can rarely build this full graph in memory (it’s
too big), but it’s a useful idea
Example State Space Graph

a G
b c

e
d f
S h
p r
q

Tiny state space graph for a tiny


search problem
State Space Graphs vs. Search Trees

Each NODE in the


State Space Graph search tree is an Search Tree
entire PATH in the
state space graph. S

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

How many states in search tree of depth 𝑑?

= 4𝑑

(a) (b)
Tree Search
Search Example: Romania
Creating the Search Tree

Arad

Sibiu Timisoara Zerind

Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

Arad

Sibiu Timisoara Zerind


Arad Fagaras Creating the Search Tree
Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

Arad

Sibiu Timisoara Zerind

Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

Arad

Sibiu Timisoara Zerind


Arad Fagaras Creating the Search Tree
Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

Arad

Sibiu Timisoara Zerind

Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea


General Tree Search

• 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

1. Frontier separates expanded from unexplored region of state-space graph


2. Expanding a frontier node:
a. Moves a node from frontier into expanded
b. Adds nodes from unexplored into frontier (maintains property 1)
Uninformed Search
Depth-First Search
Depth-First Search
Strategy: expand a deepest a G
b c
node first
e
d f
Implementation: Frontier is S h
a LIFO stack p q r

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

• Number of nodes in entire tree?


1 + 𝑏 + 𝑏2 + … . 𝑏𝑚 = 𝑂(𝑏𝑚)
Depth-First Search (DFS) Properties
• What nodes does DFS expand?
o Left prefix of the tree down to depth 𝑚
o Could process the whole tree!
o If 𝑚 is finite, takes worst case time 𝑂(𝑏𝑚) 1 node
𝑏
… 𝑏 nodes
• How much space does the frontier take?
𝑏2 nodes
o Only has siblings on path to root, so worst case is
𝑂(𝑏𝑚) 𝑚 tiers

• 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

• When will DFS outperform BFS?


o Computer with lower/limited memory
o Many goals at high depth, 𝑚
• When will BFS outperform DFS?
o Shortest path to goal without searching entire tree
o Goal is shallow, 𝑠 << 𝑚, and in the right

[Demo: dfs/bfs maze water]


Example: Maze Water DFS/BFS (part 1)
Example: Maze Water DFS/BFS (part 2)
Iterative Deepening
• Idea: get DFS’s space advantage with BFS’s time /
shallow-solution advantages
𝑏
o Run a DFS with depth limit 1. If no solution… …
o Run a DFS with depth limit 2. If no solution…
o Run a DFS with depth limit 3. …..

• Isn’t that wastefully redundant?


o Generally most work happens in the lowest level
searched, so not so bad!
Uniform Cost Search
Uniform Cost Search
𝑔(𝑛) = cost from root to 𝑛 2 a G
b c
1 8 2
Strategy: expand lowest 𝑔(𝑛) 3 d
2 e
9 2 f
S h 8
Implementation: Frontier is a 1
1 p r
priority queue sorted by 𝑔(𝑛) 15
q

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

• Search problems as models


• State space graphs is a useful representation
• Search trees can suffer from repetitions
• Uninformed search:
o Use known information for distance from starting node
o Can be complete and optimal
o “Uninformed” for distance to goal
o Computationally intensive

You might also like