Chapter 3 - Searching-Part 1
Chapter 3 - Searching-Part 1
2
Solving Problems by Searching
• How an agent can look ahead to find a sequence of actions (a plan)
that will achieve its goal.
6
Four-phase problem-solving process
• Search: Before taking any action in the real world, the agent simulates
sequences of actions in its model, searching until it finds a sequence of
actions that reaches the goal.
• Such a sequence is called a solution.
• The agent might have to simulate multiple sequences that do not reach the
goal, but eventually it will find a solution, or it will find that no solution is
possible.
• Execution: The agent can now execute the actions in the solution, one
at a time.
7
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?
8
Search Space Definitions
• State
• A description of a possible state of the world
• Includes all features of the world that are relevant to the problem
• Initial state
• Description of all relevant aspects of the state in which the agent starts the search
• Goal test
• Conditions the agent is trying to meet (e.g., have $1M)
• Goal state
• Any state which meets the goal condition
• Thursday, have $1M, live in NYC
• Friday, have $1M, live in Valparaiso
• Action
• Function that maps (transitions) from one state to another
9
Search Space Definitions
• Problem formulation
• Describe a general problem as a search problem
• Solution
• Sequence of actions that transitions the world from the initial state to a goal state
• Solution cost (additive)
• Sum of the cost of operators
• Alternative: sum of distances, number of steps, etc.
• Search
• Process of looking for a solution
• Search algorithm takes problem as input and returns solution
• We are searching through a space of possible states
• Execution
• Process of executing sequence of actions (solution)
10
Example – Eight Puzzle
Solver: https://deniz.co/8-puzzle-solver/
11
Example – Towers of Hanoi
Solver:
https://www.mathsisfun.com/games/towerofhanoi.html
12
Example – Eight Queens
Game: https://www.brainmetrix.com/8-queens/
Solutions:
https://users.encs.concordia.ca/~chvatal/8queens.html
13
Example - Route Finding Problem
• States: locations
14
Example - Automatic Assembly Sequencing
15
Searching for Solutions
16
State Space Graphs and Search Trees
17
State Space Graphs
o State space graph: A mathematical
representation of a search problem
a G
o Nodes are (abstracted) world configurations
o Arcs represent successors (action results) b c
o The goal test is a set of goal nodes (maybe only
e
one) d f
S h
o In a search graph, each state occurs only
p r
once! q
18
State Space Graphs vs. Search Trees
a G 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
19
State Space Graphs vs. Search Trees
Consider this 4-state graph: How big is its search tree (from S)?
s
a
a b
S G b G a G
a G b G
b
… …
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
22
General Tree Search
o Important ideas:
o Fringe
o Expansion
o Exploration strategy
a G
b c
e
d f
S h
p r
q
24
Example: Tree Search
a G
b c
e
d f
S h
p q r
S s
s→d
d e p s→e
s→p
b c e h r q s→d→b
s→d→c
a a h r p q f s→d→e
s→d→e→h
p q f q c G s→d→e→r
a s→d→e→r→f
q c G
s→d→e→r→f→c
a s→d→e→r→f→G 25
Search Algorithms
• Each node in the search tree corresponds to a state in the state space
and the edges in the search tree correspond to actions.
• The root of the tree corresponds to the initial state of the problem.
26
Search Algorithms
• 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.
• The search tree may have multiple paths to any given state, but each
node in the tree has a unique path back to the root (as in all trees).
27
Searching Strategies
28
Depth-First Search (DFS)
29
Depth-First Search (DFS)
a G
b c
Strategy: expand a
e
deepest node first d f
S h
p q r
Implementation:
Fringe is a LIFO stack S
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
30
a
Depth-First search
31
Example - DFS
32
Example - DFS
33
Example - DFS
34
Properties of DFS
o Complete: Guaranteed to find a solution if one exists?
o Optimal: Guaranteed to find the least cost path?
o Time complexity?
1 node
o Space complexity? b
… b nodes
b2 nodes
o Cartoon of search tree:
m tiers
o b is the branching factor
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)
35
Properties of DFS
o What nodes DFS expand?
o Some left prefix of the tree. 1 node
b
o Could process the whole tree! … b nodes
o If m is finite, takes time O(bm) b2 nodes
m tiers
o How much space does the fringe take?
o Only has siblings on path to root, so O(bm)
bm nodes
o Is it complete?
o m could be infinite, so only if we prevent
cycles (more later)
o Is it optimal?
o No, it finds the “leftmost” solution,
regardless of depth or cost 36
Example
• Apply DFS algorithm to the following graph starting from node S.
Show the contents of the stack.
A B C
38
Example
39
Example
40
Example
41
Example
42
Example
43
Example
44
Example
45
Example
46
Example
47
Example
48
Example
49
Example
50
Example
51
Example
52
53
Breadth-first search (BFS)
54
Breadth-first search
Strategy: expand a a G
shallowest node first b c
Implementation: e
d f
Fringe is a FIFO S h
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
55
a
Breadth-first search
• The root node is expanded first, then all the successors of the root
node are expanded next, then their successors, and so on.
56
Prosperities of BFS
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
bm nodes
o Is it complete?
o s must be finite if a solution exists
o Is it optimal?
o Only if costs are all 1 (more on costs later)
57
Breadth-first search
58
FIFO queue
• Enqueue the starting node S and mark it as visited. S
A B C
Output front QUEUE
S
D
S
Nodes adjacent to S: A,B,C
A B C (alphabetical order)
S, A
B C D D is adjacent to A
S, A, B
C D No adjacent to B
S, A, B, C
D No adjacent to C
S, A, B, C, D
No adjacent to D
59
Breadth-first search
60
Breadth-first search
61
Breadth-first search
62
Breadth-first search
63
Breadth-first search
64
Breadth-first search
65
Breadth-first search - Code
66
67
BFS vs DFS
69
Video of Demo Maze Water DFS/BFS (part 2)
70
Question?
• Consider the following graph. If there is ever a decision between multiple
neighbor nodes in the BFS or DFS algorithms, assume we always choose the letter
closest to the beginning of the alphabet first.
• In what order will the nodes be visited using a Breadth First Search?
The answer is: ABDCEGHF
• In what order will the nodes be visited using a Depth First Search?
The answer is: ABCEHFGD 71
Iterative Deepening
o Idea: get DFS’s space advantage with
BFS’s time / shallow-solution b
advantages …
73
Iterative Deepening (search l =2)
74
Iterative Deepening (search l =3)
75
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
1 q
5
It does not find the least-cost path. We will now cover a similar algorithm that How?
does find the least-cost path.
76
Uniform Cost Search (UCS)
77
Uniform Cost Search
2 a G
Strategy: expand a b c
1 8 2
cheapest node first: 2 e
3 d f
9 2
S h 8 1
Fringe is a priority queue 1 p r
q
(priority: cumulative cost) 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
a 78
Properties of UCS
o What nodes does UCS expand?
o Processes all nodes with cost less than cheapest solution!
b c1
o If that solution costs C* and arcs cost at least , then the …
“effective depth” is roughly C*/ c2
C*/ “tiers”
o Takes time O(b ) (exponential in effective depth)
C*/
c3
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*) 79
Uniform Cost Search
o Remember: UCS explores increasing cost … c1
contours c2
c3
o The bad:
o Explores options in every “direction”
o No information about goal location
Start Goal
80
Uniform-Cost-First
• Visits the next node which has the least total cost from the root, until a
goal state is reached.
• g(n) = path cost(n) = sum of individual edge costs to reach the current
node.
81
UCS Example
Open list: C
82
UCS Example
83
UCS Example
84
UCS Example
85
UCS Example
86
UCS Example
87
UCS Example
88
UCS Example
89
UCS Example
90
UCS Example
91
UCS Example
92
UCS Example
93
UCS Example
Open list: S(5) N(5) R(6) Z(6) F(6) G(7) D(8) L(10)
94
UCS Example
95
UCS Example
96
UCS Example
97
UCS Example
98
UCS Example
99
Uniform-Cost-First
100
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 1)
101
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 2)
102
Video of Demo Maze with Deep/Shallow Water --- DFS, BFS, or
UCS? (part 3)
103