You are on page 1of 28

MAHARANA PRATAP GROUP OF INSTITUTIONS

KOTHI MANDHANA, KANPUR


(Approved by AICTE, New Delhi and Affiliated to Dr. AKTU, Lucknow)

Digital Notes
[Department of Computer Application]
Subject Name : Artificial Intelligence
Subject Code : KCA301
Course : MCA
Branch : -
Semester : IIIth
Prepared by : Mr. Narendra Kumar Sharma

Reference No./MCA/NARENDRA/KCA301/2/3
Unit – 2
INTRODUCTION TO SEARCH

1. 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.
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.

1.2 Search Algorithm Terminologies:


o Search: Searchingis a step by step procedure to solve a search-problem in a given search
space. A search problem can have three main factors:
a. Search Space: Search space represents a set of possible solutions, which a system may
have.
b. Start State: It is a state from where agent begins the search.
c. 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.
2
Page
1.3 Properties of Search Algorithms:
Following are the four essential properties of search algorithms to compare the efficiency of
these algorithms:
1. 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.
2. 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.
3. Time Complexity: Time complexity is a measure of time for an algorithm to complete
its task.
4. Space Complexity: It is the maximum storage space required at any point during the
search, as the complexity of the problem.

2. 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.

3
Page
2.1 Uninformed Search Algorithms
The search algorithms in this section have no additional information on the goal node other
than the one provided in the problem definition. The plans to reach the goal state from the start
state differ only by the order and/or length of actions. Uninformed search is also called Blind
search.

The following uninformed search algorithms are discussed in this section.


1. Depth First Search
2. Breath First Search
3. Uniform Cost Search

Each of these algorithms will have:


 A problem graph, containing the start node S and the goal node G.
 A strategy, describing the manner in which the graph will be traversed to get to G.
 A fringe, which is a data structure used to store all the possible states (nodes) that you can
go from the current states.
 A tree, that results while traversing to the goal node.
 A solution plan, which the sequence of nodes from S to G.

1. Depth First Search


Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data
structures. 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.

Example:
Question. Which solution would DFS find to move from node S to node G if run on the graph
below? 4
Page
Solution:
The equivalent search tree for the above graph is as follows. As DFS traverses the tree
“deepest node first”, it would always pick the deeper branch until it reaches the solution (or it
runs out of nodes, and goes to the next branch). The traversal is shown in blue arrows.

Path: S -> A -> B -> C -> G


Let d = the depth of the search tree = number of levels of the search tree.
ni = number of nodes in level .

Time complexity: Equivalent to the number of nodes traversed in DFS.


T(n) = 1 + n2 + n3 + ……. + nd = O(nd)
Space complexity: Equivalent to how large can the fringe get. S(n) = O(n*d)
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.

2. Breadth First Search


Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data
structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as
5

a „search key‟), and explores all of the neighbor nodes at the present depth prior to moving on
Page

to the nodes at the next depth level.


Example:
Question. Which solution would BFS find to move from node S to node G if run on the graph
below?

Solution: The equivalent search tree for the above graph is as follows. As BFS traverses the
tree “shallowest node first”, it would always pick the shallower branch until it reaches the
solution (or it runs out of nodes, and goes to the next branch). The traversal is shown in blue
arrows.

Path: S -> D -> G


Let s = the depth of the shallowest solution.
ni = number of nodes in level i.
Time complexity: Equivalent to the number of nodes traversed in BFS until the shallowest
solution. T(n) = 1 + n2 + n3 + ……. + ns = O(ns)
6

Space complexity: Equivalent to how large can the fringe get. S(n) = O(ns)
Page
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.

3. Uniform Cost Search


UCS is different from BFS and DFS because here the costs come into play. In other words,
traversing via different edges might not have the same cost. The goal is to find a path where
the cumulative sum of costs is least.

Cost of a node is defined as:


cost(node) = cumulative cost of all nodes from root
cost(root) = 0

Example:
Question. Which solution would UCS find to move from node S to node G if run on the graph
below?

Solution: The equivalent search tree for the above graph is as follows. Cost of each node is the
cumulative cost of reaching that node from the root. Based on UCS strategy, the path with least
cumulative cost is chosen. Note that due to the many options in the fringe, the algorithm
explores most of them so long as their cost is low, and discards them when a lower cost path is
found; these discarded traversals are not shown below. The actual traversal is shown in blue.
7
Page
Path: S -> A -> B -> G
Cost: 5
Let C = cost of solution.

Ɛ = arcs cost.
Then C/ Ɛ = effective depth
Time complexity: T(n) = O (nC/ Ɛ)
Space complexity: S(n) = O (nC/ Ɛ)
Advantages:
 UCS is complete.
 UCS is optimal.
Disadvantages:
 Explores options in every “direction”.
 No information on goal location.

2.2 Informed Search Algorithms


Here, the algorithms have information on the goal state, which helps in more efficient
8

searching. This information is obtained by something called a heuristic.


Page

In this section, we will discuss the following search algorithms.


1. Greedy Search
2. A* Tree Search
3. A* Graph Search
Search Heuristics: In an informed search, a heuristic is a function that estimates how close a
state is to the goal state. For examples – Manhattan distance, Euclidean distance, etc. (Lesser
the distance, closer the goal.)
Different heuristics are used in different informed algorithms discussed below.

1. Greedy Search
In greedy search, we expand the node closest to the goal node. The “closeness” is estimated by
a heuristic h(x) .
Heuristic: A heuristic h is defined as-
h(x) = Estimate of distance of node x from the goal node.
Lower the value of h(x), closer is the node from the goal.
Strategy: Expand the node closest to the goal state, i.e. expand the node with lower h value.

Example:
Question. Find the path from S to G using greedy search. The heuristic values h of each node
below the name of the node.

9
Page
Solution: Starting from S, we can traverse to A(h=9) or D(h=5). We choose D, as it has the
lower heuristic cost. Now from D, we can move to B(h=4) or E(h=3). We choose E with lower
heuristic cost. Finally, from E, we go to G(h=0). This entire traversal is shown in the search
tree below, in blue.

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


Advantage: Works well with informed search problems, with fewer steps to reach a goal.
Disadvantage: Can turn into unguided DFS in the worst case.

2. A* Tree Search
A* Tree Search, or simply known as A* Search, combines the strengths of uniform-cost search
and greedy search. In this search, the heuristic is the summation of the cost in UCS, denoted
by g(x), and the cost in greedy search, denoted by h(x). The summed cost is denoted by f(x).

Heuristic: The following points should be noted wrt heuristics in A* search. f(x)=g(x) +h(x)
 Here, h(x) is called the forward cost, and is an estimate of the distance of the current node
from the goal node.
 And, g(x) is called the backward cost, and is the cumulative cost of a node from the root
10

node.
Page
 A* search is optimal only when for all nodes, the forward cost for a node h(x) underestimates
the actual cost h*(x) to reach the goal. This property of A* heuristic is called admissibility.
Admissibility: 0 ≤ h (x) ≤ h*x
Strategy: Choose the node with lowest f(x) value.

Example:
Question. Find the path to reach from S to G using A* search.

Solution. Starting from S, the algorithm computes g(x) + h(x) for all nodes in the fringe at
each step, choosing the node with the lowest sum. The entire working is shown in the table
below.
Note that in the fourth set of iteration, we get two paths with equal summed cost f(x), so we
expand them both in the next set. The path with lower cost on further expansion is the chosen
path.
11
Page
Path h(x) g(x) f(x)
S 7 0 7

S -> A 9 3 12

S -> D √ 5 2 7

S -> D -> B √ 4 2+1=3 7

S -> D -> E 3 2+4=6 9

S -> D -> B -> C √ 2 3+2=5 7

S -> D -> B -> E √ 3 3+1=4 7

S -> D -> B -> C -> G 0 5+4=9 9

S -> D -> B -> E -> G 0 4+3=7 7

Path: S -> D -> B -> E -> G


Cost: 7

3. A* Graph Search
 A* tree search works well, except that it takes time re-exploring the branches it has already
explored. In other words, if the same node has expanded twice in different branches of the
search tree, A* search might explore both of those branches, thus wasting time
 A* Graph Search, or simply Graph Search, removes this limitation by adding this rule: do
not expand the same node more than once.
 Heuristic. Graph search is optimal only when the forward cost between two successive
nodes A and B, given by h(A) - h (B) , is less than or equal to the backward cost between
those two nodes g(A -> B). This property of graph search heuristic is called consistency.
Consistency: h(A) – h(B) ≤ g(A→B)

Example
12

Question. Use graph search to find path from S to G in the following graph.
Page
Solution: We solve this question pretty much the same way we solved last question, but in this
case, we keep a track of nodes explored so that we don‟t re-explore them.

Path: S -> D -> B -> C -> E -> G


Cost: 7
13

https://www.tutorialandexample.com/local-search-algorithms-and-
Page

optimization-problem/
3. Local Search Algorithms and Optimization Problem
The informed and uninformed search expands the nodes systematically in two ways:
i. keeping different paths in the memory and
ii. selecting the best suitable path,
Which leads to a solution state required to reach the goal node. But beyond these “classical
search algorithms,” we have some “local search algorithms” where the path cost does not
matters, and only focus on solution-state needed to reach the goal node.
A local search algorithm completes its task by traversing on a single current node rather than
multiple paths and following the neighbors of that node generally.
Although local search algorithms are not systematic, still they have the following two
advantages:
i. Local search algorithms use a very little or constant amount of memory as they operate
only on a single path.
ii. Most often, they find a reasonable solution in large or infinite state spaces where the
classical or systematic algorithms do not work.

3.1 Working of a Local search algorithm


Let‟s understand the working of a local search algorithm with the help of an example:
Consider the below state-space landscape having both:
 Location: It is defined by the state.
 Elevation: It is defined by the value of the objective function or heuristic cost function.

14
Page
The local search algorithm explores the above landscape by finding the following two points:
 Global Minimum: If the elevation corresponds to the cost, then the task is to find the lowest
valley, which is known as Global Minimum.
 Global Maxima: If the elevation corresponds to an objective function, then it finds the
highest peak which is called as Global Maxima. It is the highest point in the valley.

3.2 Types of local searches:


1. Hill-climbing Search
2. Simulated Annealing
3. Local Beam Search

3.2.1 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.
 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 it only
keeps a single current state.

3.2.2 Features of Hill Climbing:


Following are some main features of Hill Climbing Algorithm:

o Generate and Test variant: Hill Climbing is the variant of Generate and Test method.
15

The Generate and Test method produce feedback which helps to decide which direction
Page

to move in the search space.


o Greedy approach: Hill-climbing algorithm search moves in the direction which
optimizes the cost.
o No backtracking: It does not backtrack the search space, as it does not remember the
previous states.

3.2.3 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.

3.2.4 Different regions in the state space landscape:


Local Maximum: Local maximum is a state which is better than its neighbor states, but there is
also another state which is higher than it.

Global Maximum: Global maximum is the best possible state of state space landscape. It has
the highest value of objective function.
16

Current state: It is a state in a landscape diagram where an agent is currently present.


Page
Flat local maximum: It is a flat space in the landscape where all the neighbor states of current
states have the same value.

Shoulder: It is a plateau region which has an uphill edge.

3.2.5 Types of Hill Climbing Algorithm:


1. Simple hill Climbing
2. Steepest-Ascent hill-climbing
3. Stochastic hill Climbing

1. Simple Hill Climbing:


Simple hill climbing is the simplest way to implement a hill climbing algorithm. It only evaluates
the neighbor node state at a time and selects the first one which optimizes current cost and set it
as a current state. It only checks it's one successor state, and if it finds better than the current
state, then move else be in the same state. This algorithm has the following features:

o Less time consuming


o Less optimal solution and the solution is not guaranteed

Algorithm for Simple Hill Climbing:


o Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
o Step 2: Loop Until a solution is found or there is no new operator left to apply.
o Step 3: Select and apply an operator to the current state.
o Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assign new state as a current state.
c. Else if not better than the current state, then return to step2.
Step 5: Exit.

2. Steepest-Ascent hill climbing:


The steepest-Ascent algorithm is a variation of simple hill climbing algorithm. This algorithm
examines all the neighboring nodes of the current state and selects one neighbor node which is
17

closest to the goal state. This algorithm consumes more time as it searches for multiple neighbors
Page
Algorithm for Steepest-Ascent hill climbing:
o Step 1: Evaluate the initial state, if it is goal state then return success and stop, else make
current state as initial state.
o Step 2: Loop until a solution is found or the current state does not change.
a. Let SUCC be a state such that any successor of the current state will be better than
it.
b. For each operator that applies to the current state:
i. Apply the new operator and generate a new state.
ii. Evaluate the new state.
iii. If it is goal state, then return it and quit, else compare it to the SUCC.
iv. If it is better than SUCC, then set new state as SUCC.
v. If the SUCC is better than the current state, then set current state to SUCC.

Step 5: Exit.

3. 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.

3.2.6 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.
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. 18
Page
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.

2. Simulated Annealing
Annealing is the process of heating and cooling a metal to change its internal structure for
modifying its physical properties. When the metal cools, its new structure is seized, and the
19

metal retains its newly obtained properties. In simulated annealing process, the temperature is
Page

kept variable.
We initially set the temperature high and then allow it to „cool' slowly as the algorithm
proceeds. When the temperature is high, the algorithm is allowed to accept worse solutions with
high frequency.

Start

 Initialize k = 0; L = integer number of variables;

 From i → j, search the performance difference Δ.

 If Δ <= 0 then accept else if exp(-Δ/T(k)) > random(0,1) then accept;

 Repeat steps 1 and 2 for L(k) steps.

 k = k + 1;

Repeat steps 1 through 4 till the criteria is met.

End.

3. Local Beam Search


In this algorithm, it holds k number of states at any given time. At the start, these states are
generated randomly. The successors of these k states are computed with the help of objective
function. If any of these successors is the maximum value of the objective function, then the
algorithm stops.

Otherwise the (initial k states and k number of successors of the states = 2k) states are placed in
a pool. The pool is then sorted numerically. The highest k states are selected as new initial
states. This process continues until a maximum value is reached.

function BeamSearch ( problem, k), returns a solution state.

start with k randomly generated states


loop
generate all successors of all k states
if any of the states = solution, then return the state
else select the k best successors
end.
20
Page
Adversarial search
Adversarial search is a game-playing technique where the agents are surrounded by a
competitive environment. A conflicting goal is given to the agents (multiagent). These agents
compete with one another and try to defeat one another in order to win the game. Such
conflicting goals give rise to the adversarial search. Here, game-playing means discussing those
games where human intelligence and logic factor is used, excluding other factors such as luck
factor. Tic-tac-toe, chess, checkers, etc., are such type of games where no luck factor works,
only mind works.
Mathematically, this search is based on the concept of ‘Game Theory.’ According to game
theory, a game is played between two players. To complete the game, one has to win the game
and the other looses automatically.’

Techniques required to get the best optimal solution


There is always a need to choose those algorithms which provide the best optimal solution in a
limited time. So, we use the following techniques which could fulfill our requirements:
 Pruning: A technique which allows ignoring the unwanted portions of a search tree which make
no difference in its final result.
 Heuristic Evaluation Function: It allows to approximate the cost value at each level of the
search tree, before reaching the goal node.
21
Page
Elements of Game Playing search
To play a game, we use a game tree to know all the possible choices and to pick the best one out.
There are following elements of a game-playing:
 S0: It is the initial state from where a game begins.
 PLAYER (s): It defines which player is having the current turn to make a move in the state.
 ACTIONS (s): It defines the set of legal moves to be used in a state.
 RESULT (s, a): It is a transition model which defines the result of a move.
 TERMINAL-TEST (s): It defines that the game has ended and returns true.
 UTILITY (s,p): It defines the final value with which the game has ended. This function is
also known as Objective function or Payoff function. The price which the winner will get
i.e.
 (-1): If the PLAYER loses.
 (+1): If the PLAYER wins.
 (0): If there is a draw between the PLAYERS.

For example, in chess, tic-tac-toe, we have two or three possible outcomes. Either to win, to
lose, or to draw the match with values +1,-1 or 0.
Let‟s understand the working of the elements with the help of a game tree designed for tic-tac-
toe. Here, the node represents the game state and edges represent the moves taken by the players.

22
Page
 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:
1. Minmax Algorithm
2. Alpha-beta Pruning

1. Minmax Algorithm
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
23

as well as cleverness, will surely win.


Page
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.
24
Page
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.

2. Alpha-beta Pruning
Alpha-beta pruning is an advance version of MINIMAX algorithm. The drawback of minimax
strategy is that it explores each node in the tree deeply to provide the best path among all the
paths. This increases its time complexity. But as we know, the performance measure is the first
consideration for any optimal algorithm. Therefore, alpha-beta pruning reduces this drawback of
minimax strategy by less exploring the nodes of the search tree.
The method used in alpha-beta pruning is that it cutoff the search by exploring less number of
nodes. It makes the same moves as a minimax algorithm does, but it prunes the unwanted
branches using the pruning technique (discussed in adversarial search). Alpha-beta pruning
works on two threshold values, i.e., ? (alpha) and ? (beta).
25
Page
 ?: It is the best highest value, a MAX player can have. It is the lower bound, which represents
negative infinity value.
 ?: It is the best lowest value, a MIN player can have. It is the upper bound which represents
positive infinity.
So, each MAX node has ?-value, which never decreases, and each MIN node has ?-value, which
never increases.
Note: Alpha-beta pruning technique can be applied to trees of any depth, and it is possible to
prune the entire subtrees easily.

Working of Alpha-beta Pruning


Consider the below example of a game tree where P and Q are two players. The game will be
played alternatively, i.e., chance by chance. Let, P be the player who will try to win the game by
maximizing its winning chances. Q is the player who will try to minimize P’s winning chances.
Here, ? will represent the maximum value of the nodes, which will be the value for P as
well. ? will represent the minimum value of the nodes, which will be the value of Q.

26
Page
 Any one player will start the game. Following the DFS order, the player will choose one path
and will reach to its depth, i.e., where he will find the TERMINAL value.
 If the game is started by player P, he will choose the maximum value in order to increase its
winning chances with maximum utility value.
 If the game is started by player Q, he will choose the minimum value in order to decrease the
winning chances of A with the best possible minimum utility value.
 Both will play the game alternatively.
 The game will be started from the last level of the game tree, and the value will be chosen
accordingly.
 Like in the below figure, the game is started by player Q. He will pick the leftmost value of the
TERMINAL and fix it for beta (?). Now, the next TERMINAL value will be compared with the
?-value. If the value will be smaller than or equal to the ?-value, replace it with the current ?-
value otherwise no need to replace the value.
 After completing one part, move the achieved ?-value to its upper node and fix it for the other
threshold value, i.e., ?.
 Now, its P turn, he will pick the best maximum value. P will move to explore the next part only
after comparing the values with the current ?-value. If the value is equal or greater than the
current ?-value, then only it will be replaced otherwise we will prune the values.
 The steps will be repeated unless the result is not obtained.
 So, number of pruned nodes in the above example are four and MAX wins the game with the
maximum UTILITY value, i.e.,3

27
Page
References

1. Stuart Russell, Peter Norvig, “Artificial Intelligence – A Modern Approach”, Pearson


Education
2. Elaine Rich and Kevin Knight, “Artificial Intelligence”, McGraw-Hill
3. E Charniak and D McDermott, “Introduction to Artificial Intelligence”, Pearson Education
4. Dan W. Patterson, “Artificial Intelligence and Expert Systems”, Prentice Hall of India
5. https://www.tutorialandexample.com/local-search-algorithms-and-optimization-problem/
6. https://www.tutorialandexample.com/adversarial-search-in-artificial-intelligence/
7. https://www.javatpoint.com/search-algorithms-in-ai

28
Page

You might also like