A star

Author

Clayton Cafiero

Published

2026-06-01

The \astar algorithm

\astar is a best-first search algorithm that aims to find the shortest path from a start node to a goal node in a graph. It does this by using two main components:

  1. g(x): The actual cost from the start node to the current node x.
  2. h(x): A heuristic estimate of the cost from the current node x to the goal.

The algorithm combines these two values into a total score:

f(x) = g(x) + h(x)

This score, f(x), is used to prioritize which nodes to explore next. \astar always explores the node with the lowest f(x) value, prioritizing paths that are both inexpensive so far and likely to find the goal in fewer steps than would be required without a heuristic.

Dijkstra’s algorithm can be seen as a special case of \astar in which the heuristic h(x) is always zero, meaning Dijkstra’s algorithm only uses the actual cost g(x) to determine the next node to explore. This makes Dijkstra’s algorithm an uninformed search—it explores uniformly without any “guess” or estimate as to how far the goal is.

Where can \astar be used?

An obvious case is route-finding, for example, finding the shortest route between two locations, connected by roads, highways, or paths. But \astar can be used with any problem where we can render the problem as a graph with the objective of finding a shortest route from some starting node to some goal node.

Some examples include:

  • route-finding in Euclidean space or on the surface of the globe (GPS navigation),
  • robotics and autonomous vehicles,
  • logistics and supply chain,
  • routes in telecommunication or computer networks, and
  • many kinds of puzzles and games, such as solving sliding-tile puzzles, video games, etc.

Sometimes it takes a little ingenuity to render a problem as a graph search problem, but if you can render a problem as a graph search problem, \astar may well be the best approach to finding a solution.

What is the heuristic?

\astar does not dictate which specific heuristic should be used. Indeed, different heuristics might be used for different kinds of problems.

We will see several different types of heuristic applied to different kinds of problems.

Once we have the heuristic for our problem instance, we can “plug it into” \astar.

For example, for route-finding we could use Euclidean distance (straight-line distance in flat space) or Haversine distance (great circle distance on a sphere) as an estimate of distance from goal. For other kinds of problems, we might use very different estimates of distance from goal—it really depends on the kind of problem at hand.

A crucial constraint on the heuristic

If our heuristic estimate, h(x), never overestimates the true cost to the goal, \astar will find the optimal solution but often much faster than Dijkstra (in the worst case it will explore as many nodes as does Dijkstra’s). We call such a heuristic—one which never overestimates true cost to the goal—admissible. That is,

h(x) \leq h^{*}(x)

where h^{*}(x) is the true cost of x to the goal.

Steps of \astar

  1. Initialize the open set (priority queue) with the start node and set its cost g(\text{start}) = 0.

  2. While the open set (priority queue) is not empty:

    1. Select the node x from the open set with the lowest f(x) = g(x) + h(x).
    2. If x is the goal, return the path (you’ve found the solution).
    3. For each neighbor of x, compute the tentative cost g(\text{neighbor}) = g(x) + c(x, \text{neighbor}), where c(x, \text{neighbor}) is the cost of reaching the neighbor of x.
    4. If this tentative cost is lower than any previously found, update g(\text{neighbor}) and set the predecessor of the neighbor to x.
    5. Add the neighbor to the open set with its updated f(\text{neighbor}) = g(\text{neighbor}) + h(\text{neighbor}).
  3. If you exhaust the open set without finding the goal, no solution exists.

Summary

  • Dijkstra explores all nodes equally based only on the distance from the start node.
  • \astar uses a heuristic to guide its search, combining both the distance from the start and an estimate of the remaining distance to the goal. This typically makes it more efficient while still guaranteeing the shortest path.

The danger of overestimating the cost in \astar comes from the fact that it can lead the algorithm to make incorrect decisions, potentially resulting in suboptimal solutions or failing to find the shortest path at all. We’ll see more on this shortly when we look at admissibility and consistency (another property of a heuristic) in more detail.

Copyright © 2023–2026 Clayton Cafiero

No generative AI was used in producing drafts of this material. This was written the old-fashioned way. AI was used to rewrite existing pseudocode in LaTeX to produce standalone *.tex files for rendering, and for revisions toward satisfying WCAG 2.1 AA-level accessibility standards as required by UVM policy. It may also have been used to proofread selected human-written prose. Claude 2.1.150 with model Sonnet 4.6. Revisions, if any, were performed by the author. AI was not used in generating any code whatsoever. All code samples and starter code are by the author only.