chapter03-2015
chapter03-2015
Chapter 3
Chapter 3 1
How to Solve a (Simple) Problem
7 2 4 1 2
5 6 3 4 5
8 3 1 6 7 8
Chapter 3 2
Introduction
Simple goal-based agents can solve problems via searching the state space
for a solution, starting from the initial state and terminating when (one of )
the goal state(s) are reached.
The search algorithms can be blind or informed (using heuristics).
Before we see how we can search the state space of the problem, we need
to decide on what the states and operators of a problem are.
⇒ problem formulation
Chapter 3 3
Example: Traveling in Romania
On holiday in Romania; currently in Arad.
Flight leaves tomorrow from Bucharest
You have access to a map.
Oradea
71
Neamt
Zerind 87
75 151
Iasi
Arad
140
92
Sibiu Fagaras
99
118
Vaslui
80
Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
85 Hirsova
Mehadia 146 101 Urziceni
75 138 86
Bucharest
Dobreta 120
90
Craiova Eforie
Giurgiu
Chapter 3 4
Example: Romania
Imagine that this is not given as a graph as in here (since you see the solution
easily), but as a list of roads from each city to another. This is in fact how
a robot will see the map, as a list of edges on a graph (with also associated
distances):
Arad to Zerind, Sibiu, Timisoara
Bucharest to Pitesti, Guirgiu, Fagaras, Urziceni
Craiova to Dobreta, Pitesti
Dobreta to Craiova, Mehadia
...
Oradea to Zerind, Sibiu
...
Zerind to Oradea, Arad
Chapter 3 5
Example: Traveling in Romania
Formulate goal:
be in Bucharest
Formulate problem:
Action sequence needs to be in the form of ”drive from Arad to ...; drive
from ... to ...; ...; drive from ... to Bucharest). Hence the states of the
robot, abstracted for this problem are ”various cities”.
The corresponding operators taking one state to the other are ”driving be-
tween cities”.
Find solution: sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Chapter 3 6
Selecting a state space
Real world is absurdly complex
⇒ state space must be abstracted for problem solving
(Abstract) state = set of real states
(Abstract) operator = complex combination of real actions
e.g., “Arad → Zerind” represents a complex set
of possible routes, detours, rest stops, etc.
(Abstract) solution =
set of real paths that are solutions in the real world
Chapter 3 7
Single-state problem formulation
A problem is defined by four items:
initial state e.g., “at Arad”
operators (or successor function S(x))
e.g., Arad → Zerind Arad → Sibiu etc.
goal test, can be
explicit, e.g., x = “at Bucharest”
implicit, e.g., N oDirt(x)
path cost (additive)
e.g., sum of distances, number of operators executed, etc.
A solution is a sequence of operators leading from the initial state to a goal
state.
Chapter 3 8
Example: vacuum world
Your robot needs to vacuum a two-room area. Each room may have dirt in
it and the robot may be in one of the rooms and move left or right to go to
the other room.
What are the states of this vacuum world?
Chapter 3 9
Example: vacuum world
The 8 States:
1 2
3 4
5 6
7 8
Chapter 3 10
Example: vacuum world
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
states??
operators??
goal test??
path cost??
Chapter 3 11
Example: vacuum world
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
Chapter 3 12
Example: 8-puzzle
5 4 5
1 4
2 3
6 1 88 6
8 84
7 3 22 7 6 25
states??
operators??
goal test??
path cost??
Chapter 3 13
Example: The 8-puzzle
5 4 5
1 4
2 3
6 1 88 6
8 84
7 3 22 7 6 25
Chapter 3 14
Example: robotic assembly
P
R R
R R
Chapter 3 15
Problem-solving agents
Restricted form of general agent, an intelligent agent will solve problems
among others):
Chapter 3 16
Implementation of search algorithms
Basic idea:
offline, simulated exploration of state space
by generating successors of already-explored states
(a.k.a. expanding states)
Chapter 3 17
Tree search example
(a) The initial state Arad
Chapter 3 18
Implementation: states vs. nodes
A state is a (representation of) a physical configuration
A node is a data structure constituting part of a search tree
includes state, parent, children, operator, depth, path cost g(x)
States do not have parents, children, depth, or path cost!
parent
5 4
depth = 6
State Node
g=6
6 1 88
state
7 3 22
children
The Expand function creates new nodes, filling in the various fields and
using the Operators (or SuccessorFn) of the problem to create the
corresponding states.
Chapter 3 19
Terminology
♦ depth of a node: number of steps from root (starting from depth=0)
♦ path cost: cost of the path from the root to the node
♦ expanding a node: pulling it out from the queue, goal test and expanding
(interchangeable with visiting a node)
♦ generated nodes: different than nodes expanded!
Chapter 3 20
Implementation of search algorithms
Chapter 3 21
Implementation of search algorithms
Notice that we will always take a node from the front of the Queue (called
the Fringe), so insertion of the expanded nodes (depending on the Queueing
Function) is what distinguishes between different search strategies.
The GeneralSearch (next slide) was the skeleton search algorithm given in-
stead of the TreeSearch in AIMA’s (our book) first edition, highlighting the
dependence to the Queueing Function:
Chapter 3 22
function General-Search( problem, Queuing-Fn) returns a solution, or
failure
nodes ← Make-Queue(Make-Node(Initial-State[problem]))
loop do
if nodes is empty then return failure
node ← Remove-Front(nodes)
if Goal-Test[problem] applied to State(node) succeeds then return
node
nodes ← Queuing-Fn(nodes, Expand(node, Operators[problem]))
end
Chapter 3 23
Search strategies
A strategy is defined by picking the order of node expansion
Strategies are evaluated along the following dimensions:
completeness—does it always find a solution if one exists?
time complexity—maximum number of nodes generated/expanded
(the slides mostly use visited (goal test and expand if necessary)
nodes)
space complexity—maximum number of nodes in memory
optimality—does it always find a least-cost solution?
Time and space complexity are measured in terms of
b—maximum branching factor of the search tree (finite)
d—depth of the least-cost solution
m—maximum depth of the state space (may be ∞)
Chapter 3 24
Time Complexity
An algorithm’s time complexity is often measured asymptotically. Assume
you ”process” n items with your algorithm. We say that the time
♦ The time complexity analysis can be done (separately) for the worst case
and average case
♦ In simple terms, it checks what is the dominating factor in the spent
time, for large enough problem size (n).
Chapter 3 25
Time Complexity
♦ Some problems can be solved in polynomial time (P). These are considered
as ”easy” problems (e.g. O(n), O(logn) algorithms.
♦ Some problems do not have a polynomial-time solution, but can be
verified in polynomial time if one can guess the solution. They are called
non-deterministic polynomial (NP) problems.
♦ NP-complete problems: those ”harder” NP problems that if you find
a polynomial time solution, you can solve all the other NP problems (by
reducing one problem into another).
♦ Read Appendix pp.977-979 on time complexity
Chapter 3 26
Uninformed search strategies
Uninformed strategies use only the information available
in the problem definition
Breadth-first search
Uniform-cost search
Depth-first search
Depth-limited search
Iterative deepening search
Chapter 3 27
Breadth-first search
Expand shallowest unexpanded node
Implementation:
QueueingFn = first in first out (FIFO)
Chapter 3 28
Breadth-first search
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
Chapter 3 29
Breadth-first search
Arad
Chapter 3 30
Breadth-first search
Arad
Chapter 3 31
Breadth-first search
Arad
Arad Oradea
Chapter 3 32
Breadth-first search
Arad
Rimnicu
Arad Oradea Arad Oradea Fagaras Vilcea Arad Lugoj
Chapter 3 33
Properties of breadth-first search
Complete??
Time??
Space??
Optimal??
Chapter 3 34
Properties of breadth-first search
Complete?? Yes (if b is finite - otherwise it may be stuck at generating the
first level)
Chapter 3 35
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time: ?? 1 + b + b2 + b3 + . . . + bd + (bd+1 − b) = O(b(d+1)), using the basic
tree search algorithm.
Time: ?? 1 + b + b2 + b3 + . . . + bd = O(bd), using the tree search algorithm
that is modified so that last level is not generated in BFS (fig. 3.11, in next
slide)
The exact numbers depend on a particular code for the implementation. For
instance when the goal test is in the code...
Note: To be precise, we have to specify whether we are talking about vis-
ited/expanded or generated nodes.
Chapter 3 36
Chapter 3 37
Properties of breadth-first search
Chapter 3 38
Properties of breadth-first search
Complete?? Yes (if b is finite)
Time: ?? 1 + b + b2 + b3 + . . . + bd + (bd+1 − b) = O(b(d+1)), using the basic
tree search algorithm
Time: ?? 1 + b + b2 + b3 + . . . + bd = O(bd), using the tree search algorithm
that is modified so that last level is not generated in BFS (fig. 3.11)
Space?? O(bd) for fig.3.11 algorithm
Optimal?? No (Yes if cost = 1 per step); not optimal in general
Note: BFS finds the shallowest solution; if the shallowest solution is not the
optimal one (step costs are not uniform) than BFS is not optimal.
Chapter 3 39
Time-Space Requirements
Assuming b = 10 and processing speed of 1000 nodes/second (100 bytes/node).
Chapter 3 40
Time-Space Requirements
Exponential complexity search problems cannot be solved for all but smallest
instances!
Chapter 3 41
Romania with step costs in km
BFS finds the shallowest goal state. What if we have a more general path
cost?
Straight−line distance
Oradea to Bucharest
71
Neamt Arad 366
87 Bucharest 0
Zerind 151
75 Craiova 160
Iasi Dobreta 242
Arad 140 Eforie 161
92 Fagaras 178
Sibiu 99 Fagaras
Giurgiu 77
118
Vaslui Hirsova 151
80
Iasi 226
Rimnicu Vilcea Lugoj
Timisoara 244
142 Mehadia 241
111 211 Neamt 234
Lugoj 97 Pitesti
Oradea 380
70 98 Pitesti 98
146 85 Hirsova
Mehadia 101 Urziceni Rimnicu Vilcea 193
75 138 86 Sibiu 253
Bucharest Timisoara 329
Dobreta
120
90 Urziceni 80
Craiova Eforie Vaslui 199
Giurgiu Zerind 374
Chapter 3 42
Uniform-cost search
Expand least-cost (path cost) unexpanded node
Implementation:
QueueingFn = insert in order of increasing path cost
Chapter 3 43
Uniform-cost search
Arad
Chapter 3 44
Uniform-cost search
Arad
75 140 118
Chapter 3 45
Uniform-cost search
Arad
75 140 118
75 71
Arad Oradea
Chapter 3 46
Uniform-cost search
Arad
75 140 118
75 71 118 111
Chapter 3 47
Properties of uniform-cost search
Complete??
Time??
Space??
Optimal??
Note: What would happen if some paths had negative costs?
Chapter 3 48
Properties of uniform-cost search
Complete?? Yes, if step cost ≥ ǫ (nondecreasing)
Time??
Space??
Optimal??
Note: What would happen if some paths had negative costs?
Chapter 3 49
Properties of uniform-cost search
Complete?? Yes, if step cost ≥ ǫ
Time?? # of nodes with g ≤ cost of optimal solution
Space??
Optimal??
⌈C ∗/ǫ⌉
If each step costs at least ǫ > 0, then time complexity is O(b ), if the
optimum solution has cost C ∗
Why?
Chapter 3 50
Properties of uniform-cost search
Complete?? Yes, if step cost ≥ ǫ
Time?? # of nodes with g ≤ cost of optimal solution
Space??
Optimal??
⌈C ∗/ǫ⌉
If each step costs at least ǫ > 0, then time complexity is O(b ), if the
optimum solution has cost C ∗
Why? since the optimum solution would be at a maximum depth of ⌈C ∗/ǫ⌉).
Chapter 3 51
Properties of uniform-cost search
Complete?? Yes, if step cost ≥ ǫ
Time?? # of nodes with g ≤ cost of optimal solution
Space?? # of nodes with g ≤ cost of optimal solution
Optimal??
Chapter 3 52
Properties of uniform-cost search
Complete?? Yes, if step cost ≥ ǫ
Time?? # of nodes with g ≤ cost of optimal solution
Space?? # of nodes with g ≤ cost of optimal solution
Optimal?? Yes
Optimality is provided only if we use the algorithm given in Fig. 3.14, which
is modified from the basic GRAPH search. Otherwise, it is NOT guaranteed
to be optimal.
Chapter 3 53
Chapter 3 54
BFS versus uniform-cost search
Uniform cost search becomes Breadth-first search when the path cost func-
tion g(n) is DEPTH(n)
Equivalently, if all the step costs are equal.
Chapter 3 55
Depth-first search
Expand deepest unexpanded node
Implementation:
QueueingFn = last in first out (LIFO)
Arad
Chapter 3 56
Depth-first search
Arad
Chapter 3 57
Depth-first search
Arad
Arad Oradea
Chapter 3 58
Depth-first search
Arad
Arad Oradea
Chapter 3 59
Properties of depth-first search
Complete??
Time??
Space??
Optimal??
Chapter 3 60
Properties of depth-first search
Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time??
Space??
Optimal??
Chapter 3 61
Properties of depth-first search
Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space??
Optimal??
Notice here that you can find the big-Oh answer by considering the number
of nodes in the last level that needs to be considered.
Chapter 3 62
Properties of depth-first search
Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space?? O(bm), i.e., linear space!
Optimal??
Why? Calculate the size of the Queue assuming that the left-most branch
has the maximum depth, m. Now reason that the Queue will never get
bigger, wherever the solution may be.
Chapter 3 63
Properties of depth-first search
Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space?? O(bm), i.e., linear space!
Optimal?? No
Chapter 3 64
BFS vs DFS
Notice that if the problem does not have the issue of very long (possibly
infinite) paths, DFS is very advantageous! It has very small memory require-
ments and it is very easy to program.
Chapter 3 65
Depth-limited search
= depth-first search with depth limit l:
nodes at depth l are treated as if they have no successors
E.g. when we know that there are 20 cities on the map of Romania, there is
no need to look beyond depth 19. Compare with the diameter of a problem.
Implementation:
Nodes at depth l have no successors
Chapter 3 66
Depth-limited search - properties
Similar to DFS.
Complete?? yes, if l ≥ d
Time?? O(bl )
Space?? O(bl)
Optimal?? No
Code used:
Chapter 3 67
Chapter 3 68
Iterative deepening search
Can we do away with trying to estimate the limit?
Chapter 3 69
Iterative deepening search
Chapter 3 70
Iterative deepening search l = 0
Arad
Chapter 3 71
Iterative deepening search l = 1
Arad
Chapter 3 72
Iterative deepening search l = 1
Arad
Chapter 3 73
Iterative deepening search l = 2
Arad
Chapter 3 74
Iterative deepening search l = 2
Arad
Chapter 3 75
Iterative deepening search l = 2
Arad
Arad Oradea
Chapter 3 76
Iterative deepening search l = 2
Arad
Rimnicu
Arad Oradea Arad Oradea Fagaras Vilcea
Chapter 3 77
Iterative deepening search l = 2
Arad
Rimnicu
Arad Oradea Arad Oradea Fagaras Vilcea Arad Lugoj
Chapter 3 78
Iterative deepening search
Chapter 3 79
A A
Limit = 0
A A A A
Limit = 1
B C B C B C B C
A A A A
Limit = 2
B C B C B C B C
D E F G D E F G D E F G D E F G
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
A A A A
Limit = 3
B C B C B C B C
D E F G D E F G D E F G D E F G
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
A A A A
B C B C B C B C
D E F G D E F G D E F G D E F G
Chapter 3 80
H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O
Properties of iterative deepening search
Complete??
Time??
Space??
Optimal??
Chapter 3 81
Properties of iterative deepening search
Complete?? Yes
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space?? O(bd)
Optimal?? Yes, if step cost = 1
Can be modified to explore uniform-cost tree
Chapter 3 82
Properties of iterative deepening search
The higher the branching factor, the lower the overhead of repeatedly ex-
panded states (number of leaves dominate).
Number of generated nodes for b = 10 and d = 5 :
N (IDS) = 50 + 400 + 3, 000 + 20, 000 + 100, 000 = 123, 450
N (BFS) = 10 + 100 + 1, 000 + 10, 000 + 100, 000 + 999, 990 = 1, 111, 100
Preferred method when there is a large search space and the depth of the
solution is not known.
Chapter 3 83
Bidirectional search
Simultaneously search both forward from the initial state and backward from
the goal state.
Start Goal
Chapter 3 84
Bidirectional search
Need to define predecessors
Operators may not be reversible
What if there are many goal states?
Chapter 3 85
Bidirectional search
Time?
Space?
Chapter 3 86
Bidirectional search
Time? O(bd/2)
Space? O(bd/2)
For b=10, d = 6, BFS vs. BDS: million vs 2222 nodes .
Chapter 3 87
Summary of algorithms
Note that conditional Y es∗ and No are not that different: they both do not
guarantee completeness, only differ in the strength of the assumptions (b is
finite or the max. depth is finite etc.)
Chapter 3 88
Repeated states
Failure to detect repeated states can turn a linear problem into an exponential
one, even for non-looping problems!
A A
B B B
A
C C C C C
Chapter 3 89
Graph search
Chapter 3 90
Graph search
Problems with Graph Search:
♦ Memory Requirements: Increased space requirements for Depth-First
search (keep track of states to check for repetition!)
♦ Optimality: Basic Graph search deletes the later found path to a repeated
state, which could be the path with a shorter cost according to the chosen
search strategy (e.g. in iterative deepening, unless modifications are made).
This was the reason why graph search was modified to guarantee optimality
of Uniform Cost in graphs (where a previously found node is replaced with
a smaller cost one), in Fig. 3.14
Chapter 3 91
Summary
Problem formulation usually requires abstracting away real-world details to
define a state space that can feasibly be explored
Variety of uninformed search strategies
Iterative deepening search uses only linear space
and not much more time than other uninformed algorithms
Chapter 3 92