Informed Searching Algorithms-II (A)
Informed Searching Algorithms-II (A)
ALGORITHMS-II
(A*ALGORITHM)
A* ALGORITHM- INTRODUCTION
A* ( pronounced as A- star search) is the most widely and
improved form of the best-first search.
It evaluates each node n using a function f(n) which is
combination of g(n) and h(n).
f(n) = g(n) + h(n)
where g(n) is the actual cost to reach the node n from
starting state through some known path.
h(n) is the estimated distance of the node n from goal node
(computed using heuristic function).
The algorithm also maintains OPEN as a priority queue where
the nodes are prioritized according to their f-values.
Each node in OPEN is maintained as (node, parent, f(node))
A* ALGORITHM- INTRODUCTION
CONTD…..
g(n): estimated cost to h(n): estimated distance of n
reach n from S from a from goal using heuristic
known path function
S n
G
o h(n) is defined by the heuristic function and would never change for any node n. Thus h(n)
is the property of the node.
o g(n) is the estimated cost to reach n from S from a known path. As more and more paths are
learnt, this cost will keep on changing. Thus g(n) changes while the A* algorithm explores
the search space. In general g(n) g*(n) where g*(n) denotes optimized cost to reach n
from S.
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
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
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 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 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.
For the search space shown below, find the optimal path from S
to D using the heuristic values defined in table.
S
Node Heuristic Value
1 S 7
4
A 6
A 2 B B 2
5 2 C 1
12
D 0
D C
3
EXAMPLE 2 – SOLUTION
Iteration II
Remove head node (B,S,4+2) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S ,A and C are produced (for S it is case 3 as
it is already in CLOSED, for A it is case 2 as it is already in OPEN and for C it is case 1 which is
not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 4+4=8;
since newg(S) is not less than g(S), so this successor is ignored
A : newg(A)=g(B)+cost(B,A) = 4+2=6;
since newg(A) is not less than g(A), so this successor is ignored
C: g(C)=g(B) +cost(B,C) = 4 + 2 =6, h(C) =1 f(C)=6+1
Add (C,B,6+1) after (A,S,1+6) to OPEN [node generated before has higher priority in case of
same f values]
Iteration III
Remove head node (A,S,1+6) from OPEN and add to CLOSED.
Since A is not goal node, therefore successors of A i.e. S,B,D and C are produced (for S and B it is case 3 as it is already in
CLOSED, for C it is case 2 which is already in OPEN, and for D it is case 1 as it is not in OPEN or CLOSED)
Successors of A:
S: newg(S) = g(A)+cost(A,S)= 1 + 1 = 2
since newg(S) is not less than g(S), so this successor is ignored
B : newg(B)=g(A)+cost(A,B) = 1+2=3; h(B) = 2 , f(G) = 3+2
since newg(B) is less than g(B), therefore update (B,A,3+2) in CLOSED and propagate improvement to Child C as
(C,B,5+1) in OPEN
C: newg(C)= g(A) + cost (A,C) = 1+5 =6
since newg(C) is not less than g(C), so this successor is ignored
D: g(D) =g(A) +cost(A,D) = 1+12 =13, h(D)= 0, f(D)=13+0
ADD (D,A,13+0) after (C,B,5+1) to OPEN
Iteration IV
Remove head node (C,B,5+1) from OPEN and add to CLOSED.
Since C is not goal node, therefore successors of C i.e. A ,B and D are produced (for A and B it is case 3 as it is already in
CLOSED, for D it is case 2 which is already in OPEN)
Successors of C:
A: newg(A) = g(C)+cost(C,A)= 5 + 5 = 10
since newg(A) is not less than g(A), so this successor is ignored
B : newg(B)=g(C)+cost(C,B) = 5+2=7;
since newg(B) is not less than g(B), so this successor is ignored
D: g(D) =g(C) +cost(C,D) = 5+3 =8, h(D)= 0, f(D)=8+0
UPDATE (D,C,8+0) to OPEN
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.