The A* (A-star) algorithm is a pathfinding and graph traversal algorithm, widely used in computer science to find the shortest path between nodes in a graph. It combines features of Dijkstra's algorithm (which is the best for finding the shortest path) and the Greedy Best-First-Search (which is fast at finding a path but doesn't guarantee the shortest path) to efficiently compute the shortest path.
A* uses a best-first search and finds the least-cost path from a given initial node to one goal node (out of one or more possible goals). It uses a heuristic function to prioritize which paths to explore. Along with the actual cost from the start node to the current node (denoted as g(n)), A* also maintains a heuristic estimate of the cost to get from that node to the goal (denoted as h(n)). The function f(n) = g(n) + h(n) gives the estimated total cost of the path through node n to the goal.
Here's some sudo code (pseudocode) for the A* algorithm:
python
function A*(start, goal, h)
// The open set, start with the beginning node
openSet = PriorityQueue()
openSet.insert(start, 0)
// This map holds the cost of the cheapest path from start to n currently known.
gScore = map with default value of Infinity
gScore[start] = 0
// For each node, which node it can most efficiently be reached from.
cameFrom = an empty map
// For each node, the cost of getting from the start node to the goal
// by passing by that node. That value is partly known, partly heuristic.
fScore = map with default value of Infinity
fScore[start] = h(start)
while openSet is not empty
current = openSet.removeMin() // Remove the node with the lowest fScore
if current is goal
return reconstruct_path(cameFrom, current)
for each neighbor of current
// Tentative gScore is the known gScore plus the cost from current to neighbor
tentative_gScore = gScore[current] + dist_between(current, neighbor)
if tentative_gScore < gScore[neighbor]
// This is a better path to neighbor
cameFrom[neighbor] = current
gScore[neighbor] = tentative_gScore
fScore[neighbor] = gScore[neighbor] + h(neighbor)
if neighbor is not in openSet
openSet.insert(neighbor, fScore[neighbor])
// If we get here, then there was no path
return failure
function reconstruct_path(cameFrom, current)
total_path = [current]
while current in cameFrom.Keys
current = cameFrom[current]
total_path.append(current)
return total_path.reverse()
In this sudo code, h(n) is the heuristic function for node n, gScore represents the cost of the cheapest path from start to n currently known, and fScore represents our current best guess as to how short a path from start to finish can be if it goes through n.
Remember that the heuristic function h(n) is problem-specific. A common choice for h(n) in a grid-based pathfinding context is the Manhattan distance or the Euclidean distance if diagonal movement is allowed.
Also, note that the PriorityQueue is assumed to order its elements by fScore value, and dist_between(current, neighbor) is assumed to return the actual distance between the current node and the neighbor node.