0% found this document useful (0 votes)
45 views22 pages

Informed Searching Algorithms-II (A)

The document summarizes the A* search algorithm. A* uses a cost function f(n) = g(n) + h(n) where g(n) is the cost to reach node n from the start and h(n) is a heuristic estimate of the cost to reach the goal from n. It maintains an OPEN list of nodes sorted by f cost. The example problem finds the optimal path from S to G using two different heuristics, with one being admissible and finding the true optimal path.

Uploaded by

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

Informed Searching Algorithms-II (A)

The document summarizes the A* search algorithm. A* uses a cost function f(n) = g(n) + h(n) where g(n) is the cost to reach node n from the start and h(n) is a heuristic estimate of the cost to reach the goal from n. It maintains an OPEN list of nodes sorted by f cost. The example problem finds the optimal path from S to G using two different heuristics, with one being admissible and finding the true optimal path.

Uploaded by

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

INFORMED SEARCH

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

OPEN containing nodes in


increasing order of f(n)
values

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

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
EXAMPLE I
 Consider the following search space with start node S and goal
node G.

100 A 40

S G
50
100 B

Consider following two heuristics defined for the problem:


 Heuristic 1: h1(S)=110, h1(A)= 80, h1(B) =70
 Heuristic2: h2(S)=110, h2(A)= 30, h2(B) =20
Which of the two heuristic is admissible?
Using A* algorithm find the path from S to G using both heuristics and
show that the path found using admissible heuristic is the optimal one?
EXAMPLE 1 – SOLUTION
Node Cost to reach goal from different paths Optimal Cost
S 140, 150 140 = h*(S)
A 40, 250 40 = h*(A)
B 50, 240 50 = h*(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….

 Path using heuristic 1


g(S)=0 , h1(S)=110 ; f(S)= 0+110
Add (S,,0+110) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,,0+110)} {}
Iteration I
Remove head node (S,,0+110) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S, A and B are produced (case
1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+100 =100; h1(A) = 80 , f(A) = 100+80
B : g(B)=g(S)+cost(S,B) = 0+100 =100; h1(B) = 70 , f(B) = 100+70
Iteration OPEN CLOSED
0 {(S,,0+110)} {}
1 {(B,S,100+70)(A,S,100+80)} {(S,,0+110)}
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….

 Path using heuristic 2


g(S)=0 , h2(S)=110 ; f(S)= 0+110
Add (S,,0+110) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,,0+110)} {}
Iteration I
Remove head node (S,,0+110) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S, A and B are produced (case
1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+100 =100; h2(A) = 30 , f(A) = 100+30
B : g(B)=g(S)+cost(S,B) = 0+100 =100; h2(B) = 20 , f(B) = 100+20

Iteration OPEN CLOSED


0 {(S,,0+110)} {}
1 {(B,S,100+20)(A,S,100+30)} {(S,,0+110)}
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 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)}
EXAMPLE 1 – SOLUTION CONTD….

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.

Thus, the path returned by heuristic 2 (admissible heuristic) is optimal.


EXAMPLE 2

 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

g(S)=0 , h(S)=7 ; f(S)= 0+7


Add (S,,0+7) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,,0+7)} {}
Iteration I
Remove head node (S,,0+7) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S i.e. A and B are produced
(case 1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+1 =1; h(A) = 6 , f(A) = 1+6
B : g(B)=g(S)+cost(S,B) = 0+4 =4; h(B) = 2 , f(B) = 4+2

Iteration OPEN CLOSED


0 {(S,,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,,0+7)}
EXAMPLE 2 – SOLUTION CONTD….

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 OPEN CLOSED


0 {(S,,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,,0+7) (B,S,4+2)}
EXAMPLE 2 – SOLUTION CONTD….

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 OPEN CLOSED


0 {(S,,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,,0+7) (B,S,4+2)}
3 {(C,B,5+1) (D,A,13+0)} {(S,,0+7) (B,A,3+2) (A,S,1+6) }
EXAMPLE 2 – SOLUTION CONTD….

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 OPEN CLOSED


0 {(S,,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,,0+7) (B,S,4+2)}
3 {(C,B,5+1) (D,A,13+0)} {(S,,0+7) (B,A,3+2) (A,S,1+6) }
4 {(D,C,8+0)} {(S,,0+7) (B,A,3+2) (A,S,1+6) (C,B,5+1)}
EXAMPLE 2 – SOLUTION CONTD….

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. Therefore
time and space usually grows exponentially.

You might also like