UNIT-2-AI mca1
UNIT-2-AI mca1
Searching Techniques
By: Naveen Kumar
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
and use atomic representation. 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 node.
Optimal Solution: If a solution has the lowest cost among
all solutions.
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 random input.
Optimality: 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 five main types:
•Breadth-first search
•Uniform cost search
•Depth-first search
•Iterative deepening depth-first search
•Bidirectional 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.
•Greedy Search
•A* Search
Uninformed Search Algorithms
Uninformed search is a class of general-purpose search
algorithms which operates in brute force-way. Uninformed
search algorithms do not have additional information about
state or search space other than how to traverse the tree,
so it is also called blind search.
Following are the various types of uninformed search
algorithms:
•Breadth-first Search
•Depth-first Search
•Depth-limited Search
•Iterative deepening depth-first search
•Uniform cost search
•Bidirectional Search
1. Breadth-first Search:
Breadth-first search is the most common search strategy for traversing a tree or
graph. This algorithm searches breadth wise 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:
•BFS will provide a solution if any solution exists.
•If there are more than one solutions for a given problem, then BFS will provide the
minimal solution which requires the least number of steps.
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
•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.
T (b) = 1+b2+b3+.......+ bd= O (bd)
•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 BFS will
find a solution.
•Optimality: BFS is optimal if path cost is a non-decreasing
function of the depth of the node.
2. 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
guarantee of finding the solution.
DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.
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.
•Completeness: DFS search algorithm is complete within
finite state space as it will expand every node within a
limited search tree.
•Time Complexity: 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)
•Space Complexity: 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).
•Optimal: DFS search algorithm is non-optimal, as it may
generate a large number of steps or high cost to reach to the
goal node.
3. Depth-Limited Search Algorithm:
A depth-limited search algorithm is similar to depth-first search with a
predetermined limit. Depth-limited search can solve the drawback of the infinite
path in the Depth-first search. In this algorithm, the node at the depth limit will
treat as it has no successor nodes further.
•INITIAL STATE (S0): The top node in the game-tree represents the initial
state in the tree and shows all the possible choice to pick out one.
•PLAYER (s): There are two players, MAX and MIN. MAX begins the
game by picking one best move and place X in the empty square box.
•ACTIONS (s): Both the players can make moves in the empty boxes
chance by chance.
•RESULT (s, a): The moves made by MIN and MAX will decide the
outcome of the game.
•TERMINAL-TEST(s): When all the empty boxes will be filled, it will be the
terminating state of the game.
•UTILITY: At the end, we will get to know who wins: MAX or MIN, and
accordingly, the price will be given to them.
Types of algorithms in Adversarial search
In a normal search, we follow a sequence of actions to reach the goal or to
finish the game optimally. But in an adversarial search, the result depends
on the players which will decide the result of the game. It is also obvious
that the solution for the goal state will be an optimal solution because the
player will try to win the game with the shortest path and under limited
time.
There are following types of adversarial search:
Min-max Algorithm
Alpha-beta Pruning.
Minimax Strategy
In artificial intelligence, minimax is a decision-making strategy under game
theory, which is used to minimize the losing chances in a game and to
maximize the winning chances. This strategy is also known as ‘Minmax,’ ’MM,’
or ‘Saddle point.’ Basically, it is a two-player game strategy where if one wins,
the other loose the game. This strategy simulates those games that we play in
our day-to-day life. Like, if two persons are playing chess, the result will be in
favor of one player and will unfavor the other one. The person who will make
his best try, efforts as well as cleverness, will surely win.
We can easily understand this strategy via game tree- where the nodes
represent the states of the game and edges represent the moves made by the
players in the game. Players will be two namely:
MIN: Decrease the chances of MAX to win the game.
MAX: Increases his chances of winning the game.
They both play the game alternatively, i.e., turn by turn and following the
above strategy, i.e., if one wins, the other will definitely lose it. Both players
look at one another as competitors and will try to defeat one-another, giving
their best.
In minimax strategy, the result of the game or the utility value is generated by
a heuristic function by propagating from the initial node to the root node. It
follows the backtracking technique and backtracks to find the best choice.
MAX will choose that path which will increase its utility value and MIN will
choose the opposite path which could help it to minimize MAX’s utility value.
MINIMAX Algorithm
MINIMAX algorithm is a backtracking algorithm where it backtracks to pick the
best move out of several choices. MINIMAX strategy follows the DFS (Depth-
first search) concept. Here, we have two players MIN and MAX, and the game is
played alternatively between them, i.e., when MAX made a move, then the next
turn is of MIN. It means the move made by MAX is fixed and, he cannot change
it. The same concept is followed in DFS strategy, i.e., we follow the same path
and cannot change in the middle. That’s why in MINIMAX algorithm, instead of
BFS, we follow DFS.
Keep on generating the game tree/ search tree till a limit d.
Compute the move using a heuristic function.
Propagate the values from the leaf node till the current position following the
minimax strategy.
Make the best move from the choices.
For example, in the above figure, the two players MAX and MIN are
there. MAX starts the game by choosing one path and propagating all the nodes
of that path. Now, MAX will backtrack to the initial node and choose the best
path where his utility value will be the maximum. After this,
its MIN chance. MIN will also propagate through a path and again will
backtrack, but MIN will choose the path which could minimize MAX winning
chances or the utility value.
So, if the level is minimizing, the node will accept the minimum value from the
successor nodes. If the level is maximizing, the node will accept the maximum
value from the successor.