Lecture 3 Problems Solving by Searching
Lecture 3 Problems Solving by Searching
Lecture 3 Problems Solving by Searching
Lecture-3
Formulate goal:
• be in Bucharest
Formulate problem:
• states: various cities
• actions: drive between cities
Find solution:
• sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest
Example: Romania
Problem types
• Deterministic, fully observable single-state problem
– Agent knows exactly which state it will be in; solution is a
sequence
• Non-observable sensorless problem (conformant
problem)
– Agent may have no idea where it is; solution is a sequence
• Nondeterministic and/or partially observable
contingency problem
– percepts provide new information about current state
– solution is a contingent plan or a policy
– often interleave search, execution
– Unknown state space exploration problem (“online”)
Example: vacuum world
• Single-state, start in #5. Solution??
[Right; Suck]
• Conformant, start in {1; 2; 3; 4; 5; 6; 7; 8}
e.g., Right goes to {2; 4; 6; 8}. Solution??
[Right; Suck; Left; Suck]
• Contingency, start in #5
Murphy's Law: Suck can dirty a clean carpet
Local sensing: dirt, location only. Solution??
[Right; if dirt then Suck]
Single-state problem formulation
• A problem is defined by four items:
initial state e.g., “at Arad"
successor function S(x) = set of action - state pairs
• e.g., S(Arad) = {<Arad Zerind; Zerind>,…}
goal state, can be
• explicit, e.g., x = “at Bucharest"
• implicit, e.g., NoDirt(x)
path cost (additive)
• e.g., sum of distances, number of actions executed, etc.
• c(x; a; y) is the step cost, assumed to be 0
A solution is a sequence of actions
• leading from the initial state to a goal state
Selecting a state space
• Real world is absolutely complex
state space must be abstracted for problem solving
(Abstract) state = set of real states
• (Abstract) action = complex combination of real actions
e.g., “Arad Zerind” represents a complex set of
possible routes, detours, rest stops, etc.
• For guaranteed reliability, any real state "in Arad“ must
get to some real state "in Zerind"
• (Abstract) solution =
set of real paths that are solutions in the real world
• Each abstract action should be “easier” than the
original problem
Vacuum world state space graph
1 2
3 4
5 6
8
7
A small portion of the state space of 8-puzzle is shown below. Note that
we do not need to generate all the states before the search begins. The
states can be generated when required.
Figure 8.2 Breadth-First Search of the(c)Eight-Puzzle
2000-2002 SNU CSE Biointelligence
21
8-queens problem
Place eight queens on a chessboard such that no
queen attacks any other.
Transition model: Returns the board with a queen added to the specified
square.
Goal test: 8 queens are on the board, none attacked.
• The EXPAND function creates new nodes, filling in the various fields &
using the SUCCESSORFN of the problem to create the corresponding
states.
Implementation: general tree search
Search strategies
• A search 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--number of nodes generated/expanded
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
• d--depth of the least-cost solution
• m--maximum depth of the state space (may be ∞)
State space Problem formulation
Goal based agent searching Algorithm
Searching Algorithm
Example:
Breadth First Search(BFS),
Depth First Search(DFS),
Depth Limited Search (DLS)
Iterative Deepening Search(IDS)
Breadth-first search
Breadth-first search is a simple strategy in which the root node is expanded first,
then all the successors of the root node are expanded next, then their successors,
and so on.
In general,all the nodes are expanded at a given depth in the search tree before
any nodes at the next level are expanded.
Breadth-first search
Step 1
• Expand shallowest
unexpanded node
• Implementation:
• fringe is a FIFO queue,
i.e., new successors go at
end
35
How to do breadth-first
searching
Put the root node on a queue;
while (queue is not empty) {
remove a node from the queue;
if (node is a goal node) return success;
put all children of node onto the queue;
}
return failure;
Just before starting to explore level n, the queue holds
all the nodes at level n-1
In a typical tree, the number of nodes at each level
increases exponentially with the depth
Memory requirements may be infeasible
36
Breadth First Search
Home Work for BFS algorithm
Properties of breadth-first search
• Complete?? Yes (if b is infinite)
• Time? 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1) i.e., exp. in d
• Space? O(bd+1) (keeps every node in memory)
• Optimal? Yes (if cost = 1 per step); not optimal in
general
Disadvantages of BFS:
1. All nodes are to be generated at any level. So even unwanted nodes are
to be remembered. Memory wastage.
2. Time and space complexity is exponential type- Hurdle
Uniform-cost search
• Expand least-cost unexpanded node
• Implementation:
• fringe = queue ordered by path cost, lowest first
Equivalent to breadth-first if step costs all equal
• Complete? Yes, if step cost ≥ ε
• Time? # of nodes with g ≤ cost of optimal solution,
O(bceiling(C*/ ε)) where C* is the cost of the optimal solution
• Space? # of nodes with g ≤ cost of optimal solution,
O(bceiling(C*/ ε))
• Optimal? Yes – nodes expanded in increasing order of g(n)
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO stack, i.e., put successors at
front
Depth-first search
DFS Example
A depth-first search (DFS)
A explores a path all the way to a
leaf before backtracking and
B C
exploring another path
For example, after searching A,
then B, then D, the search
D E F G
backtracks and tries another
path from B
H I J K Node are explored in the order
ABDEHLMNIOPCFG
L M N O P Q JKQ
N will be found before J
52
How to do depth-first searching
Put the root node on a stack;
while (stack is not empty) {
remove a node from the stack;
if (node is a goal node) return success;
put all children of node onto the stack;
}
return failure;
At each step, the stack contains some nodes from each
of a number of levels
The size of stack that is required depends on the
branching factor b
While searching level n, the stack contains approximately
b*n nodes
When this method succeeds, it doesn’t give the path
53
Depth-first
Home Work for DFS algorithm
Advantages of DFS:
1. Memory requirements in DFS are less compared to BFS
as only nodes on the current path are stored.
2. DFS may find a solution without examining much of the
search space of all.
Disadvantages of DFS:
1. but it is not guaranteed to find a solution even
where one is guaranteed.
This search can go on deeper and deeper into the search
space and thus can get lost. This is referred to as blind
alley.!
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
Pros of DFS
• DFS requires less memory since only the nodes on the current
path are stored
• This contrast with BFS, where all of the tree that has so far been
generated must be stored
• By chance, DFS may find a solution without examine much of
the search space at all
• Contrast with BFS, in which all parts of the tree must be
examined to level n before any nodes on level n+1 can be
examined.
• This is particularly significant if many acceptable solutions exist,
• DFS can stop when one of them is found
Comparative analysis (b = 3,d = 2)
FIFO
LIFO
67
Iterative deepening search