0% found this document useful (0 votes)
6 views49 pages

Module 2 - MCA Sem II - AIML - Search Strategies

Uploaded by

Find Device
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views49 pages

Module 2 - MCA Sem II - AIML - Search Strategies

Uploaded by

Find Device
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Module 2-

Search
Strategies
S O LV I N G P R O B L E M S BY S E A R C H I N G , S E A R C H - I S S U E S I N T H E D E S I G N O F
SEARCH PROGRAMS, UN-INFORMED SEARCH- BFS, DFS

I N F O R M E D S E A R C H ( H E U R I S T I C S E A R C H T E C H N I Q U E S ) - G E N E R AT E - A N D - T E S T,
H I L L C L I M B I N G , B E S T- F I R S T S E A R C H , A * A L G O R I T H M , A L P H A - B E TA S E A R C H
ALGORITHM,

P R O B L E M R E D U C T I O N , A O * A L G O R I T H M , C O N S T R A I N T S AT I S FA C T I O N , M E A N S -
E N D S A N A LY S I S
Solving problems by searching

Recall our previous discussion of reflex agents.


Such agents cannot operate well in environments for which the state to action mapping is too large to store or
would take too long to learn.
Problem-solving agents use atomic representations where states of the world are considered as wholes, with no
internal structure visible to the problem-solving agent.
We consider two general classes of search:
(1) uninformed search algorithms for which the algorithm is provided no information about the problem other
than its definition;
(2) informed search, where the algorithm is given some guidance.
The process of looking for a sequence of actions that reaches the goal is called search.
A search algorithm takes a problem as input and returns a solution in the form of an action sequence.
Well-defined problems and
solutions
A problem can be defined formally by (5) components:
(1) The initial state : from which the agent starts.
(2)Actions : A description of possible actions available to the agent: ACTIONS(s)
(3)Transitional Model : A description of what each action does, i.e. the transition model, specified by a function
RESULT
(4) The goal test, which determines whether a given state is a goal state.
(5) Path Cost : A path cost function that assigns a numeric cost to each path.
Problem formulation Example: Romania
1.Initial State : In(Arad)

2. Actions : In(Arad) , the applicable Actions are


{Go(Sibiu),Go(Timisoara),Go(Zerind)}

3. Transition Model :
RESULT(In(Arad),Go(Zerind)) = In(Zerind)

4. Goal Test : {In(Bucharest)}

5. Path Cost :
sequence of path with sum of all the paths from
source to destination

A solution is a sequence of actions from initial to


goal state.
Optimal solution has the lowest path cost.
Example: vacuum world

1.Initial state : In(A) Any state can be initial (A or B) State - 1 State - 2


2.Actions : {Go(Left), Go(Right), Go(Suck)}
3.Goal test : {In(Clean)} Check whether squares are clean. A B A B
4.Path cost : Number of actions to reach goal.

States : n*2^n two locations with or without dirt:


2 x 22=8 states.
Search strategies
A strategy is defined by picking the order of node
expansion.
Problem-solving performance is measured in four
ways:
◦ Completeness; Does it always find a solution if
one exists?
◦ Optimality; Does it always find the least-cost
solution?
◦ Time Complexity; Number of nodes
generated/expanded?
◦ Space Complexity; Number of nodes stored in
memory during search?

Time and space complexity are measured in terms of


problem difficulty defined by:
◦ b - maximum branching factor of the search tree
◦ d - depth of the shallowest goal node
◦ m - maximum depth of the state space (may be )
◦ search-cost and total-cost
Uninformed search strategies
Breadth-first search (BFS)
BFS 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, etc.
BFS is an instance of the general graph-search algorithm in which the shallowest unexpanded
node is chosen for expansion.
This is achieved by using a FIFO queue for the frontier. Accordingly, new nodes go to the back of
the queue, and old nodes, which are shallower than the new nodes are expanded first.
Expand shallowest unexpanded node
Implementation: frontier is a FIFO queue, i.e., new successors go at end
BF-search, an example
EXPAND SHALLOWEST UNEXPANDED NODE
IMPLEMENTATION: FRONTIER IS A FIFO QUEUE
BF-search-
evaluation
Completeness:
◦ YES (if b is finite)

Time complexity:
◦ Assume a state space where every state has b successors.
◦ root has b successors, each node at the next level has again b successors (total b2), …
◦ Assume solution is at depth d
◦ Worst case; expand all but the last node at depth d
◦ Total numb. of nodes generated:

Space complexity:
◦ if each node is retained in memory

Optimality:
◦ Does it always find the least-cost solution?
◦ In general YES
◦ unless actions have different cost.
Depth-first search (DFS)
DFS always expands the deepest node in the current frontier of the search tree. The search
proceeds immediately to the deepest level of the search tree, where the nodes have no
successors.
As those nodes are expended, they are dropped from the frontier, so then the search “backs up”
to the next deepest node that still has unexplored successors.
DFS is an instance of the general graph-search algorithm which uses a LIFO queue. This means
that the most recently generated node is chosen for expansion.
Expand deepest unexpanded node
Implementation:
◦ frontier = LIFO queue, i.e., put successors at front
DF-search, an example
Expand deepest unexpanded node
Implementation: fringe is a LIFO queue (=stack)
Expand deepest unexpanded node
Implementation: fringe is a LIFO queue (=stack)
DF-search- evaluation
Completeness;
◦ NO unless search space is finite.

Time complexity;
◦ Terrible if m is much larger than d (depth of optimal solution)
◦ But if many solutions, then faster than BF-search

Space complexity;
◦ Backtracking search uses even less memory
◦ One successor instead of all b.

Optimallity; No
◦ Same issues as completeness
◦ Assume node J and C contain goal states
Informed Search Algorithms or Heuristic Search
“Heuristics means norms, procedures or principles that helps in deciding which
amongst the several available alternative choice of action promises to be the most
effective mean to achieve some predetermined goal”.

Informed Search Algorithm uses Heuristic function to find the most appropriate route.

While doing so, it considers the present state of the agent as the starting point and
produces an approximation of gap between agent and the goal.

The Solution hence suggested by this method may or may not always be the best, but
it definitely tries to suggest good or we may say decent solution in comparatively much
reasonable time.

Heuristic function examines the gap between the state and the goal and the outcome is
denoted by h(n).

It computes the cost of an optimal path between the given pair of states.

The value of the heuristic function hence computed is always positive.


 The informed search strategies uses three main algorithms.

These are:
 Best First Search Algorithm (Greedy search)
 A* Search Algorithm
 Hill Climbing
BEST-FIRST SEARCH ALGORITHM
This is also known as GREEDY SEARCH.
This search algorithm picks such path that appears best at that moment.
This algorithm is a blend of both - depth-first search and breadth-first search algorithms and in
its operations, it hence take the benefits of both the algorithms.
This algorithm is preferred as it helps choose the most promising node at each step.
Here, the node which is closest to the goal node is expanded and the closest cost is estimated by
heuristic function,
i.e. f(n)= h(n).
Where, h(n) = estimated cost from node n to the goal.
The greedy best first algorithm is applied by the priority queue.

Steps followed in Best first search algorithm is stated as under:

Step 1: The process begins once the start node is placed into the OPEN list.

Step 2: In case the OPEN list is empty, Stop and return failure.

Step 3: Remove the node n (that has the lowest value h(n)) from the OPEN list, and place 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 lists, then add it to the OPEN list.

Step 7: Return to Step 2.


Example

Here we can take below stated search problem in example.


In this case, it has been traversed by 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
In this search example, two lists - OPEN and CLOSED Lists has been used.
Following are the iteration for traversing the above example.

Expand the nodes of A and put in the CLOSED list

Initialization: Open [B,C,D], Closed [A]


Iteration 1: Open [B,D], Closed [A,C]
Iteration 2: Open [E, B,D], Closed [A,C] : Open [E, A], Closed [A,C,F]
Iteration 3: Open [G, E, B,D], Closed [A,C,F] : Open [E,B,D], Closed [A,C,F,G]
Hence the final solution path will be: A----> C----->F----> G

Time Complexity: In the worst situation, the time complexity of this Greedy best first search is O(bm).
Space Complexity: In the worst situation, the space complexity of Greedy best first search is O(bm). Where, m is
the maximum depth of the search space.
Complete: Generally, the Greedy best-first search is incomplete even when the given state space is finite.
Optimal: Greedy best first search algorithm is also not optimal.
A* SEARCH ALGORITHM
This Search Algorithm is the most commonly identified form of best-first search.
This method applies heuristic function h(n), and cost to reach the node n from the start state g(n).
This search algorithm searches the shortest available path through the search space by utilizing the heuristic
function, expands only few search tree and hence provides optimal result much faster.
Here since both search heuristic as well as the cost to reach the node is utilized, we can take a combination both
the costs (as stated below), and the sum hence arrived is called fitness number f(n).
f(n) = g(n) + h(n) Where, f(n)
is the estimated cost of the most optimal solution,
g(n) represents the cost calculated to reach node n from start state, and
h(n) represents the cost calculated to reach from node n to goal node.
Here it has to be noted that at each step in the search space, only the node having lowest value f(n) is expanded,
and once the goal node is reached, the algorithm gets terminated.
Algorithm used in A* search

Step1: The process starts once the starting node is placed in the OPEN list.

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.

Step 6: Return to Step 2.


Advantages of applying this Search Algorithm

 This algorithm is the best search algorithm than other search algorithms.
 A* search algorithm is optimal and complete.
 This algorithm is capable to solve very complex problems also.

Disadvantages attached with this Search Algorithm

 This search algorithm does not always gives the shortest possible path as it is mostly based on heuristics
and approximation.
 This search algorithm has some complexity issues.
 The main drawback of applying this algorithm is the memory required for the task as it keeps all
generated nodes in the memory that occupies lots of space.
 This way it is not preferred to resolve large-scale problems.
Example

In this example, the given graph has been traversed utilizing the A* search algorithm.

The heuristic value hence calculated for all the states is stated in the table (given below) using which f(n)
of each state can be calculated by applying the formula

f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start state.

Final Path : S->D->E->F->G


HILL CLIMBING
Belonging to the family of local search algorithms, hill climbing is a simple optimization algorithm generally used in AI to
identify and give the optimal solution to a certain problem where objective is to choose best solution out of a set of
possible solutions.
This algorithm constantly moves in the direction of elevated terrain or improved value.
It does so till it reaches a peak value where none of its neighbors have a greater value.
Once it reaches there, the search ends. In this method, search algorithm generally starts with an initial solution.
Since its goal is to find the best possible solution out of the set of possible available solutions, it analyses the situation,
makes small changes and improves the solution.
This function is based on heuristic functions that enables assessment and evaluation of quality of solution at each step.
This process of making small changes continues till it reaches a local maximum i.e. the point from where no further
improvement is possible.
h(X)=+1 : for each block resting on correct one.
H(x)=-1 : for each block that is resting on wrong side.
Example :
Advantages of Hill Climbing

 Simple, intuitive and straight forward: This search algorithm is preferred for its being simple and straight-
forward that is easy to understand and implement and its accuracy in solving problem and giving optimal result.
 Memory efficiency: This algorithm is memory-efficient as it has to maintain only current state’s date.
 Rapid and Swift Convergence: to solution makes this search algorithm useful especially when time is crucial.
 Easily Modifiable: This algorithm may easily be changed and extended to contain additional heuristics or
constraints.

Weaknesses in using Hill Climbing

Susceptibility to Local Optima: Sometimes these search algorithms get stuck at locally optimal solutions which is
always not good and preferred.

Limited exploration: This feature restricts search algorithm to improvise according to neighboring or available
options and provides best solution from that range only which limits its search capability to local optima only and
limits its exploration which is not always desirable and helpful.

Dependence on Initial State: In this method, the initial step begins with a randomly selected solution. It has often
been found that quality and effectiveness of the solution depends a lot on the first value chosen.
Alpha-beta search
algorithm/Pruning
Alpha Beta Pruning is a search optimization technique

The word ‘pruning’ means cutting down branches and leaves. In Artificial Intelligence, Alpha-beta pruning is the pruning of useless
branches in decision trees.

In such games, one player aims to maximize their score while the other seeks to minimize it.

By eliminating unnecessary computations it simplifies the decision-making process, enabling faster and more efficient evaluations.

As a result, Alpha Beta Pruning is practical for real-time applications, such as game-playing AI, where speed and efficiency are critical.

How Alpha Beta Pruning Works


 The key idea behind Alpha Beta Pruning is to avoid evaluating branches of the game tree that cannot influence the final decision
based on the values already discovered during the search.
 It achieves this using two values: Alpha and Beta.

Alpha and Beta Values


 Alpha represents the best (highest-value) value that the maximizing player (usually the AI) can guarantee so far. It acts as a lower
bound. The initial value of alpha is −∞.
 Beta represents the best (lowest-value) value that the minimizing player (the opponent) can guarantee so far. It acts as an upper
bound. The initial value of alpha is +∞.
The Pruning Process  Working of Alpha-beta Pruning
This above-represented tree is the final tree that has nodes that are computed and nodes
that are not computed.

Therefore, in this example, the optimal value of the maximizer would be 10.
AO* Algorithm
● Its an informed search and works as best first search.

● AO* Algorithm is based on problem decomposition (Breakdown problem into


small pieces). AND-OR Graph
● Its an efficient method to explore a solution path.

● Each Node in the graph will also have a heuristic value associated with it.

f(n)=g(n)+h(n) f(n): Cost function.

g(n): Actual cost or

Edge value h(n): Heuristic/ Estimated value of the nodes● It finds applications in
diverse problems, including the problem of parsing using stochastic grammars in
NLP.

● Other cases include an Informational search with o It is useful for searching


game trees, problem solvinetc.
AO* Algorithm

1. Initialize the graph to start node


2. Traverse the graph following the current path accumulating nodes that have not yet been expanded or solved
3. Pick any of these nodes and expand it and if it has no successors call this value FUTILITY otherwise calculate
only f` for each of the successors.
4. If f` is 0 then mark the node as SOLVED
5. Change the value of f` for the newly created node to reflect its successors by back propagation.
6. Wherever possible use the most promising routes and if a node is marked as SOLVED then mark the parent
node as SOLVED.
7. If starting node is SOLVED or value greater than FUTILITY, stop, else repeat from 2.
What is a Constraint
Satisfaction Problem (CSP)?
A Constraint Satisfaction Problem is a mathematical problem where the solution must meet a number of constraints. In a CSP, the
objective is to assign values to variables such that all the constraints are satisfied.

CSPs are used extensively in artificial intelligence for decision-making problems where resources must be managed or arranged within
strict guidelines.

Common applications of CSPs include:

Scheduling: Assigning resources like employees or equipment while respecting time and availability constraints.

Planning: Organizing tasks with specific deadlines or sequences.

Resource Allocation: Distributing resources efficiently without overuse.

More formally, a CSP is defined as a triple (V,D,C) where:

V is a set of variables { V1V2​…....Vn​}.

D is a set of domains {D1​,D2, .....Dn​}, where each Di​is the set of possible values for Vi.

C is a set of constraints {C1, C2…..., Cn​}, where each Ci is a constraint that restricts the values that can be assigned to a subset of the
variables.
Example of CSP : Map or graph
Coloring
1 2 3 4
Initial Domains R,G,B R,G,B R,G,B R,G,B

1=R R G,B G,B G,B


2= G R G G,B B
3= B R G B B <-----Empty value
since we cannot
give R,GB so
problem not solved
V : { 1, 2, 3 , 4}
3= G R G G B
D : { Red , Green , Blue}

C : { 1!=2 , 1!=3 , 1!=4 , 2!=4 ,3!=4 }


Components of Constraint Satisfaction Problems

CSPs are composed of three key elements:

1.Variables: The things that need to be determined are variables. Variables in a CSP are the objects that must have
values assigned to them in order to satisfy a particular set of constraints. Boolean, integer, and categorical variables are
just a few examples of the various types of variables, for instance, could stand in for the many puzzle cells that need to be
filled with numbers in a sudoku puzzle.
2.Domains: The range of potential values that a variable can have is represented by domains. Depending on the issue, a
domain may be finite or limitless. For instance, in Sudoku, the set of numbers from 1 to 9 can serve as the domain of a
variable representing a problem cell.
3.Constraints: The guidelines that control how variables relate to one another are known as constraints. Constraints in a
CSP define the ranges of possible values for variables. Unary constraints, binary constraints, and higher-order constraints
are only a few examples of the various sorts of constraints. For instance, in a sudoku problem, the restrictions might be
that each row, column, and 3×3 box can only have one instance of each number from 1 to 9.

Types of Constraint Satisfaction Problems


CSPs can be classified into different types based on their constraints and problem characteristics:
Binary CSPs: In these problems, each constraint involves only two variables. For example, in a scheduling problem, the
constraint could specify that task A must be completed before task B.
Non-Binary CSPs: These problems have constraints that involve more than two variables. For instance, in a seating
arrangement problem, a constraint could state that three people cannot sit next to each other.
Hard and Soft Constraints: Hard constraints must be strictly satisfied, while soft constraints can be violated, but at a
certain cost. This distinction is often used in real-world applications where not all constraints are equally important.
Means-Ends Analysis
How means-ends analysis Works
Algorithm for Means-Ends Analysis:
Example of MEA :

You might also like