Chapter 6 · SimulationA* Pathfinding? ← Chapter 6 Find Path Cancel Clear Path Random Walls Empty GridSpeed Slow Normal Fast Ready.Explored0Open0Path– Start Goal Open set Explored Path WallCloseA* PathfindingClick cells to add or remove walls, then run A* to see the explored cells and the shortest path it finds.How to useThe start is the top-left cell and the goal is the bottom-right cell. Clicking a cell toggles a wall; dragging draws or erases several walls at once.Find Path animates the search. Orange cells have been expanded (closed set), light cells are waiting in the open set, and blue cells are the path found.Cancel stops the animation, Clear Path erases the search trace but keeps the walls, Random Walls fills about 20% of the grid with walls, and Empty Grid removes all walls.How A* worksA* combines the cost so far, g(n), with a heuristic estimate of the remaining distance to the goal, h(n). Here the heuristic is the Manhattan distance, which fits because movement is only up, down, left and right.At each step the node with the smallest f(n) = g(n) + h(n) is removed from the open set and expanded. Because the Manhattan distance never overestimates the true remaining cost on this grid (it is admissible and consistent), the path found when the goal is first removed from the open set is a shortest path.ComplexityWith a binary heap the running time is O(E log V); this demo finds the smallest f in the open set with a simple linear scan, which costs O(V²) on a grid of V cells. With h(n) = 0, A* becomes Dijkstra's algorithm and explores many more cells.