function AStar(Graph, start, goal):
openSet = {start}
cameFrom = empty map
for each v in Graph:
g[v] = ∞; f[v] = ∞
g[start] = 0
f[start] = h(start)
while openSet not empty:
current = argmin_{n in openSet} f[n]
if current == goal: return RECONSTRUCT(cameFrom, current)
remove current from openSet
for each neighbor in neighbors(current):
tentative = g[current] + cost(current, neighbor)
if tentative < g[neighbor]:
cameFrom[neighbor] = current
g[neighbor] = tentative
f[neighbor] = tentative + h(neighbor)
add neighbor to openSet
return failure
# A* is almost always implemented iteratively using a priority queue (min-heap).
# Recursive variants exist but are less common in practice and teaching.