# Machine Learning A* Algorithm | Read Now

MACHINE LEARNING VTU LAB

Implement A* Search algorithm

A* Algorithm program Code

```class Graph:
def get_neighbours(self,v):
def h(self,n):
H={'A':1,'B':1, 'C':1,'D':1}
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:
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)
`path found:['A', 'B', 'D']`
`['A', 'B', 'D']`