0% found this document useful (0 votes)
13 views34 pages

(R20) AI UNIT-2 srgec

The document provides an overview of various search algorithms used in artificial intelligence, categorizing them into uninformed (blind) and informed (heuristic) search methods. It details specific algorithms such as Depth-First Search, Breadth-First Search, Random Search, and Best-First Search, explaining their mechanisms, advantages, and disadvantages. Additionally, it discusses the use of open and closed lists in search strategies to track nodes during traversal.

Uploaded by

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

(R20) AI UNIT-2 srgec

The document provides an overview of various search algorithms used in artificial intelligence, categorizing them into uninformed (blind) and informed (heuristic) search methods. It details specific algorithms such as Depth-First Search, Breadth-First Search, Random Search, and Best-First Search, explaining their mechanisms, advantages, and disadvantages. Additionally, it discusses the use of open and closed lists in search strategies to track nodes during traversal.

Uploaded by

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

ARTIFICIAL INTELLIGENCE

UNIT-II
SEARCH ALGORITHMS
Syllabus:
Random search, Search with closed and open list, Depth first and Breadth first search,
Heuristic search, Best first search, A* algorithm, Game Search.

1. INTRODUCTION
 Based on the search problems we can classify the search algorithms into uninformed
(Blind search) search and informed search (Heuristic search) algorithms.

Search Algorithms

Uninformed/Blind Search Informed Search

Depth First Search Best First Search

Breadth First Search A* Search

Uniform cost Search

Depth Limited Search

Iterative deepening
Depth First Search

Bidirectional Search

Figure 1: Classification of search algorithms


1.1 Uninformed/Blind Search
 The uninformed search does not contain any domain knowledge such as closeness, the
location of the goal.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 1 of 34


 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.
1. Breadth-first search
2. Uniform cost search
3. Depth-first search
4. Iterative deepening depth-first search
5. Bidirectional Search
1.2 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.

2. DEPTH-FIRST SEARCH
 Depth-first search always expands the deepest node in the current frontier of the
search tree.
 The progress of the search is illustrated in Figure 2.
 The search proceeds immediately to the deepest level of the search tree, where the
nodes have no successors.
 As those nodes are expanded, they are dropped from the frontier, so then the search
“backs up” to the next deepest node that still has unexplored successors.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 2 of 34


Figure 2: Depth-first search on a binary tree. The unexplored region is shown in light gray.
Explored nodes with no descendants in the frontier are removed from memory. Nodes at
depth 3 have no successors and M is the only goal node.
Algorithm: Depth-First Search
1. If the initial state is a goal state, quit and return success.
2. Otherwise, do the following until success or failure is returned:
(a) Generate a successor, E, of the initial state. If there are no more successors, return failure.
(b) Call Depth-First Search with E as the initial state.
(c) If success is returned, quit. Otherwise continue in this loop
2.1 Advantage
1. 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.
2. It takes less time to reach to the goal node than BFS algorithm (if it traverses in the
right path.
2.2 Disadvantage
1. There is the possibility that many states keep re-occurring, and there is no guarantee
of finding the solution.
2. DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.
3. BREADTH-FIRST SEARCH
 Breadth-first search is the most common search strategy for traversing a tree or graph.
This algorithm searches breadth (level) 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.

Figure 3: Breadth-first search on a simple binary tree. At each stage, the node to be expanded
next is indicated by a marker.
Algorithm: Breadth-First Search
1. Create a variable called NODE-LIST and set it to the initial state.
2. Until a goal state is found or NODE-LIST is empty:
(a) Remove the first element from NODE-LIST and call it E. If NODE-LIST was empty, quit.
(b) For each way that each rule can match the state described in E do:
(i) Apply the rule to generate a new state.
(ii) If the new state is a goal state, quit and return this state.
(iii) Otherwise, add the new state to the end of NODE-LIST.
3.1 Advantages
1. BFS will provide a solution if any solution exists.
2. 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.
3.2 Disadvantage
1. It can be slow since it expands all the nodes at each level before moving on to the next
level.
2. Memory Constraints as it stores all the nodes of the present level to go for the next
level.
4. RANDOM SEARCH
 Random search (RS) is a family of numerical optimization methods that do not
require the gradient of the problem to be optimized, and RS can hence be used on
functions that are not continuous or differentiable. Such optimization methods are also
known as direct-search, derivative-free, or black-box methods.
4.1 Generate-And-Test
 If the generation of possible solutions is done systematically, then this procedure will
find a solution eventually (in time), if one exists. Unfortunately, if the problem space
is very large, "eventually" may be a very long time.
 The generate-and-test algorithm is a depth-first search procedure since complete
solutions must be generated before they can be tested.
 In its most systematic form, it is simply a complete search of the problem space.
Generate-and-test can, of course, also operate by generating solutions randomly, but
then there is no guarantee that a solution will ever be found.
 The most straight forward way to implement systematic generate-and-test is as a
depth-first search tree.
 For simple problems, in-depth generate-and-test is often a reasonable technique.

Generator

Possible Solutions

Tester

Correct Solution
Incorrect Solution

Stop

Figure 4: Diagrammatic Representation of Generate-And-Test


Algorithm: Generate-and-Test
1. Generate a possible solution.
2. Test to see if this is actually a solution by comparing the chosen point or the endpoint
of the chosen path to the set of acceptable goal states.
3. If a solution has been found, quit. Otherwise, return to step 1.
4.1.1 Example: coloured blocks
 Consider the puzzle that consists of four six-sided cubes, with each side of each cube
painted one of four colors.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 5 of 34


 A solution to the puzzle consists of an arrangement of the cubes in a row such that on
all four sides of the row one block face of each color is showing.
 This problem can be solved by a person in several minutes by systematically and
exhaustively trying all possibilities. It can be solved even more quickly using a
heuristic generate-and-test procedure.
4.1.2 Example – Traveling Salesman Problem (TSP)
 A salesman has a list of cities, each of which he must visit exactly once. There are
direct roads between each pair of cities on the list. Find the route the salesman should
follow for the shortest possible round trip that both starts and finishes at any one of
the cities.
 Traveler needs to visit n cities.
 Know the distance between each pair of cities.
 Want to know the shortest route that visits all the cities once.

 Search flow with Generate and Test

 Finding optimal path.

Path Length of Path


ABCD 13
ABDC 12
ACBD 6
ACDB 7
ADCB 12
ADBC 10
5. SEARCH WITH CLOSED AND OPEN LIST
 An Open list that keeps track of the current 'immediate' nodes available for traversal
and a Closed list that keeps track of the nodes already traversed.
 Steps to search with Open, Closed lists as follows:
Step 1: Place the starting node into the OPEN list. If the OPEN list is empty, Stop and
return failure.
Step 3: Remove node n, from the OPEN list, and place it in the CLOSED list.
Step 4: Expand node n, and generate the successors of node n.

Example 1: Search for the node E in the tree/graph structure with the help of Depth-First
Search using Open and Closed list.

B C

D E F G

Open List Closed List


A ---
B, C A
D, E, C A, B
E, C A, B, D
C A, B, D, E

 Depth-First Search use STACK data structure. So Open List in DFS is STACK.
 Here goal state E is found. So search becomes Success

Example 2: Search for the node H in the tree/graph structure with the help of Depth-First
Search using Open and Closed list.

B C

D E F G

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 7 of 34


Open List Closed List
A ---
B, C A
D, E, C A, B
E, C A, B, D
C A, B, D, E
F, G A, B, D, E, C
G A, B, D, E, C, F
--- A, B, D, E, C, F, G

 In the above example, Open list become empty and till the goal state is not found. So
search becomes failure.

Example 3: Search for the node E in the tree/graph structure with the help of Breadth-First
Search using Open and Closed list.

B C

D E F G

Open List Closed List


A ---
B, C A
C, D, E A, B
D, E, F, G A, B, C
E, F, G A, B, C, D
F, G A, B, C, D, E
 Here goal state E is found. So search becomes Success.
 Breadth-First Search use QUEUE data structure. So Open List in BFS is QUEUE.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 8 of 34


Example 4: Search for the node H in the tree/graph structure with the help of Breadth-First
Search using Open and Closed list.

B C

D E F G

Open List Closed List


A ---
B, C A
C, D, E A, B
D, E, F, G A, B, C
E, F, G A, B, C, D
F, G A, B, C, D, E
G A, B, C, D, E, F
--- A, B, C, D, E, F, G
 In the above example, Open list become empty and till the goal state is not found. So
search becomes failure.

6. HEURISTIC SEARCH
 Heuristic search is also termed as an informed search strategy one that uses problem-
specific knowledge beyond the definition of the problem itself can find solutions more
efficiently than can an uninformed strategy.
 A heuristic is a technique that improves the efficiency of a search process, possibly by
sacrificing claims of completeness.
 Using good heuristics, we can hope to get good solutions to hard problems, such as
the traveling salesman, in less than exponential time.
 There are some good general purpose heuristics that are useful in a wide variety of
problem domains.
 One example of a good general-purpose heuristic that is useful for a variety of
combinatorial problems is the nearest neighbor heuristic, which works by selecting
the locally superior alternative at each step. Applying it to the traveling salesman
problem, we produce the following procedure:
1. Arbitrarily select a starting city.
2. To select the next city, look at all cities not yet visited, and select the one closest to
the current city. Go to it next.
3. Repeat step 2 until all cities have been visited.
 Most search algorithms include as a component of heuristic function, denoted h(n).

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 9 of 34


7. BEST-FIRST SEARCH
 Best First Search, which is a way of combining the advantages of both Depth First
Search and Breadth First Search into a single method.
 Best First Search algorithm always selects the path which appears best at that
moment.
 It uses the heuristic function and search.
 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 h(n).
 Where, h(n) is estimated cost from node n to the goal.
 The Best First algorithm is implemented by the priority queue.
 The parent link will make it possible to recover the path to the goal once the goal is
found. The list of successors will make it possible, if a better path is found to an
already existing node, to propagate the improvement down to its successors. We will
call a graph of this sort an OR graph, since each of its branches represents an
alternative problem-solving path.

Example 1: Search for node K in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

A
3 2

B C
4 1 6 5

D E F G
8 3 2 1
J K H I

Open List Closed List


A0 ---
B3, C2 A0
B3, F8, G7 A0 , C 2
F8, G7, D7, E4 A0, C2, B3
F8, G7, D7, J12, K7 A0, C2, B3, E4
F8, D7, J12, K7 A0, C2, B3, E4, G7
F8, J12, K7 A0, C2, B3, E4, G7, D7
F8, J12 A0, C2, B3, E4, G7, D7, K7

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 10 of 34


 Here node K is GOAL state. So Search becomes Success.
 Final Path Cost is 7 (3 + 1 +3)

Initially A

 Current node A is not a GOAL state. So expand node A.

A
3 2

B C

 Now node C becomes Best node. Here current node C is not a GOAL state. So expand
node C.

A
3 2

B C
6 5

F G

 Now node B becomes Best node. Here current node B is not a GOAL state. So expand
node B.

A
3 2

B C
4 1 6 5

D E F G

 Now node E becomes Best node. Here current node E is not a GOAL state. So expand
node E.

A
3 2

B C
4 1 6 5

D E F G
8 3
J K

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 11 of 34


 Now node G becomes Best node. Here current node G is not a GOAL state. So
expand node G.

A
3 2

B C
4 1 6 5

D E F G
8 3
J K

 Now node D becomes Best node. Here current node D is not a GOAL state. So
expand node D.

A
3 2

B C
4 1 6 5

D E F G
8 3
J K

 Now node K becomes Best node. Here current node K is GOAL state. So stop the
search process.

A
3 2

B C
4 1 6 5

D E F G
8 3
J K

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 12 of 34


Example 2: Search for node F in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

A
2 4

B C
6 3 2 5

D E F G
1 4 5 8 6 4 3 2
H I J K L M N O

Open List Closed List


A0 ---
B2, C4 A0
C4, D8, E5 A0 , B 2
D8, E5, F6, G9 A0, B2, C4
D8, F6, G9, J10, K13 A0, B2, C4, E5
D8, G9, J10, K13, L12, M10 A0, B2, C4, E5, F6,
 Here node F is GOAL state. So Search becomes Success.
 Final Path Cost is 6 (4 + 2)

Example 3: Search for node A in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

Z
5 3

X C
2 4 6 8

B V N M

Open List Closed List


Z0 ---
X5 , C 3 Z0
X5, N9, M11 Z0, C3
N9, M11, B7, V9 Z0, C3, X5
N9, M11, V9 Z0, C3, X5, B7
M11, V9 Z0, C3, X5, B7, N9
M11 Z0, C3, X5, B7, N9, V9
--- Z0, C3, X5, B7, N9, V9, M11
 Here Open list becomes empty and goal state is not found. So search becomes failure.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 13 of 34


Example 4: Search for node Y in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

Q
10 8

W E
2 4 5 6

R T Y Z
2 1 3 4
X C V B

Open List Closed List


Q0 ---
W10, E8 Q0
W10, Y13, Z14 Q0, E8
Y13, Z14, R12, T14 Q0, E8, W10,
Y13, Z14, T14, Q0, E8, W10, R12
Z14, T14 Q0, E8, W10, R12, Y13

 Here node Y is GOAL state. So Search becomes Success.


 Final Path Cost is 13 (8 + 5)

Example 5: Search for node F in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

A 3
4

B C
5 2
8 6

D E

3 9
F

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 14 of 34


Open List Closed List
A0 ---
B4, C3 A0
B4, D5, E9 A0 , C 3
D5, E9 A0, C3, B4
E9, F8 A0, C3, B4, D5
E9 A0, C3, B4, D5, F8

 Here node F is GOAL state. So Search becomes Success.


 Final Path Cost is 13 (3 + 2 +3)

Example 6: Search for node V in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

Z 8
13

X C
5 16
10 2

V B
1

Open List Closed List


Z0 ---
X13, C8 Z0
X13, V24, B10 Z0, C8
X13, V11 Z0, C8, B10
X13, Z0, C8, B10, V11

 Here node V is GOAL state. So Search becomes Success.


 Final Path Cost is 11 (8 + 2 +1)

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 15 of 34


Initially Z

X C

V B

 Here current node Z is not a GOAL state. So expand node Z.

Z 8
13

X C

V B

 Now node C becomes Best node. Here current node C is not a GOAL state. So expand
node C.

Z 8
13

C
X
16
2

V B

 Now node B becomes Best node. Here current node B is not a GOAL state. So expand
node C.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 16 of 34


Z 8
13

C
X

V 1 B

 Here node V obtain better path through node B. So change parent of node V as B.
 Now node V becomes Best node. Here current node V is a GOAL state. So examine
node C.

Example 7: Search for node D in the following Tree/Graph using Best First Search with the
help of Open and Closed List.

A
15 3

B C
8 10
4 2

D F
6

Open List Closed List


A0 ---
B15, C3 A0
B15, D13, F5 A0 , C 3
B13, D11 A0, C3, F5
B13 A0, C3, F5, D11
 Here node D is GOAL state. So Search becomes Success.
 Final Path Cost is 11 (3 + 2 + 6)

A
Initially

B C

D F

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 17 of 34


 Here current node A is not a GOAL state. So expand node A.

A
15 3

B C

D F

 Now node C becomes Best node. Here current node C is not a GOAL state. So expand
node C.

A
15 3

B C
10
2

D F

 Now node F becomes Best node. Here current node F is not a GOAL state. So expand
node F.

A
3

B C
8
2

D F
6

 Here best path cost is calculated from node A to B, A to D through node F. So parent
of B and D is changed to node F.
 Now node D becomes Best node. Here current node D is a GOAL state. So success.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 18 of 34


Algorithm: Best-First Search
1. Start with OPEN LIST containing just the initial state.
2. Until a goal is found or there are no nodes left on OPEN LIST do:
(a) Remove the node n, from the OPEN LIST which has the lowest value of h(n)
(b) if node n is goal state, stop the search process and return node n
(c) else generate successors of node n
(d) For each successor do:
(i) If it has not been generated before, evaluate it, add it to OPEN LIST, and
record its parent.
(ii) If it has been generated before, change the parent if this new path is better
than the previous one. In that case, update the cost of getting to this node
and to any successors that this node may already have.
7.1 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.
7.2 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.

8. THE A* ALGORITHM
 The best-first search algorithm that was just presented is a simplification of an
algorithm called A*.
 This algorithm uses OPEN and CLOSED list.
 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.
f(n) = g(n) + h(n)
 Here f(n) refers path length from source node to node n.
 g(n) refers heuristic value of node n.
 A* algorithm finds shortest path in the search space using h(n).
 This algorithm expands less search tree and provides optimal path.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 19 of 34


Example 1: Search for node G in the following tree/graph structure using A* algorithm by
considering node S as initial state.

2
A B State h(n)
1 1 S 5
5 A 3
S C 3 B 4
D C 2
4 D 6
10
2
G 0
G

Open List Closed List


S0 ---
A1, G10 S0
G10, B7, C4 S0, A1
G6, B7, D14 S0, A1, C4
B7, D14 S0, A1, C4 , G6

 Here node G is GOAL state. So Search becomes Success.


 Final Path Cost is 6 (1 + 1 + 4)
 Optimal path is S  A  C  G

f(n) = g(n) + h(n)


SA f(A) = 1 + 3 4
SG f(G) = 10 + 0  10

SAB f(B) = 3 + 4 7
SAC f(C) = 2 + 2 4

SACD f(D) = 8 + 6  14
SACG f(G) = 6 + 0 6

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 20 of 34


Example 2: Search for node I in the following tree/graph structure using A* algorithm by
considering node A as initial state.
State h(n)
A A 8
1 2
B 10
B C C 4
7 5 4 8 D 15
9 3 6
E 14
D E F G H 5I J F 12
1 G 7
H 2
I 0
Open List Closed List J 4
A0 ---
B11, C6 A0
B11, G13, H7, I8, J14 A0, C6
B11, G13, I6, J14 A0, C6, H7
B11, G13, J14 A0, C6, H7, I6
 Here node I is GOAL state. So Search becomes Success.
 Final Path Cost is 6 (2 + 3 + 1)
 Optimal path is A  C  H  I

f(n) = g(n) + h(n)


AB f(B) = 1 +10  11
AC f(C) = 2 +4 6

ACG f(G) = 6 + 7  13
ACH f(H) = 5 + 2 7
ACI f(I) = 8 + 0 8
ACJ f(J) = 10 + 4  14

ACHI f(I) = 6 + 0 6

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 21 of 34


Example 3: Search for node F in the following tree/graph structure using A* algorithm by
considering node A as initial state.

A State h(n)
12 15
A 0
B C B 4
14 2
C 7
8 6 D 5
E 2
D E
4 F 4

10 3
F

Open List Closed List


A0 ---
B16, C22 A0
C22, D25, E28 A0, B16
D22, E23 A0, B16, C22
E23, F31 A0, B16, C22, D22
F28 A0, B16, C22, D22, E23
--- A0, B16, C22, D22, E23, F28

 Here node F is GOAL state. So Search becomes Success.


 Final Path Cost is 24 (15 + 2 + 4 + 3)
 Optimal path is A  C  D  E  F

f(n) = g(n) + h(n)


AB f(B) = 12 + 4  16
AC f(C) = 15 + 7  22

ABD f(D) = 20 + 5  25
ABE f(E) = 26 + 2  28

AC D f(D) = 17 + 5  22

AC E f(E) = 21 + 2  23

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 22 of 34


AC DE f(E) = 21 + 2  23

AC DF f(F) = 27 + 4  31

AC DE F f(F) = 24 + 4  28

Algorithm of A* search
1. Place the starting node in the OPEN list.
2. Check if the OPEN list is empty or not, if the list is empty then return failure and stop.
3. else select a node n from the OPEN list which has the smallest value of evaluation function
f(n).
4. If node n is goal node then return success and stop.
5. Otherwise expand node n and generate all of its successors, and put node n into the closed
list.
(a) For each successor n', check whether n' is already in the OPEN list, if not then
compute evaluation function for n' and place into OPEN list.
(b) Else if node n' is already in OPEN list, then it should be attached to the
back pointer which reflects the lowest g(n') value.
6. Goto Step 2.

8.1 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.
8.2 Disadvantages
 It does not always produce the shortest path as it mostly based on heuristics and
approximation.
 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.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 23 of 34


9. GAME SEARCH
9.1 The Minimax Search Procedure
 The minimax search procedure is a depth-first, depth-limited search procedure. The
idea is to start at the current position and use the plausible-move generator to generate
the set of possible successor positions.
 Mini-max algorithm is a recursive or backtracking algorithm which is used in
decision-making and game theory.
 It provides an optimal move for the player assuming that opponent is also playing
optimally.
 Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers,
tic-tac-toe, etc.
 This Algorithm computes the minimax decision for the current state.
 In this algorithm two players play the game, one is called MAX and other is called
MIN.
 Both the players fight it as the opponent player gets the minimum benefit while they
get the maximum benefit.
 Both Players of the game are opponent of each other, where MAX will select the
maximized value and MIN will select the minimized value.
 The minimax algorithm performs a depth-first search algorithm for the exploration of
the complete game tree.
 The minimax algorithm proceeds all the way down to the terminal node of the tree,
then backtrack the tree as the recursion.
 Max will try to maximize its utility. i.e. Best moves.
 Min will try to minimize its utility. i.e. worst move.

Example 1: Obtain optimal path using minimax search on the following tree/graph structure.

A Max

B C Min

D E F G Max

4 -1 8 5 -7 -4 -2 -1

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 24 of 34


A Max

B C Min

4 D E 8 -4 F G -1 Max

4 -1 8 5 -7 -4 -2 -1

Maximizing player

A Max

4 -4
B C Min

4 D E 8 -4 F G -1 Max

4 -1 8 5 -7 -4 -2 -1

Minimizing player
4 Max
A

4 -4
B C Min

4 D E 8 -4 F G -1 Max

4 -1 8 5 -7 -4 -2 -1

Maximizing player
4 Max
A

4 -4
B C Min

4 D E 8 -4 F G -1 Max

4 -1 8 5 -7 -4 -2 -1

Optimal path is A – B – D
Optimal path value is 4

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 25 of 34


Example 2: Obtain optimal path using minimax search on the following tree/graph structure.

M Max

N O Min

P Q R S Max

3 8 -3 4 7 -6 -9 -7

M Max

N O Min

8 4 7 -7
P Q R S Max

3 8 -3 4 7 -6 -9 -7

Maximizing player

M Max

4 -7
N O Min

8 4 7 -7
P Q R S Max

3 8 -3 4 7 -6 -9 -7

Minimizing player
4 Max
M
4 -7
N O Min

8 4 7 -7
P Q R S Max

3 8 -3 4 7 -6 -9 -7

Maximizing player

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 26 of 34


4 Max
M
4 -7
N O Min

8 4 7 -7
P Q R S Max

3 8 -3 4 7 -6 -9 -7

Optimal path is M – N – Q
Optimal path value is 4

Example 3: Obtain optimal path using minimax search on the following tree/graph structure.

Z Max

Min
X C V

B N M Q W E Max

5 4 -3 2 1 8 9 4 3 1 8 5

Z Max

Min
X C V
5 2 8 9 3 8
B N M Q W E Max

5 4 -3 2 1 8 9 4 3 1 8 5

Maximizing player

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 27 of 34


Z Max

2 8 3
Min
X C V
5 2 8 9 3 8
B N M Q W E Max

5 4 -3 2 1 8 9 4 3 1 8 5

Minimizing player
8
Z Max

2 8 3
Min
X C V
5 2 8 9 3 8
B N M Q W E Max

5 4 -3 2 1 8 9 4 3 1 8 5

Maximizing player

Z Max

2 8 3
Min
X C V
5 2 8 9 3 8
B N M Q W E Max

5 4 -3 2 1 8 9 4 3 1 8 5

Optimal path is Z – C – M
Optimal path value is 8

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 28 of 34


Algorithm for MiniMax
function MiniMax(node, depth, maximizingPlayer) is
if depth == 0 or node is a terminal node then
return static evaluation of node
if MaximizingPlayer then // for Maximizer Player
maxEva = -∞
for each child of node do
eva = MiniMax(child, depth+1, false)
maxEva= max(maxEva,eva) //gives Maximum of the values
return maxEva

else // for Minimizer player


minEva = +∞
for each child of node do
eva = MiniMax(child, depth+1, true)
minEva= min(minEva, eva) //gives minimum of the values
return minEva

9.2 Adding Alpha-Beta Cutoffs (Alpha-Beta Pruning)


 Alpha-beta pruning is a modified version of the MiniMax algorithm. It is an
optimization technique for the MiniMax algorithm.
 As we have seen in the MiniMax search algorithm that the number of game states it
has to examine are exponential in depth of the tree. Since we cannot eliminate the
exponent, but we can cut it to half. Hence there is a technique by which without
checking each node of the game tree we can compute the correct MiniMax decision,
and this technique is called pruning. This involves two threshold parameter Alpha
and beta for future expansion, so it is called alpha-beta pruning.
 Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only
prune the tree leaves but also entire sub-tree.
 The two-parameter can be defined as:
Alpha denotes MAX value. Worst case value is -∞.
Beta denotes MIN value. Worst case value is +∞.
 At MAX level alpha value will be update, beta value will remain same.

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 29 of 34


 At MIN level alpha value will remain same, beta value will be update.
 This algorithm follows DFS strategy.
 Here search process is to handle both maximizing and minimizing player, it includes
two bounds, one for each player. i.e. lower bound and upper bound.
 Every time this algorithm will verify α>=β. If this condition is true, prune the
remaining sub-tree.
 Whenever we are moving from top to bottom, i.e. from parent to child node, parent
node values will be transfer to child node.
 Whenever we are moving from bottom to top, i.e. from child to parent node, parent
node values will be update based on its RIGHT child node.
 Alpha value will be update if parent node is at MAX level, Beta value will be update
if parent node is at MIN level.

Example 1: Apply Alpha-Beta Pruning on the following tree/graph structure.

A Max

B C Min

D E F G Max

-1 3 5 1 -6 -4 0 9

α = -∞ 3
β = +∞ A Max

α = -∞ α=3
Min
β = +∞ 3 B C β = +∞ 3
α = -∞ 5
α = -∞ -1 3 β=3 α=3
D E F G Max
β = +∞ β = +∞

-1 3 5 1 -6 -4 0 9

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 30 of 34


Example 2: Apply Alpha-Beta Pruning on the following tree/graph structure.

A Max

Min
B C D

3 12 8 2 4 6 14 5 2

α = -∞ 3
β = +∞ A Max

α = -∞ α=3 α=3
β = +∞ 3 B Min
β = +∞ 2 C β = +∞ 14 5 2 D

3 12 8 2 4 6 14 5 2

Example 3: Apply Alpha-Beta Pruning on the following tree/graph structure.

A MAX

MIN
B C

MAX
D E F G

H I J K L M N O MIN

10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 31 of 34


α = -∞ 7
β = +∞
A MAX

α = -∞ α=7
β = +∞ 7 β = +∞ 11 MIN
B C
α = -∞ 5 7
β = +∞ α = -∞ 7 α = 7 11 α=78
β=7 β = +∞ β = 11
MAX
D E F G
α = -∞ α=5 α=7
α = -∞
β = +∞ 10 5 β = +∞ 7 β = 7 β = +∞ 11
α=7
α=8
H I J K β = +∞ 5 L M α=7 N β = 11 7 O MIN
β = 11 9 8

10 5 7 11 12 8 9 8 5 12 11 12 9 8 7 10

Example 4: Apply Alpha-Beta Pruning on the following tree/graph structure.

A MAX

MIN
B C D

E F G H I J MAX

2 3 5 9 0 7 4 2 1 5 6

α = -∞ 3
β = +∞ A MAX

α = -∞ α=3
β = +∞ 3
α=3
β = +∞ 3 MIN
B β = +∞ 3 C D

α = -∞ 2 3 α = -∞ 5 α=3 α=3
β = +∞ E F β=3 G H β = +∞ I J MAX
β = +∞

2 3 5 9 0 7 4 2 1 5 6

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 32 of 34


Example 5: Apply Alpha-Beta Pruning on the following tree/graph structure.

A MAX

B C MIN

D E F G MAX

1 2 3 4 5 6 7 8

α = -∞ 2
β = +∞
A MAX
α = -∞
α=2
β = +∞ 2
β = +∞ 6 MIN
B C

α = -∞ 1 2 α = -∞ 3 α=256 α=27
β = +∞ D E β=2 β = +∞ F β=6 G MAX

1 2 3 4 5 6 7 8

Algorithm Alpha-Beta Pruning


function MiniMax (node, depth, alpha, beta, maximizingPlayer) is
if depth == 0 or node is a terminal node then
return static evaluation of node
if MaximizingPlayer then // for Maximizer Player
maxEva = - ∞
for each child of node do
eva = MiniMax(child, depth+1, alpha, beta, False)
maxEva = max(maxEva, eva)
alpha = max(alpha, maxEva)
if alpha >= beta
break
return maxEva

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 33 of 34


else // for Minimizer player
minEva= +∞
for each child of node do
eva = MiniMax(child, depth+1, alpha, beta, true)
minEva = min(minEva, eva)
beta = min(beta, minEva)
if alpha >= beta
break
return minEva

2023-24 III B.Tech I Semester Unit - II AI Learning Material Page 34 of 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy