DFS%2C+BFS+%26+UCS
DFS%2C+BFS+%26+UCS
Search
1
Today
o Agents that Plan Ahead
o Search Problems
2
Why Search Problems?
3
Why Search Problems?
4
Why Search Problems?
o Let’s say there is a real world problem that needs to be
solved.
o Goal: Build an AI agent to solve this problem.
o How to build this agent?
5
Why Search Problems?
o How to build this AI agent?
6
Why Search Problems?
o More interesting problems require the framework of search
problems
o more than one possible alternative needs to be explored before the
problem is solved
o the number of alternatives to search among can be very large
7
Some Characteristics of When to use Search Problems
o Single Agent
8
Reflex Agents
o Reflex agents:
o Choose action based on current percept
o May have memory
o Do not consider the future consequences of
their actions
o Consider how the world IS
10
Video of Demo Reflex Odd
11
Planning Agents
o Planning agents:
o Ask “what if”
o Decisions based on (hypothesized)
consequences of actions
o Must have a model of how the world evolves
in response to actions
o Must formulate a goal (test)
o Consider how the world WOULD BE
13
Video of Demo Mastermind
14
Search Problems
15
Search Problems
o A search problem consists of:
o A state space
“N”, 1.0
o A successor function
(with actions, costs)
“E”, 1.0
o A start state and a goal test
16
Search Problems Are Models
17
Search – Not Just Pathing Problems
18
Search – Not Just Pathing Problems
19
Search – Not Just Pathing Problems
20
Search – Not Just Pathing Problems
21
Example: Traveling in Romania
o State space:
o Cities
o Successor function:
o Roads: Go to adjacent city with
cost = distance
o Start state:
o Arad
o Goal test:
o Is state == Bucharest?
o Solution?
22
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)
23
State Space Sizes?
o World state:
o Agent positions: 120
o Food count: 30
o Ghost positions: 12
o Agent facing: NSEW
o How many
o World states?
120x(230)x(122)x4
o States for pathing?
120
o States for eat-all-dots?
120x(230)
24
Safe Passage
25
State Space Graphs and Search Trees
26
State Space Graphs
27
State Space Graphs
28
Search Trees
This is now / start
“N”, 1.0 “E”, 1.0
Possible futures
o A search tree:
o A “what if” tree of plans and their outcomes
o The start state is the root node
o Children correspond to successors
o Nodes show states, but correspond to PLANS that achieve those states
o For most problems, we can never actually build the whole tree
29
State Space Graphs vs. Search Trees
Each NODE in in
State Space Graph the search tree is Search Tree
an entire PATH in
S
the state space
a G graph. d e p
b c
b c e h r q
e
d f h r p q f
a a
S h We construct both
on demand – and p q f q c G
p q r
we construct as q c G a
little as possible.
a
30
State Space Graphs vs. Search Trees
Consider this 4-state graph: How big is its search tree (from S)?
S G
31
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
… …
33
Search Example: Romania
34
Searching with a Search Tree
o Search:
o Expand out potential plans (tree nodes)
o Maintain a fringe of partial plans under consideration
o Try to expand as few tree nodes as possible
35
General Tree Search
o Important ideas:
o Fringe
o Expansion
o Exploration strategy
36
Example: Tree Search
a G
b c
e
d f
S h
p q r
37
Example: Tree Search
a G
b c
e
d f
S h
p q r
S s
s d
d e p s e
q s p
b c e h r
s d b
a a h r p q f s d c
s d e
p q f q c G s d e h
a s d e r
q c G
s d e r f
a s d e r f c
38
s d e r f G
Depth-First Search
39
Depth-First Search
Strategy: expand a a G
deepest node first b c
Implementation: e
d f
Fringe is a LIFO stack S h
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
40
Search Algorithm Properties
41
Search Algorithm Properties
o Complete: Guaranteed to find a solution if one exists?
o Optimal: Guaranteed to find the least cost path?
o Time complexity?
o Space complexity? 1 node
b
… b nodes
o Cartoon of search tree: b2 nodes
o b is the branching factor m tiers
o m is the maximum depth
o solutions at various depths
bm nodes
o Number of nodes in entire tree?
o 1 + b + b2 + …. bm = O(bm)
42
Depth-First Search (DFS) Properties
o What nodes DFS expand?
o Some left prefix of the tree. 1 node
o Could process the whole tree! b
… b nodes
o If m is finite, takes time O(bm)
b2 nodes
o How much space does the fringe take? m tiers
o Only has siblings on path to root, so O(bm)
o Is it complete?
o m could be infinite, so only if we prevent cycles (more bm nodes
later)
o Is it optimal?
o No, it finds the “leftmost” solution, regardless of depth
or cost
43
Breadth-First Search
44
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
e
Implementation: Fringe d f
is a FIFO queue S h
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
45
Breadth-First Search (BFS) Properties
o What nodes does BFS expand?
o Processes all nodes above shallowest solution 1 node
b
o Let depth of shallowest solution be s … b nodes
s tiers
o Search takes time O(bs) b2 nodes
o Is it complete? bm nodes
o Is it optimal?
o Only if costs are all 1 (more on costs later)
46
Quiz: DFS vs BFS
47
DFS vs BFS
49
Video of Demo Maze Water DFS/BFS (part 2)
50
Iterative Deepening
o Idea: get DFS’s space advantage with
BFS’s time / shallow-solution advantages b
o Run a DFS with depth limit 1. If no solution… …
51
Cost-Sensitive Search
a GOAL
2 2
b c
3
2
1 8
2 e
3 d
f
9 8 2
START h
1 4 2
p 4 r
15
q
53
Uniform Cost Search
2 a G
Strategy: expand a b c
cheapest node first: 1 8 2
2 e
3 d f
Fringe is a priority queue 9 2
S h 8 1
(priority: cumulative cost)
1 p q r
15
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
54
Uniform Cost Search (UCS) Properties
o What nodes does UCS expand?
o Processes all nodes with cost less than cheapest solution!
o If that solution costs C* and arcs cost at least ε , then the “effective b c≤1
…
depth” is roughly C*/ε c≤2
o Takes time O(bC*/ε) (exponential in effective depth) C*/ε “tiers”
c≤3
o How much space does the fringe take?
o Has roughly the last tier, so O(bC*/ε)
o Is it complete?
o Assuming best solution has a finite cost and minimum arc cost is
positive, yes!
o Is it optimal?
o Yes! (Proof next lecture via A*)
55
Uniform Cost Issues
o Remember: UCS explores increasing cost c≤1
…
contours c≤2
c≤3
o The bad:
o Explores options in every “direction”
o No information about goal location
Start Goal
57
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or UCS? (part
1)
58
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or UCS? (part
2)
59
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or UCS? (part
3)
60
The One Queue
o All these search algorithms are the
same except for fringe strategies
o Conceptually, all fringes are priority
queues (i.e. collections of nodes with
attached priorities)
o Practically, for DFS and BFS, you can
avoid the log(n) overhead from an
actual priority queue, by using stacks
and queues
o Can even code one implementation
that takes a variable queuing object
61
Search and Models
62
Search Gone Wrong?
63