Chap-3 Search Algorithms in Artificial Intelligence
Chap-3 Search Algorithms in Artificial Intelligence
in
Artificial Intelligence
Search algorithms are one of the most important areas of
Artificial Intelligence.
Solution: It is an action sequence which leads from the start node to the goal
node.
Optimal Solution: If a solution has the lowest cost among all solutions.
Properties of Search Algorithms
Completeness: A search algorithm is said to be complete if it guarantees to return a
solution if at least any solution exists for any random input.
Time Complexity: Time complexity is a measure of time for an algorithm to complete its
task.
Space Complexity: It is the maximum storage space required at any point during the
search, as the complexity of the problem.
Types of search algorithms
Types of search algorithms
Uninformed Search Algorithms
BFS algorithm starts searching from the root node of the tree and expands all
successor node at the current level before moving to nodes of next level.
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Example:
In the given tree structure, we
have shown the traversing of
the tree using BFS algorithm
from the root node S to goal
node K. BFS search algorithm
traverse in layers, so it will
follow the path which is
shown by the dotted arrow,
and the traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Time Complexity: Time Complexity of BFS algorithm can be obtained
by the number of nodes traversed in BFS until the shallowest Node.
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Example
S---> A--->B---->D---->E--->C--->G
Backtracking is an algorithm technique for finding all possible solutions using recursion.
It will start searching from root node S, and traverse A, then B, then D and E, after traversing E, it will backtrack
the tree as E has no other successor and still goal node is not found. After backtracking it will traverse node C and
then G, and here it will terminate as it found goal node.
Advantages of DFS:
•DFS requires very less memory as it only needs to store a stack of the
nodes on the path from root node to the current node.
•It takes less time to reach to the goal node than BFS algorithm (if it
traverses in the right path).
Disadvantages of DFS:
•There is the possibility that many states keep re-occurring, and there is
no guarantee of finding the solution.
•DFS algorithm goes for deep down searching and sometime it may go to
the infinite loop.
Time Complexity of DFS:
Time complexity of DFS will be equivalent to the node traversed by the
algorithm. It is given by:
T(n)= 1+ n2+ n3 +.........+ nm=O(nm)
Where, m= maximum depth of any node and this can be much larger than d
(Shallowest solution depth)
DFS algorithm needs to store only single path from the root node, hence
space complexity of DFS is equivalent to the size of the fringe set, which
is O(bm).
Completeness:
Optimal:
Disadvantages:
• Depth-limited search also has a disadvantage of
incompleteness.
• It may not be optimal if the problem has more than
one solution.
Completeness:
DLS search algorithm is complete if the solution is above the
depth-limit.
Time Complexity:
Time complexity of DLS algorithm is O(bℓ).
Space Complexity:
Space complexity of DLS algorithm is O(b×ℓ).
Optimal:
Depth-limited search can be viewed as a special case of DFS, and it
is also not optimal even if ℓ>d.
4. Uniform-cost Search Algorithm:
4. Uniform-cost Search Algorithm:
• 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.
Time Complexity:
Let C* is Cost of the optimal solution, and ε is each step to get closer
to the goal node. Then the number of steps is = C*/ε+1. Here we have
taken +1, as we start from state 0 and end to C*/ε.
Hence, the worst-case time complexity of Uniform-cost search isO(b1 +
[C*/ε]
)/.
Space Complexity:
The same logic is for space complexity so, the worst-case space
complexity of Uniform-cost search is O(b1 + [C*/ε]).
Completeness:
Uniform-cost search is complete, such as if there is a solution, UCS
will find it.
Optimal:
Uniform-cost search is always optimal as it only selects a path with
the lowest path cost.
5. Iterative deepening depth-first Search:
5. Iterative deepening depth-first (IDDFS) Search:
• The iterative deepening algorithm is a combination of DFS and BFS
algorithms. This search algorithm finds out the best depth limit and does it
by gradually increasing the limit until a goal is found.
• This algorithm performs depth-first search up to a certain "depth limit",
and it keeps increasing the depth limit after each iteration until the goal
node is found.
• This Search algorithm combines the benefits of Breadth-first search's fast
search and depth-first search's memory efficiency.
• The iterative search algorithm is useful uninformed search when search
space is large, and depth of goal node is unknown.
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.
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 the path cost is a non-decreasing
function of the depth of the node.
6. Bidirectional Search Algorithm:
6. Bidirectional Search Algorithm:
Time Complexity:
The time complexity of bidirectional search using BFS is O(bd).
Space Complexity:
The space complexity of the bidirectional search is O(bd).
Optimal:
Bidirectional search is Optimal.
Informed Search Algorithms
Informed Search Algorithms
• The 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 efficiently the
goal node.
• The informed search algorithm is more useful for large
search space.
• Informed search algorithm uses the idea of heuristic, so it is
also called Heuristic 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 close
agent is from the goal.
• The heuristic method, however, might not always give the best
solution, but it guaranteed to find a good solution in reasonable
time.
• 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.
• The value of the heuristic function is always positive.
Admissibility of the heuristic function is given as:
h(n) <= h*(n)
Here h(n) is the heuristic cost, and h*(n) is the estimated cost.
At each point in the search space, only those node is expanded which have
the lowest value of f(n), and the algorithm terminates when the goal node is
found.
Algorithm of A* search:
Step1: Place the starting node 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.
Example:
We will traverse the graph using the A* algorithm in this example. The heuristic value of
all states is given in the below table so we will calculate the f(n) of each state using the
formula f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start state.
Here we will use OPEN and CLOSED list.
Solution:
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.
Solution: Backtracking technique can be a solution of the local maximum in state
space landscape. Create a list of the promising path so that the algorithm can
backtrack the search space and explore other paths as well.
2. 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.
Solution: The solution for the plateau is to take big steps or very little steps while
searching, to solve the problem. Randomly select a state which is far away from the
current state so it is possible that the algorithm could find non-plateau region.
3. 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.
Solution: With the use of bidirectional search, or by moving in different directions,
we can improve this problem.
Simulated Annealing:
• A hill-climbing algorithm which never makes a move towards a lower
value guaranteed to be incomplete because it can get stuck on a local
maximum. And if algorithm applies a random walk, by moving a
successor, then it may complete but not efficient.
• Simulated Annealing is an algorithm which yields both efficiency and
completeness.
• In mechanical term Annealing is a process of hardening a metal or glass
to a high temperature then cooling gradually, so this allows the metal to
reach a low-energy crystalline state. The same process is used in
simulated annealing in which the algorithm picks a random move,
instead of picking the best move. If the random move improves the state,
then it follows the same path. Otherwise, the algorithm follows the path
which has a probability of less than 1 or it moves downhill and chooses
another path.
Means-Ends Analysis in Artificial Intelligence
•We have studied the strategies which can reason either in forward or
backward, but a mixture of the two directions is appropriate for solving
a complex and large problem. Such a mixed strategy, make it possible
that first to solve the major part of a problem and then go back and solve
the small problems arise during combining the big parts of the problem.
Such a technique is called Means-Ends Analysis.
•The MEA technique was first introduced in 1961 by Allen Newell, and
Herbert A. Simon in their problem-solving computer program, which
was named as General Problem Solver (GPS).
•Means-Ends Analysis is problem-solving technique used in Artificial
intelligence for limiting search in AI programs.
•It is a mixture of Backward and forward search techniques.
•The MEA analysis process centered on the evaluation of the difference
between the current state and the goal state.
How means-ends analysis Works:
The means-ends analysis process can be applied recursively for
a problem. It is a strategy to control search in problem-solving.
Following are the main steps describing the working of the
MEA technique for solving a problem.
• First, evaluate the difference between Initial State and the
final State.
• Select the various operators which can be applied for each
difference.
• Apply the operator at each difference, reducing the difference
between the current and goal states.
Operator Subgoaling
In the MEA process, we detect the differences between the
current and goal states. Once these differences occur, then
we can apply an operator to reduce the differences. But
sometimes it is possible that an operator cannot be applied
to the current state. So we create the sub problem of the
current state, in which operator can be applied, such type
of backward chaining in which operators are selected, and
then sub goals are set up to establish the preconditions of
the operator is called Operator Subgoaling.
Algorithm for Means-Ends Analysis:
Let's we take Current state as CURRENT and Goal State as GOAL, then following
are the steps for the MEA algorithm.
Step 1: Compare CURRENT to GOAL, if there are no differences between both
then return Success and Exit.
Step 2: Else, select the most significant difference and reduce it by doing the
following steps until the success or failure occurs.
Select a new operator O which is applicable for the current difference, and if there is no such
operator, then signal failure.
Attempt to apply operator O to CURRENT. Make a description of two states.
i) O-Start, a state in which O’s preconditions are satisfied.
ii) O-Result, the state that would result if O were applied In O-start.
If
(First-Part <------ MEA (CURRENT, O-START)
And
(LAST-Part <----- MEA (O-Result, GOAL), are successful, then signal Success and return
the result of combining FIRST-PART, O, and LAST-PART.
Example of Mean-Ends Analysis:
Let's take an example where we know the initial state and goal state as given below. In
this problem, we need to get the goal state by finding differences between the initial
state and goal state and applying operators.
To solve the above problem, we will first find the differences between initial states and
goal states, and for each difference, we will generate a new state and will apply the
operators. The operators we have for this problem are:
•Move
•Delete
•Expand
1. Evaluating the initial state: In the first step, we will evaluate the initial state
and will compare the initial and Goal state to find the differences between both
states.
2. Applying Delete operator: As we can check the first difference is
that in goal state there is no dot symbol which is present in the initial
state, so, first we will apply the Delete operator to remove this dot.
3. Applying Move Operator: After applying the Delete
operator, the new state occurs which we will again compare
with goal state. After comparing these states, there is another
difference that is the square is outside the circle, so, we will
apply the Move Operator.
4. Applying Expand Operator: Now a new state
is generated in the third step, and we will compare
this state with the goal state. After comparing the
states there is still one difference which is the size
of the square, so, we will apply Expand
operator, and finally, it will generate the goal
state.