(AI) Searching
(AI) Searching
Simple Idea:
Begin
depth
1. Place the starting node in a queue
0
2. repeat 1
Delete queue to get the front element
Else 5 6 7 8 2
insert the children of the first
element, if exists in any order at
the rear end of the queue.
9 10 11 12 3
UNTIL the queue is empty;
Breadth first search on a tree, where the node number denotes the order of visiting that node
END
Breadth First Search
Time complexity
largest depth = d
Space complexity
Each node that is generated must remain in memory, so, the space
complexity is same as that of the time complexity (plus one node
for the root)
Optimality
The path cost is usually taken to be the sum of the step costs.
B C
15 13 4 3
D E F H
12 10 18 6 6 3 1 7
I J G1 K L M N G2
Uniform cost Search
Example Contd… A
11
4
B C
4 11
Since B has the least cost we expand it
A
4 11
B C
15 13 11
D E Of our three choices, C has the least cost so we
19 17 will expand it.
Uniform cost Search
Example Contd…
A
4 11
B C
15 13 4 3
D E F H
17 19 15 14
Node H has the least cost thus far, so we expand it.
Uniform cost Search
Example Contd… A
4 11
B C
15 13 4 3
D E F H
17 19 15 1 7
N G2
15 21
We have a goal, G2 but need to expand other branches to see if there is another goal with
less distance.
Uniform cost Search
Example Contd… A
4 11
B C
15 13 4 3
D E F H
17 19 6 15 3 1 7
L M N G2
21 18 15 21
Both nodes F and N have a cost of 15, we chose to expand the leftmost node first. We
continue expanding until all remaining paths are greater than 21, the cost of G2.
7
Depth First Search
Simple Idea:
Begin
depth
1. Push the starting node at the stack, pointed
to by the stack-top; 0
2. While the stack is not empty do
1
Begin
Else 4 5 9 10 2
Push the children at the stack-
top element in any order into the
stack;
6 7 11 12 3
End While
Depth first search on a tree, where the node number denotes the order of visiting that node
End
Depth First Search
Time complexity
Space complexity
Optimality
Time complexity:
Node Generation:
level d: once
level d-1: 2
level d-2: 3
...
level 2: d-1
level 1: d
– Total no. of nodes generated:
d.b +(d-1). b + (d-2). b + .......................+1. bd= O(bd)
2 3
Iterative Deepening Depth First
Search
Iterative Deepening Search evaluation:
Space complexity:
It needs to store only a single path from the root node to a leaf
node, along with remaining unexpanded sibling nodes for each
node on the path.
Optimality:
Completeness: Yes
Backward reasoning
The most promising node is then expanded and the fitness of all the
newborn children is measured.
Kupondole
Sanepa Patan
140 0
Thapathali Sanepa
600
Bakhundole 50
A* Search
Kupondole
Sanepa Patan
140+50+50=240 0+30+50+60=140
Thapathali Sanepa
Use Best First Search and A* Search to reach Bucharest from Lugoj.
Hill Climbing Search
After f(x) is evaluated at the possible initial nodes x, the nodes are sorted
in ascending order of their functional values and pushed into a stack in
the ascending order of their ‘f’ values.
It is now popped and compared with the goal. If it is not the goal then
then it is expanded and f is measured for each of its children. They are
now sorted according to their ascending order of the functional values
and then pushed into the stack. If the stack-top element is the goal, the
algorithm exits; otherwise the process is continued until the stack
becomes empty. Pushing the sorted nodes into the stack adds a depth
first flavor to the present algorithm.
Hill Climbing Search
Algorithm
Begin
2. Repeat
Fig: Moving along a ridge in two
Pop stack to get the stack-top element; steps (by two successive operators)
If the stack-top element is the goal,
announce it and exit in hill climbing
Else push its children into the stack in
the ascending order of their f values;
End
Hill Climbing Search
Example: Block
+1 A +5
World Problem A +1 D
Heuristic
Start B D Goal+1 B
Functions: C E +1 C +1 E
1. h1(n) = add 1 if
block on a
correct block. -1 D
+3 A +1 +1 A +1 -1 A
Subtract 1 if it is
-1
+1
on a wrong +1 B D -1 +1 B -1 D +1 B +1 B
block. C E C E A E E D
+1 +1 +1 +1 -1 +1 C +1 +1 C +1 -1
a b c d
According to the given heuristic function, the heuristic value of the different states are as
indicated in the figure.
In the above figure we see that, state (a) has maximum value of the heuristic function. So, (a) is
chosen for further exploration.
Hill Climbing Search
Example: +3 A +1 +1 A +5
+1 B D -1 +1 D
Heuristic
Functions: +1 C E +1 Goal+1 B
a +1 C +1 E
1. h1(n) = add 1 if
block on a
correct block. -1 B +1
-1 A +1 +1 +1 +1 A +1 A
Subtract 1 if it is
on a wrong +1 B -1 D +1 B -1 D -1 D -1 D
block. +1 C +1 E +1 C +1 E -1 A +1 C +1 E +1 C +1 E -1 B
a11 a12 a13 a14
In the above figure we see that, all states has same value of the heuristic function. Suppose
we choose a13 for further exploration. Then we get the following (shown in next slide):
Hill Climbing Search
Example: B +1 A
+1 +1
-1
+5
A +1 D
Heuristic
Functions: -1 D Goal+1 B
+1 C +1 E +1 C +1 E
1. h1(n) = add 1 if a13
block on a
correct block.
-1 C
Subtract 1 if it is -1 B
on a wrong -1 +1 +3
block.
A A +1 +1 A +1
-1 D +1 B D -1 -1 D
+1 E +1 C E +1 +1 C +1 E -1 B
a131 a132 a133
In the above figure we see that, state (a132) has maximum value of the heuristic function. So, (a132)
will be chosen for further exploration. But by inspection, we see that (a132) will again generate the
same state as before (shown in previous slide), i.e. we are stucked in a loop without reaching to the
goal.
Hill Climbing Search
Example: +4 A +11
Heuristic A +3 D
Functions: Start B D Goal+2 B
2. h2(n) = add 1 C E +1 C +1 E
for every block
in a correct
structure that
the block is -4 D
sitting on. -1 A -3 +1 A -3 -3 A
Subtract 1 for
-3
0
every wrong
+2 B D -2 +2 B -2 D +2 B +2 B
block. +1 C E +1 +1 C +1 E -1 A +1 C +1 E +1 C +1 E -1 D
a b c d
According to the given heuristic function, the heuristic value of the different states are as
indicated in the figure.
In the above figure we see that, state (b) has maximum value of the heuristic function. So, (b) is
chosen for further exploration.
Hill Climbing Search
Example: +1 +4 A +11
Heuristic
+2 B -2 D +3 D
Functions: C E A
+1 +1 -1
Goal+2 B
2. h2(n) = add 1
for every block
b +1 C +1 E
in a correct
structure that
the block is
sitting on.
-1 -1
-3 A +3 D +6 -3 A +1 -2
Subtract 1 for +2 B D
every wrong
-2 +2 B +2 B -2 D +2 B D
block. +1 C E +1 +1 C +1 E -1 A +1 C +1 E +1 C +1 E -1 A
b11 b12 b13 b14
In the above figure we see that, state (b12) has maximum value of the heuristic function. So, (b12)
will be chosen for further exploration.
Hill Climbing Search
Example: +3 D +6 +4 A +11
Heuristic +2 B +3 D
Functions:
+1 C +1 E -1 A Goal+2 B
2. h2(n) = add 1
for every block b12 +1 C +1 E
in a correct
structure that
the block is +4 A
-1
sitting on.
+3 D +11 +1 +3 D +1 -2
Subtract 1 for +2 B A
every wrong
+2 B -2 D +2 B -2 +2 B D
block. +1 C +1 E +1 C +1 E -1 A +1 C +1 E +1 C +1 E -1 A
b121 b122 b123 b124
In the above figure we see that, state (b121) has maximum value of the heuristic function and it
also matches with the goal state. Hence, the algorithm stops.
Hill Climbing Search
Reaching a Plateau. Flat area of the search space in which a whole set
of neighbouring states have the same value. It is very difficult to find
by local comparisons, the best direction for move.