Definition
A* is an informed search algorithm that finds the shortest path between two nodes in a graph. It uses a heuristic function to estimate the cost from the current node to the goal, prioritizing nodes that appear to be on the shortest path.
Constraints
- Requires an admissible heuristic (never overestimates the true cost)
- Graph edges must have non-negative weights
- Optimal for grids and graphs with consistent heuristics
Step-by-step explanation
Arguments:
- Graph representation -
graph
- Start node -
start
- Goal node -
goal
- Heuristic function -
h(n)
Explanation:
- Initialize open set with start node (f(n) = g(n) + h(n))
- Initialize closed set (empty)
- While open set is not empty:
- Select node with lowest f(n) from open set
- If current node is goal, return path
- Generate current node's neighbors
- For each neighbor:
- Calculate tentative g score
- If better path found, update neighbor's values
- Add to open set if not already there
- Move current node to closed set
Returns:
The shortest path from start to goal (if one exists), or indication that no path exists