A Star Algorithm
A Star Algorithm
Estimated cost of the Cost to reach node n Cost to reach from node
cheapest solution. from start state. n to goal node.
h(n): the estimated (or “heuristic”) distance from current node to goal, it
estimates which node is closest to the goal node. Nodes are visited in this
manner until a goal is reached.
Algorithm A*
1. Initialize: Set OPEN = (s), CLOSED = {}, g(s) = 0, f(s) = h(s)
2. Fail: If OPEN = { }, Terminate & fail
3. Select : Select the minimum cost state, n, from OPEN, Save n in CLOSED.
4. Terminate: If n ∈ G, terminate with success, and return f(n)
5. Expand: For each successor, m, of n If m ∉ [OPEN ∪
CLOSED]
Set g(m) = g(n) + C(n,m)
Set f(m) = g(m) +h(m)
If m ∈ [OPEN ∪ CLOSED]
Insert m in OPEN
Step-1: Define a list OPEN. Initially, OPEN consists of a single node, the
start node S.
Step-2: If the list is empty, return failure and exit.
Step-3: Remove node n with the smallest value of f(n) from OPEN and move it to list CLOSED.
If node n is a goal state, return success and exit.
Step-4: Expand node n.
Step-5: If any successor to n is the goal node, return success and the solution by tracing the path from
goal node to S.
Otherwise, go to Setp-6.
Step-6: For each successor node,
Apply the evaluation function f to the node.
If the node has not been in either list, add it to OPEN.
Step-7: Go back to Step-2
Example 1 Step-1:
We start with node A. Node B and Node F can be reached from node A. A* Algorithm calculates f(B)
and f(F). Estimated Cost f(n) = g(n) +h(n) for Node B and Node F is:
f(B) = 6+8=14
f(F) = 3+6=9
Since f(F) < f(F), so it decides to go to node F.
Closed list (F)
Path- A →F
Step-4: Node E, Node H and Node J can be reached from node I. A* Algorithm
Step-3: Node I can be reached from node G. A* Algorithm calculates f(E), f(H), f(J).
f(E) = (3+1+3+5) + 3 =15
calculates f(I). f(I)=(3+1+3)+1=8; It decides to go to node I. f(H) = (3+1+3+2) + 3 = 12
f(J) = (3+1+3+3) +0 = 10
Closed list (I). Since f(J) is least, so it decides to go to node J.
Closed list (J)
Path- A→F→G→I
Shortest Path - A→F→G→I→J Path Cost is
3+1+3+3=10
Example 2
Steps 5-6:
S→A→C→G=1+1+4=6
Steps 1-4: S→A→B→D=1+2+5+6=14
1. S→A=1+3=4
Steps 7-8:
2. S→G=10+0=10
S→A→C→D→G=1+1+3+2=7
3. S→A→B=1+2+4=7
S→A→B→D→G=1+2+5+2=10
4. S→A→C=1+1+2=4 S→A→C→D=1+1+3+6=11