Program 1
Program 1
class Graph:
# example of adjacency list (or rather map)
# adjacency_list = {
# 'A': [('B', 1), ('C', 3), ('D', 7)],
# 'B': [('D', 5)],
# 'C': [('D', 12)]
#}
return H[n]
g[start_node] = 0
if n == None:
print('Path does not exist!')
return None
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start_node)
reconst_path.reverse()
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
adjacency_list = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjacency_list)
graph1.a_star_algorithm('A', 'D')
Output:
Path found: ['A', 'B', 'D']
['A', 'B', 'D']