A* is a pathfinding graph traversal algorithm. The algorithm was first described in 1968 by Stanford researchers Peter Hart, Nils Nilsson and Bertram Raphael.
frontier = PriorityQueue()frontier.put(start, 0)came_from = dict()cost_so_far = dict()came_from[start] = Nonecost_so_far[start] = 0while not frontier.empty(): current = frontier.get() if current == goal: break for next in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current, next) if next not in cost_so_far or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost + heuristic(goal, next) frontier.put(next, priority) came_from[next] = current
Heuristic
A heuristic is a function that estimates the cost from the current node to the goal node.