You are on page 1of 4

Informed Search A*

Aim:
To write a python program to implement the A* search.

Algorithm-
1. The implementation of A* Algorithm involves maintaining two lists- OPEN and
CLOSED.
2. OPEN contains those nodes that have been evaluated by the heuristic function but have
not been expanded into successors yet.
3. CLOSED contains those nodes that have already been visited.
4. Define a list OPEN.
5. Initially, OPEN consists solely of a single node, the start node S.
6. If the list is empty, return failure and exit.
7. Remove node n with the smallest value of f(n) from OPEN and move it to list CLOSED.
8. If node n is a goal state, return success and exit.
9. Expand node n.
10. If any successor to n is the goal node, return success and the solution by tracing the path
from goal node to S.
11. Otherwise, go to Step-06.
12. For each successor node,
13. Apply the evaluation function f to the node.
14. If the node has not been in either list, add it to OPEN.
15. Go back to Step-02.

class Graph:
def __init__(self,adjac_lis):
self.adjac_lis = adjac_lis
def get_neighbours(self,v):
return self.adjac_lis[v]
def h(self,n):
H={'A': 10,
'B': 8,'C': 5,'D': 7,'E': 3,'F': 6,'G': 5,'H': 3,'I': 1,'J': 0}
return H[n]
def a_star_algorithm(self,start,stop):
open_lst = set([start])
closed_lst = set([])
dist ={}
dist[start] = 0
prenode ={}
prenode[start] =start
while len(open_lst)>0:
n = None
for v in open_lst:
if n==None or dist[v]+self.h(v)<dist[n]+self.h(n):
n=v;
if n==None:
print("path doesnot exist")
return None
if n==stop:
reconst_path=[]
while prenode[n]!=n:
reconst_path.append(n)
n = prenode[n]
reconst_path.append(start)
reconst_path.reverse()
print("path found:{}".format(reconst_path))
return reconst_path
for (m,weight) in self.get_neighbours(n):
if m not in open_lst and m not in closed_lst:
open_lst.add(m)
prenode[m] = n
dist[m] = dist[n]+weight
else:
if dist[m]>dist[n]+weight:
dist[m] = dist[n]+weight
prenode[m]=n
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
open_lst.remove(n)
closed_lst.add(n)
print("Path doesnot exist")
return None
adjac_lis ={'A': [('B', 6), ('F', 3)],
'B': [('C', 3), ('D', 2)],'C': [('D', 1), ('E', 5)],'D': [('C', 1), ('E', 8)],'E': [('I', 5), ('J', 5)],
'F': [('G', 1),('H', 7)] ,'G': [('I', 3)],'H': [('I', 2)],'I': [('E', 5), ('J', 3)],}
graph1=Graph(adjac_lis)
graph1.a_star_algorithm ('A', 'J')

RESULT:
Thus, the python program for implementation of A* is executed and its output is verified.

You might also like