Chapter 3
Chapter 3
searching
Chapter 3
Outline
• Problem-solving agents
• Problem types
• Problem formulation
• Example problems
• Basic search algorithms
2
Problem solving agent
State space forms a graph in which the nodes are states and the arcs between nodes are
actions. (It defines all the possible configurations of the relevant objects associated with
the problem.)
• Goal test :- It determines whether a given state is a goal state.
• Path cost :- A path in the state space is a sequence of states connected by a sequence of
actions. A path cost function assigns a numeric cost to each path. The problem-solving
agent chooses a cost function that reflects its own performance measure.
Path cost = ∑Step costs => Sum of the costs of the individual actions along the path.
(Step cost of taking action a to go from state x to state y is denoted by c(x, a, y) )
4
Problem solving…
• These four components that define a problem can be gathered together into a
single data structure that is given as input to a problem-solving algorithm.
6
Example: Romania
• On holiday in Romania; currently in Arad.
• Formulate goal:
• To be in Bucharest
• Formulate problem:
• states: various cities
• actions: drive between cities
• Find solution:
• sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest 7
• The initial sate for our agent romania is described as In(Arad).
• For example, from the state In(Arad) , the applicable actions are { Go(Sibiu), Go (Timisoara),
Go(Zerind)}.
• The transition model gives description of what each action does and is specified by a function
RESULT(s,a). It returns the state that results from doing action a in state s.
• The term successor is used to refer to any state reachable from a given state by single action.
For example, we have
RESULT(In(Arad), Go(Zerind))=In(Zerind)
• A solution to a problem is an action sequence that leads from the initial state to a goal state.
• Optimal solution has the lowest path cost among all the solutions.
Romania with step costs in km
Selecting a state space
• Real world is absurdly complex
state space must be abstracted for problem solving. The process of removing detail from a
presentation is called abstraction; for example the traveling companions, the current radio
program, the scenery out of the window, the proximity of law enforcement
officers, the distance to the next rest stop, the condition of the road, the weather, and so on. All
these considerations are left out of our state descriptions because they are irrelevant to the
problem of finding a route to Bucharest. T
• (Abstract) state = set of real states
• e.g., "Arad Zerind" represents a complex set of possible routes, detours, rest stops, etc.
• For guaranteed realizability, any real state "in Arad“ must get to some real state "in Zerind"
• (Abstract) solution =
• states?
• actions?
• goal test?
The state space for the vacuum world. Links denote actions: L = Left,R=
• path cost? Right,S= Suck.
12
Simple vacuum world problem
(State space graph)…
• states?
• actions?
• goal test?
• path cost?
14
The 8-puzzle problem…
• It consists of 3*3 board with eight numbered tiles and a blank space. The tile adjacent to the
• blank space can slide into the space. The object is to reach a specified goal state as shown in the
figure.
Consider the airline travel problem that must be solved • Transition model: The state resulting from taking a
by a travel-planning Web site: flight will have the flight’s destination as the current
• States: Each state obviously includes a location (e.g., location and the flight’s arrival time as the current time.
an airport) and the current time. Furthermore, because Transition model gives the description of what each
the cost of an action (a flight segment) may depend on action does.
previous segments, their fare bases, and their status as • Goal test: Are we at the final destination specified by
domestic or international, the state must record extra the user?
information about these “historical” aspects.
• Path cost: This depends on monetary cost, waiting
• Initial state: This is specified by the user’s query. time, flight time, customs and immigration procedures,
• Actions: Take any flight from the current location, in seat quality, time of day, type of airplane,
any seat class, leaving after the current time, leaving frequent-flyer mileage awards, and so on.
enough time for within-airport transfer if needed.
Some but not exhaustive list of Real world
problems
• Touring problems
• Traveling salesperson problem (TSP)
• VLSI layout problem
• Robot navigation
• Automatic assembly sequencing
• Protein design
Travelling salesman problem
• It is related to route finding problems in which each city must be visited
exacltly once while finding the shortest tour.
• It is a NP-hard problem.
VLSI layout problem
• This problem requires positioning of millions of components and connections
on a chip to minimize area, minimize circuit delays, minimize stray
capacitances, and maximize manufacturing yield.
• The aim is to place cells on the chip so that they do not overlap and so that
there is room for the connecting wires to be placed between the cells.
Robot navigation problem
• It is a route finding problem where the robot can move in a continous space with an infinite
set of possible actions and states.
• For a circular robot moving on a flat surface, the space is two dimensional.
• When the robot has arms and legs or wheels that must be controlled, the search space
becomes many-dimensional.
• The robots must deal with errors in their sensors and motor controls while addressing the
complexity of the problem.
Automatic assembly sequencing problem
• Here, the automatic assmbly sequencing of complex objects are carried out by robots.
• The aim is to find an order in which to assemble the parts of some object.
• If the wrong order is chosen, there will be no way to add some part later in the sequence
without undoing some of the work already done.
• In protein design problem the goal is to find a sequence of amino acids that wil fold
into a three-dimensional protein with the right properties to cure some disease.
Basic AI problem solving techniques
24
Informal Tree search algorithm
• Basic idea:
• offline, simulated exploration of state space by generating
successors of already-explored states (a.k.a.~expanding states)
25
Tree search example
26
Tree search example
27
Tree search example
28
Informal Graph search algorithms
Some examples of Graph search
State vs. node
• A state is a (representation of) a physical configuration
• A node is a data structure constituting part of a search tree includes
state, parent node, action, path cost g(x), depth
• The Expand function creates new nodes, filling in the various fields
and using the SuccessorFn of the problem to create the
corresponding states.
31
Node Details
Implementation: general tree search
33
Implementation of nodes in search algorithm
• d: the depth of the shallowest goal node (i.e., the number of steps along the path from the root)
36
• m: the maximum length of any path in the state space (may be ∞)
Evaluating Search strategies…
• Time is often measured in terms of the number of nodes generated during the
search, and space in terms of the maximum number of nodes stored in memory.
• For the most part, we describe time and space complexities for search on a tree; for
a graph, the answer depends on how “redundant” the paths in the state space are.
• To assess the effectiveness of a search algorithm, we can consider just the search
cost— which typically depends on the time complexity but can also include a term
for memory usage.
• Or we can use the total cost, which combines the search cost and the path cost of
the solution found (also sometimes called solution cost).
Search strategies
They are of 2 types:-
• Uninformed search / Blind search
Blind search has no additional information about the states
beyond that provided in the problem definition. All they can
do is generate successor and distinguish a goal state from a
non-goal state.
• Informed search / Heuristic search
Informed search identifies whether one non-goal state is
“more promising” than another one.
All search strategies are distinguished by the order in which nodes are expanded. 38
Uninformed search strategies
• Uninformed search strategies use only the information available in the
problem definition. Popular ones are:
• Breadth-first search (BFS)
• Uniform-cost search (UCS)
• Depth-first search (DFS)
• Depth-limited search (DLS)
• Iterative deepening search (IDS)
39
• Bidirectional search (BDS)
Breadth-first search
• It is a strategy in which the root node is expanded first, then all the successors
of the root node are expanded, then their successors. At a given depth in the
search tree, all the nodes are expanded before any node at the next level is
expanded.
• A FIFO queue is used i.e, new successors join the tail of the queue.
40
Breadth Fast Search Algorithm
Breadth-first search algorithm
42
Breadth-first search algorithm
B. For each rule ( from the rule set ) whose L.H.S matches with the
state described in V
Do
I. Apply the rule to generate a new state.
II. If the new state is a goal state then quit and return the state.
III. Otherwise add the new state to the end of the NODE-LIST.
43
Breadth-first search
44
Breadth-first search
• This algorithm comes into play when a different cost is available for each
edge.
• When all step costs are equal, breadth-first search is optimal because it always
expands the shallowest unexpanded node.
• By a simple extension, we can find an algorithm that is optimal with any
step-cost function. Instead of expanding the shallowest node, uniform-cost
search expands the node n with the lowest path cost g(n).
• This is done by storing the frontier as a priority queue ordered by g.
•
Uniform cost search (UCS)…
function UNIFORM-COST-SEARCH(problem) returns a solution, or failure
• frontier ←a priority queue ordered by PATH-COST, with node as the only element
• loop do
• Both of these modifications come into play in the next example where the
problem is to get from Sibiu to Bucharest in the Romania Touring problem.
Uniform cost search (UCS)…
Uniform cost search (UCS)…
• The successors of Sibiu are Rimnicu Vilcea and Fagaras, with costs 80 and 99, respectively.
• The least-cost node, Rimnicu Vilcea, is expanded next, adding Pitesti with cost 80 + 97=177.
• The least-cost node is now Fagaras, so it is expanded, adding Bucharest with cost 99+211=310.
• Now a goal node has been generated, but uniform-cost search keeps going, choosing Pitesti for
expansion and adding a second path to Bucharest with cost 80+97+101= 278.
• Now the algorithm checks to see if this new path is better than the old one; it is, so the old one is
discarded. Bucharest, now with g-cost 278, is selected for expansion and the solution is returned.
• It is easy to see that uniform-cost search is optimal in general. Uniform-cost search expands nodes in
order of their optimal path cost. (It is because step costs are nonnegative and paths never get shorter
as nodes are added.)
• Hence, the first goal node selected for expansion must be the optimal solution.
Uniform-cost search
• Expand least-cost unexpanded node
• Implementation:
• fringe = queue ordered by path cost
107
Depth-first search( DFS)
2. Until the stack is empty or the goal node is found out, repeat the
following:-
1. Remove the first element from the stack. If it is the goal node announce
success, return.
2. If not, add the first element’s children if any, to the top of the stack.
108
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
109
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
110
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at fron
111
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
112
Depth-first search
113
Depth-first search
114
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
115
Depth-first search
116
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
117
Depth-first search
118
Depth-first search
119
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
120
Properties of depth-first search
• Complete? No: fails in infinite-depth spaces, spaces with loops
• We may modify the algorithm to avoid repeated states along the path
DFS is complete in finite spaces.
• Time? The number of nodes generated is of the order of O(bm) . It is terrible if m is much
larger than d
• Space? It only stores the unexpanded nodes. So it requires 1 +b + b +b ….. m times = O(bm),
i.e., linear space!
• Optimal? No. If it makes a wrong choice, it may go down a very long path and finds a
solution, where as there may be a better solution at a higher level in the tree.
121
DFS Algorithm
● Note: it is possible that w2 was unvisited when we recursively visit w1, but became
visited by the time we return from the recursive call.
u
v w3
w1 w2
DFS Algorithm
8 1 F -
2 F -
source 2 3 F -
9
4 F -
1
5 F -
3 7 6 F -
6 7 F -
4
5 8 F -
9 F -
Pred
Initialize visited
table (all False)
Initialize Pred to -1
Example
Adjacency List Visited Table (T/F)
0 0 F -
1 F -
8 2 T -
3 F -
source 2 9 4 F -
1 5 F -
6 F -
3 7
7 F -
6
4 8 F -
5 9 F -
Pred
Mark 2 as visited
RDFS( 2 )
Now visit RDFS(8)
Example
Adjacency List Visited Table (T/F)
0
0 F -
8 1 F -
2 T -
source 2 9 3 F -
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 F -
Pred
Mark 8 as visited
Recursive
calls RDFS( 2 )
RDFS(8) mark Pred[8]
2 is already visited, so visit RDFS(0)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -
2 T -
source 2 9 3 F -
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 F -
Pred
Mark 0 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[0]
RDFS(0) -> no unvisited neighbors, return
to call RDFS(8)
Example
Back to 8
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -
2 T -
source 2 9 3 F -
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 F -
Pred
Recursive
calls RDFS( 2 )
RDFS(8)
Now visit 9 -> RDFS(9)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -
2 T -
source 2 9 3 F -
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Pred
Mark 9 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[9]
RDFS(9)
-> visit 1, RDFS(1)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 F -
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Pred
Mark 1 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[1]
RDFS(9)
RDFS(1)
visit RDFS(3)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 F -
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Pred
Mark 3 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[3]
RDFS(9)
RDFS(1)
RDFS(3)
visit RDFS(4)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 T 3
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Pred
RDFS( 2 ) Mark 4 as visited
Recursive RDFS(8)
calls RDFS(9) Mark Pred[4]
RDFS(1)
RDFS(3)
RDFS(4) STOP all of 4’s neighbors have been visited
return back to call RDFS(3)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 T 3
5 F -
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Back to 3
Pred
RDFS( 2 )
Recursive RDFS(8)
calls RDFS(9)
RDFS(1)
RDFS(3)
visit 5 -> RDFS(5)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 T 3
5 T 3
3 7 6 F -
6 7 F -
4
5 8 T 2
9 T 8
Pred
RDFS( 2 )
Recursive RDFS(8) Mark 5 as visited
calls RDFS(9)
Mark Pred[5]
RDFS(1)
RDFS(3)
RDFS(5)
3 is already visited, so visit 6 -> RDFS(6)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 T 3
5 T 3
3 7 6 T 5
6 7 F -
4
5 8 T 2
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) Mark 6 as visited
calls
RDFS(1) Mark Pred[6]
RDFS(3)
RDFS(5)
RDFS(6)
visit 7 -> RDFS(7)
Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9
2 T -
source 2 9 3 T 1
1 4 T 3
5 T 3
3 7 6 T 5
6 7 T 6
4
5 8 T 2
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) Mark 7 as visited
calls
RDFS(1) Mark Pred[7]
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) -> Stop no more unvisited neighbors
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6) -> Stop
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3)
RDFS(5) -> Stop
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3) -> Stop
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1) -> Stop
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) -> Stop
calls
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 )
RDFS(8) -> Stop
Recursive
calls
Example
Adjacency List Visited Table (T/F)
0 0 T 8
1 T 9
8
2 T -
3 T 1
source 2 9 4 T 3
1 5 T 3
3 7 6 T 5
7 T 6
6
4 8 T 2
5
9 T 8
Pred
RDFS( 2 ) -> Stop
Recursive
calls
Example
0 Adjacency List Visited Table (T/F)
8 0 T 8
1 T 9
source 2 2 T -
9
3 T 1
1
4 T 3
3 7 5 T 3
6 6 T 5
4
5 7 T 6
8 T 2
9 T 8
Pred
Check our paths, does DFS find valid paths? Yes.
• The problem with DFS is that the search can go down an infinite branch and
thus never return. Depth limited search avoids this problem by imposing a
depth limit l which effectively terminates the search at that depth. That is ,
nodes at depth l are treated as if they have no successors.
• The choice of depth parameter l is an important factor. If l is too deep , it is
wasteful in terms of both time and space. But if l < d (the depth at which
solution exists) i,e the shallowest goal is beyond the depth limit, then this
algorithm will never reach a goal state.
147
Depth-limited search
148
Properties of depth- limited search
• Complete? No. if l<d, the shallowest goal is beyond the depth limit and if l>d, then
depth-limited search is non-optimal.
• Time? O(b l ).
• Optimal? No.
149
Iterative deepening search (IDS)
• The problem with depth-limited search is deciding on a suitable
depth parameter, which is not always easy.
• The IDS is preferred when the search space is large and the depth
of the solution is not known.
150
Iterative deepening search
151
Iterative deepening search l =0
152
Iterative deepening search l =1
153
Iterative deepening search l =2
154
Iterative deepening search l =3
155
Iterative deepening search
156
Iterative deepening search
• For b = 10, d = 5,
• NBFS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
• NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456
• Overhead = (123,456 - 111,111)/111,111 = 11%
we can see that compared to the overall number of expansions , the total is not
substantially increased.
157
Properties of iterative deepening search
163
Bidirectional Search (BDS)
BDS (contd…)
•The time complexity of bidirectional search using breadth-first searches in both
directions is O(bd/2). The space complexity is also O(bd/2). This is because search
stops in the midway as soon as any one of the search paths from initial state
(forward search) meets with any one of the search paths from goal state
(backward search).
•For the similar condition, the time complexity of breadth first search from initial
state to goal state is O(bd). The space complexity is also O(bd). It is because
searching proceeds only in one direction i.e. from initial state to goal state (only
forward search).
• The motivation is that bd/2 + bd/2 is much less than bd, or in the figure, the area of
the two small circles is less than the area of one big circle centered on the start
and reaching to the goal.
Comparing Uninformed Search Strategies
Repeated states
• Failure to detect repeated states can turn a linear problem into an
exponential one!
171
Repeated states
172
Repeated states
173
Repeated states
3. Don’t generate any state that is the same as any state generated
before. This requires that every state is kept in memory. (space
complexity is O(bd ) )
174
Graph search
175
Summary
• Iterative deepening search uses only linear space and not much more time than
other uninformed algorithms
176
Informed Search Strategy
Informed Search Strategy
(Best-first search)
Heuristic search techniques
• Blind searches (uninformed searches)are normally
very inefficient. By adding domain knowledge we can
improve the search process.
• The idea behind the heuristic search is that we explore
the node that is most likely to be nearest to a goal
state.
• A heuristic function has some knowledge about the
problem so that it can judge how close the current
state is to the goal state.
• A heuristic function h(n) = estimated cost of the
cheapest path from the state at node n to a goal state.
Informed Search Strategy
(Heuristic functions)
• The choice of f determines the search strategies.
• Most best first algorithms include as a component of f a heuristic function,
denoted h(n)
• h(n) = estimated cost of the cheapest path from the state at node n to a goal
state.
• h(n) takes a node as input, unlike g(n) in UCS, it depends only on the state
at that node.
• For example, in Romania, one might estimate the cost of the cheapest path
from Arad to Bucharest via the straight-line distance from Arad to
Bucharest.
Heuristic functions (contd…)
• Heuristic functions are the most common form in which additional knowledge
of the problem is imparted to search algorithm.
• Implementation:
Order the nodes in fringe or sequence in decreasing order of desirability.
• Two special cases of Best-first search where h(n) is used as f(n) to guide search:
• Greedy best-first search [only h(n) is used as f(n)]
• A* search [h(n) + g(n) is used as f(n)]
Greedy best-first search
• CLOSED
• It contains nodes that have already been expanded. ( it is
used to ensure that the same node is not expanded more
than once.)
Greedy best-first search
• Complete? No – can get stuck in loops, e.g., Iasi Neamt Iasi Neamt
….
• Complete? Yes (unless there are infinitely many nodes with f ≤ f(G) )
• Time? Exponential
• Space? Keeps all nodes in memory
• Optimal? Yes if h(n) is admissible.
Admissible heuristics
• A heuristic h(n) is admissible if for every node n,
h(n) ≤ h*(n), where h*(n) is the true cost to reach the
goal state from n.
• A heuristic is consistent if for every node n, every successor n' of n generated by any action a,
h(n) ≤ c(n,a,n') + h(n')
• If h is consistent, we have
• h1(S) = ?
• h2(S) = ?
Admissible heuristics
• h1(S) = ? 8
• h2(S) = ? 3+1+2+2+2+3+3+2 = 18
8-puzzle heuristics: explanation
8-puzzle heuristics: explanation (contd…)