0% found this document useful (0 votes)
5 views

Problem Solving by Searching AI

The document discusses search algorithms in Artificial Intelligence, focusing on their role in problem-solving for rational agents. It categorizes search algorithms into uninformed (blind) and informed (heuristic) types, detailing specific algorithms like breadth-first search, depth-first search, uniform-cost search, greedy search, and A* search. Key properties such as completeness, optimality, time complexity, and space complexity are also highlighted to compare the efficiency of these algorithms.

Uploaded by

namrach62
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)
5 views

Problem Solving by Searching AI

The document discusses search algorithms in Artificial Intelligence, focusing on their role in problem-solving for rational agents. It categorizes search algorithms into uninformed (blind) and informed (heuristic) types, detailing specific algorithms like breadth-first search, depth-first search, uniform-cost search, greedy search, and A* search. Key properties such as completeness, optimality, time complexity, and space complexity are also highlighted to compare the efficiency of these algorithms.

Uploaded by

namrach62
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/ 34

PROBLEM SOLVING BY SEARCHING

Search Algorithms in Artificial Intelligence


Search algorithms are one of the most important areas of Artificial
Intelligence. This topic will explain all about the search algorithms in
AI.
Problem-solving agents:
In Artificial Intelligence, Search techniques are universal problem-
solving methods. Rational agents or Problem-solving agents in AI
mostly used these search strategies or algorithms to solve a specific
problem and provide the best result. Problem-solving agents are the
goal-based agents In this topic, we will learn various problem-solving
search algorithms.
Search Algorithm Terminologies:
•Search: Searching is a step by step procedure to solve a search-problem in a
given search space. A search problem can have three main factors:
• Search Space: Search space represents a set of possible solutions, which a
system may have.
• Start State: It is a state from where agent begins the search.
• Goal test: It is a function which observe the current state and returns
whether the goal state is achieved or not.
•Search tree: A tree representation of search problem is called Search tree. The
root of the search tree is the root node which is corresponding to the initial state.
•Actions: It gives the description of all the available actions to the agent.
•Transition model: A description of what each action do, can be represented as a
transition model.
•Path Cost: It is a function which assigns a numeric cost to each path.
•Solution: It is an action sequence which leads from the start node to the goal
Properties of Search Algorithms:
Following are the four essential properties of search algorithms to
compare the efficiency of these 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
Optimality:
random input.If a solution found for an algorithm is guaranteed to be
the best solution (lowest path cost) among all other solutions, then
such a solution for is said to be an optimal solution.
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
Based on the search problems we can classify the search
algorithms into uninformed (Blind search) search and
informed search (Heuristic search) algorithms.
Uninformed/Blind Search:
The uninformed search does not contain any domain knowledge such as
closeness, the location of the goal. It operates in a brute-force way as it
only includes information about how to traverse the tree and how to
identify leaf and goal nodes. Uninformed search applies a way in which
search tree is searched without any information about the search space like
initial state operators and test for the goal, so it is also called blind search.
It examines each node of the tree until it achieves the goal node.
It can be divided into three main types:
•Breadth-first search
•Uniform cost search
•Depth-first search
Informed Search
Informed search algorithms use domain knowledge. In an informed
search, problem information is available which can guide the search.
Informed search strategies can find a solution more efficiently than an
uninformed search strategy. Informed search is also called a Heuristic
search.
A heuristic is a way which might not always be guaranteed for best
solutions but guaranteed to find a good solution in reasonable time.
Informed search can solve much complex problem which could not be
solved in another way.
An example of informed search algorithms is a traveling salesman
problem.
1.Greedy Search
2.A* Search
Uninformed Search Algorithms
Following are the various types of uninformed search algorithms:
1.Breadth-first Search
2.Depth-first Search
3.Uniform cost search
Breadth-first Search:
•Breadth-first search is the most common search strategy for traversing a
tree or graph. This algorithm searches breadthwise in a tree or graph, so
it is called breadth-first search.
•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.
•The breadth-first search algorithm is an example of a general-graph
search algorithm.
•Breadth-first search implemented using FIFO queue data structure.
Advantages:
Disadvantages:
• It requires lots of memory since each level of the tree must be saved into
memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.
Example:
In the below 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
s = the depth of the shallowest
solution.
n = number of nodes in level .
Time complexity: Equivalent to the number
of nodes traversed in BFS until the shallowest
solution.

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.
Depth-first Search
•Depth-first search is a recursive algorithm for traversing a tree or graph
data structure.
•It is called the depth-first search because it starts from the root node and
follows each path to its greatest depth node before moving to the next
path.
•DFS uses a stack data structure for its implementation.
•The process of the DFS algorithm is similar to the BFS algorithm.
Advantage:
•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).
Disadvantage:
•There is the possibility that many states keep re-occurring, and there is no
Example:
In the below search tree, we have shown the flow of depth-first search, and
it will follow the order as:
Root node--->Left node ----> right node.
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.
d= the depth of the search tree = the number of levels of
the search tree.
n = number of nodes in level .
Time complexity: Equivalent to the number of nodes
traversed in DFS.

Space complexity: Equivalent to how large can the fringe


get.
Completeness: DFS is complete if the search tree is finite,
meaning for a given finite search tree, DFS will come up
with a solution if it exists.
Optimality: DFS is not optimal, meaning the number of
steps in reaching the solution, or the cost spent in reaching
it is high.
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.
Example:

4
Informed Search Algorithms
So far we have talked about the uninformed search algorithms which
looked through search space for all possible solutions of the problem
without having any additional knowledge about search space. But
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
h(n) <= path
optimal h*(n)between the pair of states. The value of the heuristic
Here h(n) is heuristic cost, and h*(n) is the estimated cost.
function
Hence is always positive.
heuristic cost should be less than or equal to the
estimated cost.
Pure Heuristic Search:
Pure heuristic search is the simplest form of heuristic search algorithms. It
expands 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 unit a goal state is found.
In the informed search we will discuss two main algorithms which are given
below:
•Best First Search Algorithm(Greedy search)
•A* Search Algorithm
Best-first Search Algorithm (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. The greedy best first
algorithm is implemented by the priority queue.
Best first search algorithm:
•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.
•This algorithm is not optimal.
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.
In this search example, we are using two lists which
are OPEN and CLOSED Lists. Following are the iteration for
traversing the above example.

Expand the nodes of S and put in the CLOSED list

Initialization: Open [A, B], Closed [S]

Iteration 1: Open [A], Closed [S, B]

Iteration 2: Open [E, F, A], Closed [S, B]


: Open [E, A], Closed [S, B, F]

Iteration 3: Open [I, G, E, A], Closed [S, B, F]


: Open [I, E, A], Closed [S, B, F, G]

Hence the final solution path will be: S----> B----->F----> G


Time Complexity: The worst case time complexity of Greedy
best first search is O(bm).
Space Complexity: The worst case space complexity of
Greedy best first search is O(bm). Where, m is the maximum
depth of the search space.
Complete: Greedy best-first search is also incomplete, even if
the given state space is finite.
Optimal: Greedy best first search algorithm is not optimal.
A* Search Algorithm:
A* search is the most commonly known form of best-first search. It uses
heuristic function h(n), and cost to reach the node n from the start state
g(n). It has combined features of UCS and greedy best-first search, by
which it solve the problem efficiently. A* search algorithm finds the
shortest path through the search space using the heuristic function. This
search algorithm expands less search tree and provides optimal result
faster. A* algorithm is similar to UCS except that it uses g(n)+h(n) instead
of g(n).
In A* search algorithm, we use search heuristic as well as the cost to reach
the node. Hence we can combine both costs as following, and this sum is
called as a fitness number.
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.
Advantages:
•A* search algorithm is the best algorithm than other search
algorithms.
•A* search algorithm is optimal and complete.
•This algorithm can solve very complex problems.
Disadvantages:
•It does not always produce the shortest path as it mostly based on
heuristics and approximation.
•A* search algorithm has some complexity issues.
•The main drawback of A* is memory requirement as it keeps all
generated nodes in the memory, so it is not practical for various large-
scale problems.
Example:
In this example, we will traverse the given graph using the A* algorithm. 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.

2
Solution:
F(x) = g(x) + h(x)
Initialization: {(S, 5)}
Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B,
7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides
the optimal path with cost 6.w2a3
Local Search Algorithms
• Local search algorithms operate using a single current node and generally move only
to neighbors of that node.
• Local search method keeps small number of nodes in memory . They are suitable for
problems where the solution is the goal state itself and not the path.
• In addition to finding goals, local search algorithms are useful for solving pure
optimization problems, in which the aim is to find the best state according to an
objective function.
• Hill-climbing and simulated annealing are examples of local search algorithms.
Hill Climbing Algorithm in Artificial Intelligence
•Hill climbing algorithm is a local search algorithm which continuously moves in the
direction of increasing 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.
•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.
•Hill Climbing is mostly used when a good heuristic is available.
•In this algorithm, we don't need to maintain and handle the search tree or graph as
Features of Hill Climbing:
Following are some main features of Hill Climbing Algorithm:
•Generate and Test variant: Hill Climbing is the variant of
Generate and Test method. The Generate and Test method produce
feedback which helps to decide which direction to move in the search
space.
•Greedy approach: Hill-climbing algorithm search moves in the
direction which optimizes the cost.
•No backtracking: It does not backtrack the search space, as it
does not remember the previous states.
State-space Diagram for Hill Climbing:
The state-space landscape is a graphical representation of the hill-climbing
algorithm which is showing a graph between various states of algorithm and
Objective function/Cost.
On Y-axis we have taken the function which can be an objective function or
cost function, and state-space on the x-axis. If the function on Y-axis is cost
then, the goal of search is to find the global minimum and local minimum. If
the function of Y-axis is Objective function, then the goal of the search is to
find the global maximum and local maximum.

You might also like