AlgorithmsFoundational

A* Search: Open Set, Closed Set, and f-score

A* finds paths by balancing the cost already paid with a heuristic estimate of what remains.

A*PathfindingHeuristicsAlgorithms
Robotic drive chassis with velocity vectors, a planned trajectory, and fiducial field landmarks
Generated visual worldRobotics & planning

Motion, sensing, control loops, and plans made visible as a field of forces and trajectories.

Interactive model

Frontier expansion on a grid

Switch heuristics to see how the explored cells and final path differ.

Live HTML simulation · adjust the controls and watch the computed output respond.

Interactive

A* expands likely paths first instead of flooding the whole grid

Expanded 36 cells; path contains 14 cells.

This is a simplified teaching model. Its displayed values are computed from the controls; the article explains where the model stops.

Site connection

The path finder project visualizes A* through grid cells, obstacles, heuristics, and the final path.

Implemented: The source documents a pygame grid visualizer with configurable obstacles, taxicab or Euclidean heuristics, visited-cell coloring, and a final path. Its exact priority-queue and tie-breaking implementation are not documented here.

The Mental Model

A* behaves like a careful explorer with a map estimate. It keeps a frontier of possible next cells, scores each candidate, and always expands the candidate that looks cheapest after combining what it already paid with what it still expects to pay.

f(n)=g(n)+h(n)f(n)=g(n)+h(n)

  • g(n)g(n) is the real cost from the start to node nn.
  • h(n)h(n) is the heuristic estimate from node nn to the goal.
  • f(n)f(n) is the priority score used to choose what to expand next.
1. DiscoverPut neighboring cells into the open set with tentative scores.
2. PrioritizePick the open cell with the lowest f-score.
3. CommitMove that cell to the closed set after considering its neighbors.
4. ReconstructWhen the goal is reached, follow parent pointers backward.
Why the heuristic condition mattersIf the heuristic never overestimates the true remaining cost, A* keeps its shortest-path guarantee. If it overestimates, it may become faster but no longer reliably optimal.
Reference table for this concept
ChoiceEffect
Manhattan distanceFits four-direction grid movement and produces rectilinear search pressure
Euclidean distanceFits continuous geometry or diagonal movement better
Zero heuristicDegenerates toward Dijkstra's algorithm
Overconfident heuristicCan be fast but may skip the optimal path

The Score That Drives the Search

A* gives each candidate node an f-score: f(n) = g(n) + h(n).

g(n) is the known cost from the start to the node. h(n) is the estimated cost from the node to the goal. The algorithm repeatedly expands the node with the smallest f-score.

A* is powerful because h(n) gives the search a sense of direction without discarding the actual path cost.

Open and Closed Sets

The open set stores frontier nodes that might still lead to the best path. The closed set stores nodes already expanded.

Walls or obstacles are never expanded. The final path appears when the goal is reached and parent pointers are traced backward.

Reference table for this concept
SetMeaning
OpenCandidates discovered but not fully expanded
ClosedNodes whose neighbors have already been considered
PathParent chain from goal back to start
WallBlocked cell that cannot be traversed

Worked Example

Suppose S connects to A at cost 2 and B at cost 1. A connects to goal G at cost 3; B connects to G at cost 7. With h(A)=3, h(B)=4, and h(G)=0, both A and B initially have f=5.

If a tie-break selects A, expanding it proposes G with g=5 and f=5. Following parent pointers G←A←S reconstructs a path of total cost 5. The example shows that f controls exploration while parents encode the final route; equal f-scores may change expansion order without changing optimal cost under A*'s guarantee conditions.

When a route through the current node lowers a neighbor's g-score, update that score and parent. This relaxation step is essential.

Guarantees, Mechanics, and Limits

A* starts with g(start)=0, repeatedly selects a minimum-f open node, tests it for the goal, and relaxes traversable neighbors using tentative_g=g(current)+edge_cost. Expanded nodes enter the closed set; a correct implementation may reopen one if a later route improves it.

An admissible heuristic never overestimates true remaining cost. A consistent heuristic also satisfies h(n) ≤ c(n,n′)+h(n′), so f does not decrease along an edge and graph search is simpler. With h=0, A* becomes Dijkstra-style uniform-cost search.

The usual model assumes nonnegative edge costs and can consume substantial memory for the frontier, scores, parents, and closed set. The repository describes the teaching visualizer's behavior but not its exact queue structure or tie rule, so those details should not be inferred.

Common Pitfalls

  • Using a heuristic that overestimates and breaks shortest-path guarantees.
  • Forgetting to update a node when a cheaper route is found.
  • Confusing visited cells with the final path.
  • Using Euclidean distance on a grid where only four-direction movement is allowed without thinking through the movement model.

Sources and Further Reading

Related Explainers