You are on page 1of 3

Experiment No.

Code:
graph={

'S':[('1',3,24),('4',4, 22)],

'1':[('2',1,20),('4',5,22)],

'2':[('3',4,18),('5',5,20)],

'3':[],

'4':[('1',5,24),('5',2,20)],

'5':[('2',5,20),('6',4,20)],

'6':[('7',3,0)],

'7':[]}

def astar(start,stop,openList=[],closedList=[], costs=0):

if start not in closedList:

closedList.append(start)

for neighbor in graph[start]:

if neighbor[0] not in closedList:

openList.append(neighbor)

openList.sort(key=lambda neighbor:neighbor[1]+neighbor[2])

if openList[0][0]==stop:

costs+=openList[0][1]+openList[0][2]
closedList.append(openList[0][0])

print("Visited Nodes= ",closedList,"\ncost= ",costs)

else:

value=openList[0]

closedList.append(value[0])

costs+=value[1]

openList.remove(value)

astar(value[0],stop,openList,closedList,costs)

astar('S','7')

output:

You might also like