Problem Solving Using Search Techniques
Problem Solving Using Search Techniques
Prepared by
Dr.R.Sivakami
ASP/ CSE, SONA
Unit 2 :Course Topics
Key concepts in search
• Set of states that we can be in
– Including an initial state…
– … and goal states (equivalently, a goal test)
• For every state, a set of actions that we can take
– Each action results in a new state
– Typically defined by successor function
• Given a state, produces all states that can be reached from it
• Cost function that determines the cost of each
action (or path = sequence of actions)
• Solution: path from initial state to a goal state
– Optimal solution: solution with minimal cost
Searching for Solutions
A solution is an action sequence and search algorithms considers various
possible action sequences.
The possible action sequences starting at the initial state form a search tree
with the initial state at the root;
The branches are actions and the nodes correspond to states in the state
space of the problem.
Expanding the current state is application of each legal action to the
current state and generation of a new set of states.
The current state is the parent node, newly generated states are child
nodes
Leaf node is a node with no children in the tree.
The set of all leaf nodes available for expansion at any given point is
called the frontier.
Search algorithms all share this basic structure; they vary primarily
according to how they choose which state to expand next: search strategy.
Partial Search Trees for Travelling in Romania
Initial State
Partial Search Trees for Travelling in Romania
After expanding Arad
Partial Search Trees for Travelling in Romania
After expanding Sibiu
Generic search algorithm
Fringe = set of nodes generated but not expanded
All nodes are expanded at a given depth in search tree before any
nodes at next level are expanded.
nodes traversed in BFS until the shallowest Node. Where the d= depth of shallowest
Space Complexity: Space complexity of BFS algorithm is given by the Memory size of
Completeness: BFS is complete, which means if the shallowest goal node is at some
Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the
node.
1.Breadth-first search
Example
1.Breadth-first search
Example
1.Breadth-first search
Example
1.Breadth-first search
Example
1.Breadth-first search
Properties of Breadth-first search
Time Complexity: Time Complexity of BFS algorithm can be obtained by the number of nodes
traversed in BFS until the shallowest Node. Where the d= depth of shallowest solution and b is a node
at every state.
Space Complexity: Space complexity of BFS algorithm is given by the Memory size of frontier which is
O(bd).
Completeness: BFS is complete, which means if the shallowest goal node is at some finite depth, then
Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the node.
Breadth-first search
2.Uniform-Cost Search
Uniform-cost search is a searching algorithm used for traversing a weighted tree or
graph.
This algorithm comes into play when a different cost is available for each edge. The
primary goal of the uniform-cost search is to find a path to the goal node which has the
lowest cumulative cost.
Uniform-cost search expands nodes according to their path costs form the root node.
It can be used to solve any graph/tree where the optimal cost is in demand.
A uniform-cost search algorithm is implemented by the priority queue
It gives maximum priority to the lowest cumulative cost. Uniform cost search is
equivalent to BFS algorithm if the path cost of all edges is the same.
Advantages:
• Uniform cost search is optimal because at every state the path with
the least cost is chosen.
Disadvantages:
• It does not care about the number of steps involve in searching and
only concerned about path cost. Due to which this algorithm may be
stuck in an infinite loop.
2.Uniform-Cost Search
When all step costs are equal, breadth-first search is optimal because it
– By a simple extension, we can find an algorithm that is optimal with any step-
cost function.
Uniform-cost search expands node n with the lowest path cost g(n).
1'st Iteration-----> A
2'nd Iteration----> A, B, C
3'rd Iteration------>A, B, D, E, C, F, G
4'th Iteration------>A, B, D, H, I, E, C, F, K,
G
In the fourth iteration, the algorithm will
find the goal node.
5.Iterative Deepening Depth-First Search
5.Iterative Deepening Depth-First Search
5.Iterative Deepening Depth-First Search
5.Iterative Deepening Depth-First Search
5.Iterative Deepening Depth-First Search
5.Iterative Deepening Depth-First Search
Completeness:
• This algorithm is complete is if the branching factor is finite.
Time Complexity:
• Let's suppose b is the branching factor and depth is d then
the worst-case time complexity is O(bd).
Space Complexity:
• The space complexity of IDDFS will be O(bd).
Optimal:
• IDDFS algorithm is optimal if path cost is a non- decreasing
function of the depth of the node.
6.Bidirectional Search Algorithm
Bidirectional search algorithm runs two simultaneous searches, one
form initial state called as forward-search and other from goal node
called as backward-search, to find the goal node.
Bidirectional search replaces one single search graph with two small
sub graphs in which one starts the search from an initial vertex and
other starts from goal vertex.
The search stops when these two graphs intersect each other.
Bidirectional search can use search techniques such as BFS, DFS, DLS,
etc.
Advantages:
• Bidirectional search is fast.
• Bidirectional search requires less memory
Disadvantages:
• Implementation of the bidirectional search tree is difficult.
• In bidirectional search, one should know the goal state in advance.
6.Bidirectional search
• Even better: search from both the start and the
goal, in parallel!
Do not generate any repeated states (need to store and check a potentially
large number of states)
This is done by keeping a list of "expanded states" i.e., states whose daughters
have already been put on the enqueued list. This entails removing states from
the "enqueued list" and placing them on an "expanded list"
(In the standard algorithm literature, the list of expanded states is called the
"closed list ", thus, we would move states from the open list to the closed list)
Informed search algorithms
Informed search algorithm contains an array of knowledge such as how
far we are from the goal, path cost, how to reach to goal node, etc. This
knowledge help agents to explore less to the search space and find more
The informed search algorithm is more useful for large search space.
Heuristic search.
Types
Greedy Search
A* Search
Heuristics function
Heuristic is a function which is used in Informed Search, and it finds the most
promising path.
It takes the current state of the agent as its input and produces the estimation of how
The heuristic method, however, might not always give the best solution, but it
Heuristic function estimates how close a state is to the goal. It is represented by h(n),
and it calculates the cost of an optimal path between the pair of states.
nodes based on their heuristic value h(n). It maintains two lists, OPEN and CLOSED list.
In the CLOSED list, it places those nodes which have already expanded and in the
OPEN list, it places nodes which have yet not been expanded.
On each iteration, each node n with the lowest heuristic value is expanded and
generates all its successors and n is placed to the closed list. The algorithm continues
In the informed search we will discuss two main algorithms which are given below:
2. A* Search Algorithm
1.Best-first Search (Greedy Search)
Greedy best-first search algorithm always selects the path which
appears best at that moment.
It is the combination of depth-first search and breadth-first search
algorithms. It uses the heuristic function and search.
Best-first search allows us to take the advantages of both algorithms.
With the help of best-first search, at each step, we can choose the
most promising node.
In the best first search algorithm, we expand the node which is
closest to the goal node and the closest cost is estimated by heuristic
function, i.e.
f(n)= g(n).
where, h(n)= estimated cost from node n to the goal.
The greedy best first algorithm is implemented by the priority queue.
1.Best-first Search (Greedy Search)
Step 1: Place the starting node into the OPEN list.
Step 2: If the OPEN list is empty, Stop and return failure.
Step 3: Remove the node n, from the OPEN list which has the lowest value of h(n), and
places it in the CLOSED list.
Step 4: Expand the node n, and generate the successors of node n.
Step 5: Check each successor of node n, and find whether any node is a goal node or
not. If any successor node is goal node, then return success and terminate the
search, else proceed to Step 6.
Step 6: For each successor node, algorithm checks for evaluation function f(n), and
then check if the node has been in either OPEN or CLOSED list. If the node has not
been in both list, then add it to the OPEN list.
Step 7: Return to Step 2.
Advantages:
• Best first search can switch between BFS and DFS by gaining the advantages of both the
algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
• It can behave as an unguided depth-first search in the worst case scenario.
• It can get stuck in a loop as DFS.
1.Best-first Search (Greedy Search) -Example
Consider the below search problem, and we will traverse it using greedy best-first
search. At each iteration, each node is expanded using evaluation function
f(n)=h(n) , which is given in the below table
1.Best-first Search (Greedy Search) -Example
• In this search example, we are using two lists which
are OPEN and CLOSED Lists. Following are the iteration for traversing the
above example.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure
and stops.
Step 3: Select the node from the OPEN list which has the smallest value of
evaluation function (g+h), if node n is goal node then return success and stop,
otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed
list. For each successor n', check whether n' is already in the OPEN or CLOSED
list, if not then compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to
the back pointer which reflects the lowest g(n') value.