This project is an interactive visualization of the A* Pathfinding Algorithm, implemented in JavaScript using the p5.js library. It demonstrates how the algorithm finds the shortest path between two points on a grid, navigating around obstacles.
-
Interactive Grid:
- Draw Walls: Click and drag on the grid to create obstacles.
- Randomize: Generate random wall patterns with a single click.
- Clear: Reset the grid to a blank state.
- Real-time Visualization: Watch the algorithm explore nodes (green) and find the optimal path (blue).
- Optimized Performance: Uses a Min-Heap data structure for the open set, ensuring efficient node retrieval ($O(\log N)$) compared to a standard array ($O(N)$).
- Responsive Design: The grid adapts to the canvas size.
-
Open the Application: Simply open
index.htmlin your modern web browser.Alternatively, you can serve it locally:
python3 -m http.server 8000
Then open
http://localhost:8000in your browser. -
Controls:
- Start: Begins the pathfinding visualization.
- Reset Path: Clears the current path and search data, keeping the walls intact.
- Clear Walls: Removes all walls and resets the grid.
- Randomize Walls: Adds random obstacles to the grid.
-
Interact: Click and drag your mouse across the grid to draw custom walls before starting the algorithm.
The
The algorithm minimizes the total estimated cost of a path, defined by the function:
-
$g(n)$ : The actual cost to reach node$n$ from the start node. In this grid, moving to an adjacent cell (up, down, left, right) or a diagonal cell has a cost. -
$h(n)$ : The heuristic estimated cost from node$n$ to the goal. This implementation uses the Euclidean Distance (straight-line distance), which is admissible and consistent, ensuring the shortest path is found. -
$f(n)$ : The total estimated cost of the path through node$n$ .
-
Initialization:
- The algorithm begins by adding the Start Node to the
openSet. - The
openSetis a priority queue (implemented here as a Min-Heap) that orders nodes by their lowest$f(n)$ score.
- The algorithm begins by adding the Start Node to the
-
Selection:
- The algorithm repeatedly extracts the node with the lowest
$f(n)$ score from theopenSet. Let's call this nodecurrent.
- The algorithm repeatedly extracts the node with the lowest
-
Termination Check:
- If
currentis the End Node, the path has been found! The algorithm stops and reconstructs the path by backtracking through thepreviouspointers.
- If
-
Exploration:
- The
currentnode is moved to theclosedSet(conceptually, a set of visited nodes) to avoid re-processing. - The algorithm then looks at all valid neighbors of
current(up, down, left, right, and diagonals).
- The
-
Relaxation:
- For each neighbor:
- If it is a Wall or already in the
closedSet, it is ignored. - A tentative
$g$ score is calculated:tempG = current.g + distance(current, neighbor). - If this path to the neighbor is shorter than any previously found path (or if the neighbor is being visited for the first time):
- Update the neighbor's
$g$ score totempG. - Calculate the neighbor's
$h$ score (distance to goal). - Calculate the neighbor's
$f$ score ($g + h$ ). - Set the neighbor's
previousparent tocurrent. - Add the neighbor to the
openSetif it's not already there.
- Update the neighbor's
- If it is a Wall or already in the
- For each neighbor:
-
No Solution:
- If the
openSetbecomes empty and the End Node has not been reached, there is no possible path (e.g., the target is completely walled off).
- If the
A standard array implementation of the openSet would require scanning the entire array to find the lowest
![]() |
![]() |
![]() |
![]() |



