Professional Documents
Culture Documents
INFORMED SEARCH
• All the previous searches have been blind searches .They make no use of any
knowledge of the problem
• For instance, consider a rubik cube which have six faces and each face can be
rotated in three ways (90°, 180°, 270°). Therefore, the each node of rubik has
a branching factor of 18 (6X3).
• If the solution of the rubik cube lies at a depth 20, then nearly 1.25×1025
nodes are generated and searched. A system which can process 1 million
nodes per second will take nearly 40 billion centuries to search the goal node.
• Heuristic functions in searching are human defined functions that is an estimate of the
distance to the goal from the node.
• The idea of using heuristic function is to guide the search, so that it has a tendency to
explore the region leading to the goal.
• A heuristic function is designed to help the algorithm to pick the candidate from the
OPEN list that is most likely to be on the path of goal.
• To incorporate heuristic values in search, the node pair values in the search, the node
pair representation will be augmented with the heuristic value such as
searchNode= (currentState, parentState, heuristicValue)
Heuristic
• There is no general theory for finding heuristics, because every problem is different
• A heuristic may find a sub-optimal solution or fail to find a solution since it uses limited
information
• HEURISTIC INFORMATION: Information about the problem (the nature of the states,
the cost of transforming from one state to another , the promise of taking a certain path,
and the characteristics of the goals).can sometimes be used to help guide the search more
efficiently.
CURRENT ← Remove-Head(OPEN)
Another
YES O left?
NO
4
A
B C
5 7
D E F G
5 7 3 8
Let A is the initial node with heuristic function vale 4 and suppose
the better state is the one where heuristic value is greater than its
parent state.
EXAMPLE – SIMPLE HILL CLIMBING CONTD..
First, successor B is produced by applying an operator. It will be
checked for goal state. If it is yes , it will be reported else its
heuristic function value will be checked. Its heuristic function
value is 5. It is better than parent node. So it will be new state.
Successor C will not be generated.
The successor D is produced after applying an operator on node
B. It will be checked for goal state. If it is yes , it will be reported
else its heuristic function value will be checked. Its heuristic
function value is 5. It is equal to parent node. So, the state will be
rejected.
Another successor E is produced after applying another operator
on node B. It will be checked for goal state. If it is yes , it will be
reported else failure will be returned.
If goal state of the problem is G, then simple hill climbing will
not produce the solution with the selected heuristic function.
Thus, the algorithm will find the goal node depending upon the
heuristic function. One heuristic may find it and other may not.
(It is depicted in the example given in next slide)
PROBLEM 1
Find the solution to the following blocks world
problem using simple hill method. State the
heuristics used.
A D
D C
C B
B A
Step 2: II state
HF = 1(A)-1 (B) +1(C) -1 (D) =0
C
B A D
Again HF(III)< HF(I) and no other operator is possible on state I. Therefore, return
fail.
The contents of OPEN and CLOSED at each step according to first
heuristic is:
Step OPEN CLOSED
1 {(S,Φ,0)} { }
2 {(1,S,2)} {(S,Φ)}
3 {} {(S,Φ),(1,S)}
SOLUTION-PROBLEM 1 CONTD…
C
D B A
HF(II) = 0 (A) -0 (B) -1 (C) -0 (D) = -1
HF(II) > HF(I). Therefore II state is current state and an
operator is applied on it.
SOLUTION-PROBLEM I CONTD….
Step 3: III state
C D B A
D C A
2 3 1 2 3
1 8 4 8 4
7 6 5 7 6 5
Step 2: I state:
2 3
1 8 4
7 6 5
HF = 1 (for 1) + 1 (for 8) =2
HF(I) < HF (initial state) . Therefore I state, is current state and new state is
produced after applying a valid operator.
SOLUTION- PROBLEM II CONTD….
Step 3: II State:
1 2 3
8 4
7 6 5 HF = 1 (for 8) = 1
HF(II) < HF(I) . Therefore II state, is current state and new state is produced after
applying a valid operator.
Better YES
than Add NEXT to OPEN
current
NO
4
A
B C
5 7
D E F G
5 7 3 8
Let A is the initial node with heuristic function vale 4 and suppose
the better state is the one where heuristic value is greater than its
parent state.
EXAMPLE – STEEPEST HILL CLIMBING CONTD..
First, successors B and C will be produced by applying
both operators. C has a higher value of heuristic
function. So it will be selected. It will be checked for
goal state. If it is yes , it will be reported else C will be
set as new current state.
The successor F and G are produced after applying
operators on node C. G has higher value of heuristic
function. So it will be selected. It will be checked for
goal state. If it is yes , it will be reported else failure
will be reported.
If goal state of the problem is D or E, then steepest
hill climbing will not produce the solution with the
selected heuristic function.
PROBLEM - I
Solve the following 8-puzzle problem using steepest hill
climbing problem. State the heuristic used.
2 3 1 2 3
1 8 4 8 4
7 6 5 7 6 5
2 3
(A,Φ
Φ,3) 1 8 4 Step 1:OPEN={(A,Φ
Φ,3)} CLOSED ={ }
7 6 5
(E,B,1)
1 2 3 Step 3: OPEN={(E,B,1)} CLOSED=((A,Φ
Φ),(B,A)}
8 4
7 6 5
(F,E,0) (G,E,2)
1 2 3 1 2 3
Step 4: OPEN={(F,E,0)} CLOSED=((A,Φ
Φ),(B,A)(E,B)}
8 4 7 8 4
Step 5: OPEN={ } CLOSED=((A,Φ
Φ),(B,A)(E,B)(F,E)}
7 6 5 6 5
PROBLEM- II
Find the solution to the following blocks world
problem using steepest hill method. State the
heuristics used.
C
A B
C B A
Let heuristic be :
+ score for block which is resting on the current
support structure and that score is equal to
number of blocks below it.
- score for block which is resting on the incurrent
support structure and that score is equal to
number of blocks below it.
SOLUTION- PROBLEM -II
HF= -1-0-0 = -1 A State 1
C B
B
A
HF=0 State 2 HF= -3
C B A C
A B B C C
C B A C A C A B A B
HF= -1 HF= 1
State 3 HF= -1 HF= -1 HF= -1
C
B
A State 4
SOLUTION- PROBLEM -II
The contents of OPEN and CLOSED are as
follows:
Time Complexity:
In hill climbing algorithm, each node generate at the
most b nodes.
Therefore, the time complexity grows linearly and is of
the order O(bd) where b is the branching factor and d is
the depth at which solution lies.
Best-First Search
• Depth-first search: not all competing branches
having to be expanded.
28
Best-First Search
A A A
B C D B C D
3 5 1 3 5
E F
4 6
A A
B C D B C D
5 5
G H E F G H E F
6 5 4 6 6 5 6
I J
2 1
29
Best-first Vs Hill Climbing
hill climbing best-first search
• In hill climbing, one move • one move is selected, but
is selected and all the others the others are kept around so
are rejected and are never that they can be revisited
reconsidered. later if the selected path
becomes less promising
• Once the goal is found the parent link will make it possible to
recover the path to the goal.
33
BEST FIRST SEARCH-ALGORITHM
OPEN- Priority Queue with generated nodes of the form
(Node,Parent,Heuristic) sorted on the values of Heuristic.
CLOSED- Queue of expanded nodes; initially empty
ALGORITHM
Insert the start node in OPEN
Loop until success is returned or OPEN is empty
Node (N,P,HN)← Remove-Head(OPEN) and add it to CLOSED
If N is goal node return success & path to N
Else
Generate NEW successors of N, append it to OPEN
and sort the list in order of heuristic values
i.e. OPEN←sorth(append(NEW,OPEN))
where NEW= {MOVEGEN(N)\{OPEN∪CLOSED}}
End If
End Loop
Best-First Search
• Best-First Search: use a heuristic function of following form:
f(n) = g(n) + h(n)
f(n) = measures the value of the current state (its “goodness”).
h(n) = the estimated cost of the cheapest path from node n to a goal state.
g(n) = the exact cost of the cheapest path from the initial state to node n.
34
Greedy Best First Search
• Greedy search:
f(n)=h(n) ; i.e. g(n) = 0. it means the estimated
cost of the cheapest path from node n to a goal
state.
Neither optimal nor complete
35
Example-1
f(n) = g(n) + h (n)
Start g(n): is the exact cost to reach node n from the
A 75
118 initial state.
B State Heuristic: h(n)
C 140
111 A 366
E
B 374
D 80 99
C 329
G F D 244
97 E 253
H F 178
211
101 G 193
I H 98
Goal
I 0
36
Greedy Best First Search
This search ignore g(n), hence
f(n) = h (n) = straight-line distance heuristic
Start
A
B
C State Heuristic: h(n)
E A 366
D B 374
C 329
G F
D 244
E 253
H
F 178
G 193
I
Goal H 98
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
38
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
39
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
40
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
41
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
42
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
43
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
44
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
45
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
46
Greedy Best First Search State h(n)
A 366
B 374
Start C 329
A
D 244
E 253
F 178
G 193
H 98
I 0
47
Greedy Search: Tree Search State h(n)
A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C
[253] E F 178
G 193
H 98
I 0
48
Greedy Search: Tree State h(n)
Search A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C
[253] E F 178
G 193
[193] H 98
G F [178]
[366] A I 0
49
Greedy Search: Tree Search
State h(n)
A 366
Start
A B 374
C 329
[329] [374] B
C D 244
[253] E E 253
F 178
[193] [178] G 193
G F
[366] A H 98
I 0
[253] I [0]
E
Goal
50
Greedy Search: Tree Search State h(n)
A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C
[253] E F 178
G 193
[193] H 98
G F [178]
[366] A I 0
[253] I [0]
E
Goal
Exact cost to reach to goal: dist(A-E-F-I) = 140 + 99 + 211 = 450 51
Greedy Best First Search :
Optimal ? NO
Start State Heuristic: h(n)
A
A 366
C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
dist(A-E-G-H-I) =140+80+97+101=418 52
Greedy Best First Search :
If for some reason, lets say straight distance from C is 250, then what happened?
Also suppose we do not maintain CLOSE list
C
B B 374
** C 250
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
53
Greedy Search: Tree Search
State h(n)
Start A 366
A B 374
C 250
D 244
E 253
F 178
G 193
H 98
I 0
54
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C
C 250
[253] E D 244
E 253
F 178
G 193
H 98
I 0
55
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C C 250
[253] E D 244
E 253
[244] D
F 178
G 193
H 98
I 0
56
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C
C 250
[253] E D 244
[244] D E 253
F 178
Infinite Branch !
G 193
[250] C
H 98
I 0
57
Greedy Search: Tree Search
Start State h(n)
A
A 366
[250] [374] B B 374
C
[253] E C 250
D 244
[244] D
E 253
Infinite Branch ! F 178
[250] C G 193
Not Complete H 98
I 0
[244] D
58
Greedy Search: Time and Space
Complexity ?
Start
A
• Greedy search is not optimal.
B
C
• Greedy search is incomplete
E
without systematic checking of
D
repeated states.(it means, if we do
G F not maintain close list.
• In the worst case, the Time and
H
Space Complexity of Greedy
I
Search are both O(bm)
Goal Where b is the branching factor and m the maximum
path length 59
PROBLEM - I
Solve the following 8-puzzle problem using Best First
Search. State the heuristic used.
2 3 1 2 3
1 8 4 8 4
7 6 5 7 6 5
2 3
(A,Φ
Φ,3) 1 8 4
7 6 5
(E,B,1)
1 2 3
8 4
7 6 5
(F,E,0) (G,E,2)
1 2 3 1 2 3
8 4 7 8 4
7 6 5 6 5
PERFORMANCE OF BEST FIRST SEARCH
Completeness
The best first search is complete, at least for finite search space.
This is because it explores the nodes in search space until goal
is found or OPEN is empty. The only difference is it explore
nodes in order of their heuristic value.
For infinite search space, the completeness of best first search
depends upon the heuristic function. If the heuristic function is
good enough, the search will end up in goal state.
Optimality
The optimality of the best first algorithm also depends upon the
heuristic function.
The heuristic function may find the optimal path or sub optimal
path to problem.
If two nodes have same heuristic value but one node is
expensive to achieve, the best first search algorithm has no way
of discriminating it.
PERFORMANCE OF BEST FIRST SEARCH CONTD…
Space Complexity
The OPEN (search frontier) for the Best First Search
depends upon the accuracy of heuristic function.
If the heuristic function is accurate then the search will
find goal directly, and the OPEN will grow linearly.
Otherwise, the search algorithm may go down some path,
change its mind, and move to another branch in search
tree.
For most interesting problems, it is difficult to devise
accurate heuristic functions, and thus space grows
exponentially (depicted in the figure in next slide).
Time Complexity
Like space complexity, time complexity is also dependent
on the accuracy of heuristic function and time tends to
grow exponentially for most of the problems.
A* Algorithm
• A* ( pronounced as A- star search) is the most widely and improved
form of the best-first search.
g(n)
f(n) = g(n)+h(n) n
h(n)
2
OPTIMALITY OF A* ALGORITHM
The necessary conditions for A* algorithm to be
optimal is:
1) The branching factor of the search space is finite.
2) The cost to move from one node to another should be
greater than or equal to zero i.e. cost(i,j) ≥ 0 for all i, j.
3) The heuristic is an admissible heuristic. An
admissible heuristic h(n) is a heuristic that
always underestimates the cost to reach the goal
i.e. h(n) ≤ h*(n) where h*(n) is the optimized cost
to reach goal from n.
In other words, for every node n, the cost to reach the
goal provided by the heuristic function is never greater
than the optimized cost to reach the goal.
If the above mentioned conditions are not satisfied
then the algorithm is A algorithm but not A* (* for
any search algorithm denotes optimal algorithm).
A* ALGORITHM PSEUDOCODE
Insert start node (S,φ,f(S)) to OPEN
Loop until OPEN is empty or success is returned
(n,p,f(n)) ← remove-head(OPEN) and add it CLOSED
If n is a goal node return success and path to n.
else
successors ← MOVEGEN(n)
for each successor m do
switch case
case 1: if m ∉ OPEN and m ∉ CLOSED
compute g(m) = g(n) + cost (n,m) and h(m)
compute f(m)=g(m) + h(m)
add (m,n,f(m)) to OPEN according to priority of f(m)
case 2: if m ∈ OPEN
compute newg(m)=g(n)+cost(n,m)
if newg(m)<g(m) then
update g(m) =newg(m) and f(m)=g(m)+h(m)
update (m,n,f(m)) to OPEN according to priority of f(m)
end if
case 3: if m ∈ CLOSED
compute newg(m)=g(n)+cost(n,m)
if newg(m)<g(m) then
update g(m) = newg(m) and f(m)=g(m)+h(m)
update (m,n,f(m)) to CLOSED according to priority of f(m)
propogateimprovement(m)
end if
end for
end if
end loop
A* ALGORITHM PSEUDOCODE CONTD….
propagateimprovement(m)
for each successor s of m
compute newg(s)=g(m) + cost(m,s)
if newg(s) < g(s) then
update g(s)=newg(s)
update f(s)=g(s) + h(s)
update (s,m,f(s)) in OPEN or CLOSED
if s ∈ CLOSED then
propogateimprovement(s)
end if
end if
end for
A* Search
Start State Heuristic: h(n)
A 75
118 A 366
140 B B 374
C
111 C 329
E
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = g(n) + h (n)
g(n): is the exact cost to reach node n from the initial state. 3
A* Search: Tree Search
State h(n)
A 366 A Start
B 374
C 329
D 244
E 253
F 178
G 193
H 98
I 0
4
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
E 253
F 178
G 193
5
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
H 98
I 0
6
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
7
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
8
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
9
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
10
A* Search: Tree Search
State h(n)
A 366 A Start
B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253
11
EXAMPLE I
Consider the following search space with start node S
and goal node G.
100 A 40
S G
50
100 B
As given,
Heuristic 1: h1(S)=110, h1(A)= 80, h1(B) =70
Heuristic 2: h2(S)=110, h2(A)= 30, h2(B) =20
For every node, heuristic 2 underestimates the optimal cost to reach
goal whereas heuristic 1 is overestimating the optimal cost for nodes A
and B.
Therefore, heuristic 2 is admissible and heuristic 1 is not
EXAMPLE 1 – SOLUTION CONTD….
Iteration II
Remove head node (B,S,100+70) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 1 which is not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 100+100 =200;
since newg(S) is not less than g(S), so this successor is ignored
G: g(G)=g(B)+cost(B,G) = 100+50=150; h1(G) = 0 , f(G) = 150 + 0
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+70)(A,S,100+80)} {(S,φ,0+110)}
2 {(G,B,150+0)(A,S,100+80)} {(S,φ,0+110) (B,S,100+70)}
EXAMPLE 1 – SOLUTION CONTD….
Iteration III
Remove head node (G,B,150+0) from OPEN and add to CLOSED.
Since G is goal node, therefore algorithm will stop and path
S→B → G is returned with path cost 150.
EXAMPLE 1 – SOLUTION CONTD….
Iteration II
Remove head node (B,S,100+20) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 1 which is not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 100+100 =200;
since newg(S) is not less than g(S), so this successor is ignored
G : g(G)=g(B)+cost(B,G) = 100+50=150; h2(G) = 0 , f(G) = 150 + 0
Add (G,B,150+0) to OPEN after (A,S,100+30)
Iteration III
Remove head node (A,S,100+30) from OPEN and add to CLOSED.
Since A is not goal node, therefore successors of A i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 2 which is already in OPEN)
Successors of A:
S: newg(S) = g(A)+cost(A,S)= 100 + 100 = 200
since newg(S) is not less than g(S), so this successor is ignored
G : newg(G)=g(A)+cost(A,G) = 100+40=140; h2(G) = 0 , f(G) = 140 + 0
since newg(G) is less than g(G), therefore update (G,A,140+0) in
OPEN
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+20)(A,S,100+30)} {(S,φ,0+110)}
2 {(A,S,100+30) (G,B,150+0)} {(S,φ,0+110) (B,S,100+20)}
3 {(G,A,140+0)} {(S,φ,0+110) (B,S,100+20) (A,S,100+30) }
EXAMPLE 1 – SOLUTION CONTD….
Iteration IV
Remove head node (G,A,140+0) from OPEN and add to CLOSED.
Since G is goal node, therefore algorithm will stop and path
S→A → G is returned with path cost 140.
S
Node Heuristic Value
1
4 S 7
A 6
A 2 B B 2
5 2 C 1
12
D 0
D C
3
EXAMPLE 2 – SOLUTION
Iteration V
Remove head node (D,C,8+0) from OPEN and add to CLOSED.
Since D is goal node, therefore algorithm will stop and path
S→A → B → C → D is returned with path cost 8.
PERFORMANCE OF A*
Completeness and Optimality:
For finite, positive path costs, and admissible heuristics
A* algorithm is always complete and optimal.
Space and Time Complexity
Depends upon heuristic function.
For most of the problems, heuristics are never perfect.
There time and space usually grows exponentially.