Unit II
Unit II
UNIT II
PREPARED BY
VARSHA HIMTHANI
ASSISTANT PROESSOR
JECRC UNIVERSITY
SEARCH ALGORITHMS
• In Artificial Intelligence, Search techniques are universal problem-solving
methods.
• Optimality
• Completeness
VARSHA HIMTHANI
JECRC UNIVERSITY
TYPES OF SEARCH ALGORITHM
Uninformed Search
• Uninformed search is also called Blind search.
• It examines each node of the tree until it achieves the goal node.
Informed Search
• Informed search is also called a Heuristic search.
• Informed search strategies can find a solution more efficiently than an uninformed
search strategy.
VARSHA HIMTHANI
JECRC UNIVERSITY
SEARCH ALGORITHMS
VARSHA HIMTHANI
JECRC UNIVERSITY
BREADTH FIRST SEARCH
• BFS starts at the tree root (or some arbitrary node of a graph, sometimes
referred to as a ‘search key’), and explores all of the neighbor nodes at the
present depth prior to moving on to the nodes at the next depth level.
VARSHA HIMTHANI
JECRC UNIVERSITY
BREADTH FIRST SEARCH
Time complexity: Equivalent to the number of nodes traversed in BFS until the
shallowest solution. [O(bd)]
Space complexity: Equivalent to how large can the fringe get.
Completeness: BFS is complete, meaning for a given search tree, BFS will come
up with a solution if it exists.
Optimality: BFS is optimal as long as the costs of all edges are equal.
VARSHA HIMTHANI
JECRC UNIVERSITY
DEPTH FIRST SEARCH
The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each
branch before backtracking.
Path: S→A→B→C→G
VARSHA HIMTHANI
JECRC UNIVERSITY
DEPTH FIRST SEARCH
• Time complexity: Equivalent to the number of nodes traversed in
DFS. [O(nm)]
• Space complexity: DFS algorithm needs to store only single path from
the root node. [O(bm)]
VARSHA HIMTHANI
JECRC UNIVERSITY
DEPTH LIMITED SEARCH
The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each
branch before backtracking.
Depth limit = 2
VARSHA HIMTHANI
JECRC UNIVERSITY
DEPTH LIMITED SEARCH
• Time and space complexity: Same as DFS.
VARSHA HIMTHANI
JECRC UNIVERSITY
ITERATIVE DEEPENING SEARCH ALGORITHM
• Also called Iterative Deepening Depth First Search (IDDFS)
• IDDFS calls DFS for different depths starting from an initial value. In every
call, DFS is restricted from going beyond given depth. So basically we do
DFS in a BFS fashion.
VARSHA HIMTHANI
JECRC UNIVERSITY
Disadvantage of DFS
DFS first traverses nodes going through one adjacent of root, then next adjacent.
The problem with this approach is, if there is a node close to root, but not in first
few subtrees explored by DFS, then DFS reaches that node very late.
VARSHA HIMTHANI
JECRC UNIVERSITY
Disadvantage of BFS
BFS goes level by level, but requires more space. The space required by DFS
is O(d) where d is depth of tree, but space required by BFS is O(n) where n is
number of nodes in tree.
VARSHA HIMTHANI
JECRC UNIVERSITY
ITERATIVE DEEPENING SEARCH ALGORITHM
VARSHA HIMTHANI
JECRC UNIVERSITY
ITERATIVE DEEPENING SEARCH ALGORITHM
• The last (or max depth) level is visited once, second last level is visited twice,
and so on.
• It may seem expensive, but it turns out to be not so costly, since in a tree
most of the nodes are in the bottom level.
• So it does not matter much if the upper levels are visited multiple times.
VARSHA HIMTHANI
JECRC UNIVERSITY
INFORMED SEARCH: HEURISTIC
• Heuristic (from Greek "I find, discover") is a technique designed for solving a
problem more quickly when classic methods are too slow, or for finding an
approximate solution when classic methods fail to find any exact solution.
VARSHA HIMTHANI
JECRC UNIVERSITY
HEURISTIC FUNCTION
• It is represented by h(n), and it calculates the cost of an optimal path between the
pair of states.
Types
VARSHA HIMTHANI
JECRC UNIVERSITY
HEURISTIC FUNCTION: EXAMPLE
H(n)=2
X
Source
There are many methods used to
1
H(n)=1
calculate heuristic function like
Y Euclidean distance, Manhattan
1 distance, Number of misplaced tiles
etc.
Z
Goal
BEST FIRST SEARCH
• Best first search uses the concept of a priority queue and heuristic
search.
• Both BFS and DFS blindly explore paths without considering any cost
function.
• The idea of Best First Search is to use an evaluation function to decide
which adjacent is most promising and then explore.
• We use a priority queue to store costs of nodes. So the implementation is a
variation of BFS, we just need to change Queue to Priority Queue.
VARSHA HIMTHANI
JECRC UNIVERSITY
BEST FIRST SEARCH: EXAMPLE
Path: S- B- F- G
VARSHA HIMTHANI
JECRC UNIVERSITY
BEST FIRST SEARCH
Time complexity: Equivalent to the number of nodes traversed in BFS until the
shallowest solution. [Worst case O(log n)]
Space complexity: Equivalent to how large can the fringe get.
Completeness: BFS is complete, meaning for a given search tree, BFS will
come up with a solution if it exists.
Optimality: BFS is optimal. Performance of the algorithm depends on how well
the cost or evaluation function is designed.
VARSHA HIMTHANI
JECRC UNIVERSITY
HILL CLIMBING ALGORITHM
•Hill climbing algorithm is a local search algorithm which continuously moves in
the direction of increasing elevation/value to find the peak of the mountain or
best solution to the problem.
•It terminates when it reaches a peak value where no neighbor has a higher
value.
•Hill climbing algorithm is a technique which is used for optimizing the
mathematical problems.
•One of the widely discussed examples of Hill climbing algorithm is Traveling-
salesman Problem in which we need to minimize the distance traveled by the
salesman.
VARSHA HIMTHANI
JECRC UNIVERSITY
HILL CLIMBING ALGORITHM
•Greedy approach: It is also called greedy local search as it only looks to its
good immediate neighbor state and not beyond that.
•A node of hill climbing algorithm has two components which are state and
value.
•No backtracking: It does not backtrack the search space, as it does not
remember the previous states.
•In this algorithm, we don't need to maintain and handle the search tree or
graph as it only keeps a single current state.
VARSHA HIMTHANI
JECRC UNIVERSITY
HILL CLIMBING ALGORITHM
VARSHA HIMTHANI
JECRC UNIVERSITY
TYPES OF HILL CLIMBING ALGORITHM
• Steepest-Ascent hill-climbing:
VARSHA HIMTHANI
JECRC UNIVERSITY
SIMPLE HILL CLIMBING
VARSHA HIMTHANI
JECRC UNIVERSITY
STEEPEST-ASCENT HILL CLIMBING
VARSHA HIMTHANI
JECRC UNIVERSITY
STOCHASTIC HILL CLIMBING
Stochastic hill climbing does not examine for all its neighbor before moving.
Rather, this search algorithm selects one neighbor node at random and
decides whether to choose it as a current state or examine another state.
VARSHA HIMTHANI
JECRC UNIVERSITY
PROBLEMS IN HILL CLIMBING ALGORITHM
• 1. Local Maximum: A local maximum is a peak state in the landscape which
is better than each of its neighboring states, but there is another state also
present which is higher than the local maximum.
VARSHA HIMTHANI
JECRC UNIVERSITY
PROBLEMS IN HILL CLIMBING ALGORITHM:
• Plateau: A plateau is the flat area of the search space in which all the
neighbor states of the current state contains the same value, because of this
algorithm does not find any best direction to move. A hill-climbing search
might be lost in the plateau area.
VARSHA HIMTHANI
JECRC UNIVERSITY
PROBLEMS IN HILL CLIMBING ALGORITHM
• Ridges: A ridge is a special form of the local maximum. It has an area which
is higher than its surrounding areas, but itself has a slope, and cannot be
reached in a single move.
VARSHA HIMTHANI
JECRC UNIVERSITY
A* SEARCH ALGORITHM
• A * algorithm is a searching algorithm that searches for the shortest path
between the initial and the final state.
• A* Search algorithm is one of the best and popular technique used in path-
finding and graph traversals.
• Many games and web-based maps use this algorithm to find the shortest
path very efficiently (approximation).
VARSHA HIMTHANI
JECRC UNIVERSITY
A* SEARCH ALGORITHM
• A* Search Algorithm does is that at each step it picks the node according to a
value-‘f’ which is a parameter equal to the sum of two other parameters –
• At each step it picks the node/cell having the lowest ‘f’, and process that
node/cell.
VARSHA HIMTHANI
JECRC UNIVERSITY
A* SEARCH ALGORITHM: EXAMPLE
In the given graph orange color numbers
on vertices shows the heuristic values
and the black color numbers on edges
shows the movement cost. In this, a is
source and z is goal node.
VARSHA HIMTHANI
JECRC UNIVERSITY
AO* SEARCH ALGORITHM
VARSHA HIMTHANI
JECRC UNIVERSITY
AO* SEARCH ALGORITHM
VARSHA HIMTHANI
JECRC UNIVERSITY
AO* SEARCH ALGORITHM
VARSHA
VARSHA HIMTHANI
HIMTHANI
JECRC
JECRC UNIVERSITY
UNIVERSITY
AO* SEARCH ALGORITHM: EXAMPLE
VARSHA HIMTHANI
JECRC UNIVERSITY
AO* SEARCH ALGORITHM: EXAMPLE
VARSHA HIMTHANI
JECRC UNIVERSITY
THANK
YOU