Unit 2 Search
Unit 2 Search
UNIT – 2
PREPAED BY
Dr N.Saranya
Unit 2 List of Topics
SE 420 5
GENERAL SEARCH ALGORITHM
• Searching for solutions
• Problem solving agents
• Control strategies
• Parameters for search evaluation
Searching for solutions
• State space-finding path
• State space-collection of all possible configurations
• [S,A,I,G]-state space search-process of starting the state space for
solution to reach the goal.
Algorithm
Problem solving agent
• Agent –formulate the goal as well the problem
• Entity that can perceive the environment and act on it.
Control strategies
• How to reach or what way to follow while searching for a solution
• The most common startegies
• Forward search
• Backward Search
• Systematic Search –when search space is small.
• depth and Breadth first search
• Its also called blind search
• Heuristic Search –based on previous experience
Evaluation of search strategies
• Search algorithms are commonly evaluated according to
the following four criteria:
• Completeness: does it always find a solution if one exists?
• Time complexity: how long does it take as a function of number
of nodes?
• Space complexity: how much memory does it require?
• Optimality: does it guarantee the least-cost solution?
SE 420 11
Last time: uninformed search strategies
Uninformed search:
Use only information available in the problem formulation
• Breadth-first
• Uniform-cost
• Depth-first
• Depth-limited
• Iterative deepening
SE 420 12
Breadth first search (BFS)
• It follows the shallow node approach
• Search technique where from the root node ,all successors are
searched across level
• Queue data structures is used to carry out the search
BFS ALGORITHM
Time complexity (b)=O(bd) and space complexity=O(bd)
Cost of every edge is same.b =branching tool
STEPS
Important Terms
1 2
3 4
Blind strategies BFS
Search Methods
BFS characteristics
Completeness: if the branching factor is ‘b’ and the goal node is
at depth d, BFS will eventually find it.
Optimality: BFS is optimal if path cost is a non-decreasing
function of depth.a
Time complexity:
1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1).
Space complexity: O(bd+1).b
a
Otherwise, the shallowest node may not necessarily be optimal.
b
b branching factor; d depth of the goal node
spring 2011
Uniform cost search
• Every edge cost associated with it.
• If all the edges do not have same cost,BFS generalizes
to uniform cost search.
• At each step ,next thing to be performed
expands/selects a node X ,whose cost c(x)is lowest,
• C(x)=sum of cost of edges from root to the node.
• Implemented using priority queue
• Mostly it explores large trees
Example-uniform cost search
Depth first search
• Single branch of the tree is considered at a time
• Backtracking
• Stack data structure is used .
• Goal state:To reach F
• Intial s,working is same as the way to reach to D.
• Next step needs backtracking and hence it starts with E.
• Path =A-E-F
• At any point of time needs to keep the path from the root to the
current node .O(bd)
Blind Search : Depth First Search (DFS)
Implementation:
fringe = LIFO queue, i.e., put successors at front.
1 2 3
4 5 N+1
…….
Search Methods
DFS characteristics
Small space requirements: only the path to the current node and
the siblings of each node in the path are stored.
Backtracking search generates only one successor for each
node.
Completeness: no, if the expanded subtree has an
infinite depth.
Optimality: no, if a solution located deeper, but located in a
subtree expanded earlier, is found.
Time complexity: O(b )m.
Space complexity: O(bm) (linear!).
spring 2011
BFS Vs DFS
ADVANTAGES OF DFS ADVANTAGES OF BFS
Simple to implement Guaranteed to find a solution(if one
exists)
Needs relatively small memory fo storing Depending on the problem ,can be
the state space guaranteed to find an optimal solution
Cannot find solution in all cases and hence Memory management along with
can sometimes fail to find a solution allocation is the key factor and hence
more complex to implement
Not guaranteed to find an optimal Needs a lot of memory for storing the
solution can take a lot of time to find a state space is the search space has a high
solution branching factor
Search Methods
In the above example , If we fix the depth limit to 2, DLS can be carried out
similarly to the DFS until the goal node is found to exist in the search domain
of the tree.
spring 2011
Algorithm
• There is no guarantee that search will give solution that will be
optimal, it finds the one which is within its limits
ITEREATIVE DEEPENING SEARCH
• Enhanced version of depth limited search .
• Iterative deepening helps in addressing such
problems.
• Extend the limit by limit=limit+1
Blind Search : Iterative Deepening DFS
(ID-DFS)
DEPTH
LIMITED
SEARCH
Blind Search : Iterative Deepening DFS
(ID-DFS)
Blind Search : Iterative Deepening DFS Search Methods
(ID-DFS)
IDS characteristics
Completeness: yes.
Optimality: yes, if step cost = 1.
Time complexity:
(d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd ).
Space complexity: O(bd).
Conclusion
IDS exhibits better performance, because it does not expand other
nodes at depth d.
APPLICATION: MAZE GAME
• BFS SOLUTION?
• S-1-2-3-5-8-10-12-14-16-19-G
• SEARCH SOLUTION FOUND IN 12
STEPS
DFS SOLUTION?
S-1-2-3-6-5-8-9-10-11-13-16-18-G
SEARCH SOLUTION FOUND IN 14 STEPS
APPLICATION: MAZE GAME
• BFS SOLUTION?
• S-1-2-3-5-8-10-12-14-16-19-G
• SEARCH SOLUTION FOUND IN 12 STEPS
5 15 19
S
6 7 8 17 18
1 G 21
10
9 20 21
2
12
3
11
19
x
x
14
5 4 13
16
15
19
APPLICATION: MAZE GAME
• BFS SOLUTION?
• S-1-2-3-5-8-10-12-14-16-19-G
• SEARCH SOLUTION FOUND IN 12 STEPS
5 15 19
S
6 7 8 17 18
1 G 21
10
9 20 21
2
12
3
11
19
x
x
14
5 4 13
16
15
19
Blind Search : Uniform Cost Search
Minimum is S->D->C->G2
And also G2 is one of the destination nodes thus we found our path.
In this way we can find the path with the minimum cumulative cost from a start
node to ending node – S->D->C->G2 with cost total cost as 13(marked with green
color).
Uniform Cost Search
Implementation: fringe =
queue ordered by path cost
Equivalent to breadth-first if
all step costs all equal.
Breadth-first is only optimal
if step costs is increasing
with depth.
(e.g. constant). Can we
guarantee optimality for
any step cost?
Uniform-cost Search:
Expand node with
smallest path cost g(n).
Blind strategies UCS
Search Methods
Search Methods
60
Summary of Blind Search Algorithms
Complete? Yes No
61
Summary of Blind Search Algorithms
63
Informed Search Algorithms
65
Generate-and-test
Example - Traveling Salesman Problem
(TSP)
• Traveler needs to visit n cities.
• Know the distance between each pair of cities.
• Want to know the shortest route that visits all the
cities once.
• n=80 will take millions of years to solve exhaustively!
66
Generate-and-test
TSP Example
A 6 B
1 2
5 3
D 4 C
67
Generate-and-test Example
D C D B B C
68
Best First Search Algorithms
• Implementation:
Order the nodes in fringe increasing order of cost.
• Special cases:
• greedy best-first search
• A* search
Romania with straight-line dist.
Greedy best-first search
• f(n) = estimate of cost from n to goal
• e.g., f(n) = straight-line distance from n to
Bucharest
• Greedy best-first search expands the node that
appears to be closest to goal.
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Properties of greedy best-first
search