A* Search Intermediate

A* Pathfinding Visualizer

Explain and visualize how A* finds the shortest path on a grid with walls.

Try it

Cyan = visited · Violet = frontier · White = final path · Click cells to toggle walls (start/end are fixed).

Goal

Explain and visualize how A* finds the shortest path between two points on a grid.

Concept

  • Heuristic function estimates cost from to the goal.
  • g(n) is the actual cost from the start to .
  • A* minimizes in its priority queue.

Manhattan distance is a common admissible heuristic on grids:

A* is optimal when the heuristic is admissible (never overestimates).

How it works

  1. Initialize open set with the start node ().
  2. Pop the node with lowest .
  3. If it is the goal, reconstruct the path via parent pointers.
  4. Otherwise expand neighbors, skip walls, and relax edges when a cheaper is found.
  5. Repeat until the open set is empty (no path) or the goal is reached.

Observations

  • Why A* is optimal: With an admissible heuristic, the first time we pop the goal it has minimum .
  • When it struggles: Dense mazes with a misleading heuristic can explore many nodes before finding the goal.
  • Compared to Dijkstra: A* is Dijkstra + heuristic guidance toward the target.

Source

GitHub — portfolio & experiments

View source on GitHub →