(R20) AI UNIT-2 srgec
(R20) AI UNIT-2 srgec
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
Iterative deepening
Depth First Search
Bidirectional Search
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.
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
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
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
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
B C
D E F G
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).
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
Initially 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
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
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
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
Q
10 8
W E
2 4 5 6
R T Y Z
2 1 3 4
X C V B
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
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
X C
V B
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.
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
A
Initially
B C
D F
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.
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.
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
SAB f(B) = 3 + 4 7
SAC f(C) = 2 + 2 4
SACD f(D) = 8 + 6 14
SACG f(G) = 6 + 0 6
ACG f(G) = 6 + 7 13
ACH f(H) = 5 + 2 7
ACI f(I) = 8 + 0 8
ACJ f(J) = 10 + 4 14
ACHI f(I) = 6 + 0 6
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
ABD f(D) = 20 + 5 25
ABE f(E) = 26 + 2 28
AC D f(D) = 17 + 5 22
AC E f(E) = 21 + 2 23
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.
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
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
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
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
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
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
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
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
α = -∞ α=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
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
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