A modern, interactive Pathfinding Algorithm Visualizer developed in Python using Pygame, designed to demonstrate and compare the execution of A* (A-Star) and Dijkstra's Algorithm on customizable grid-based environments.
Users can design custom grid environments by placing start and goal nodes, drawing obstacles, and selecting either A* or Dijkstra's Algorithm to observe its behavior in real time. The application visualizes each stage of the search process, including frontier expansion, explored nodes, and the reconstructed shortest path, while reporting execution runtime and the number of explored nodes for algorithm comparison. This makes it a practical educational tool for understanding shortest-path algorithms and their performance characteristics.
- Controls
- Theoretical Background
- Algorithm Comparison
- Features
- Project Structure
- Architecture
- Installation & Setup
- Configuration Guide
- Potential Enhancements
- License
| Input | Action |
|---|---|
| ๐ฑ๏ธ Left Click | Place the Start Node, then the End Node, followed by Wall/Barrier nodes. |
| ๐ฑ๏ธ Right Click | Reset the selected node to its default state. |
| โจ๏ธ A | Run A* Algorithm |
| โจ๏ธ D | Run Dijkstra's Algorithm |
| โจ๏ธ C | Clear the grid, removing all nodes, barriers, and previous search results. |
The visualizer demonstrates the contrast between Informed (Heuristic) Search and Uninformed Search on a 2D grid network where each node connects up to 4 neighbors (orthogonal movement only).
The A* algorithm determines the shortest path by evaluating nodes based on a combination of the actual distance from the starting node and an estimated distance to the target. It minimizes the total cost function:
Where:
-
$g(n)$ : The exact cost of the path from the starting node to node$n$ . On our uniform grid, each step represents a unit step cost of$+1$ . -
$h(n)$ : The heuristic function estimating the cost to reach the target from node$n$ . -
$f(n)$ : The total estimated cost of the cheapest path through node$n$ .
Because diagonal movement is disabled in this grid implementation, the Manhattan Distance (
Dijkstra's Algorithm is a special case of A* where the heuristic component is completely omitted (
Dijkstra's explores the grid uniformly in all directions, creating concentric wave-like patterns of "closed" nodes. While it guarantees the absolute shortest path, it does so at the cost of high spatial exploration, visiting significantly more nodes than A*.
| Metric | A* (A-Star) Algorithm | Dijkstra's Algorithm |
|---|---|---|
| Search Category | Informed (Heuristic) Search | Uninformed (Non-Heuristic) Search |
| Heuristic Function | Manhattan Distance ( |
None ( |
| Time Complexity |
|
|
| Space Complexity | ||
| Exploration Style | Directional, guided towards the target | Concentric radial expansion |
| Optimality | Guaranteed (with admissible heuristic) | Guaranteed for non-negative weights |
| Nodes Explored | Minimal (highly optimized spatial efficiency) | Maximal (exhaustively searches surrounding space) |
While A* and Dijkstra's Algorithm both have a worst-case time complexity of
$O(E \log V)$ , A* typically performs significantly better in practice. Its heuristic guides the search toward the target, resulting in fewer explored nodes, lower execution time, and a more efficient search than Dijkstra's uninformed exploration.
-
๐ฎ Interactive Grid Editing: Place start and goal nodes, draw obstacles, and modify the grid in real time using intuitive mouse controls.
-
โก Multiple Pathfinding Algorithms: Visualize and compare the execution of A* and Dijkstra's Algorithm on the same grid environment.
-
๐ Performance Metrics: Reports execution runtime and the total number of explored nodes after each search.
-
๐จ Real-Time Visualization: Animates frontier expansion, explored nodes, and the reconstructed shortest path as the algorithm executes.
-
โ๏ธ Configurable Settings: Customize grid size, frame rate, and the application's visual theme through a centralized configuration file.
pathfinding-visualizer/
โ
โโโ assets/
โ โโโ demo.gif
โ
โโโ src/
โ โโโ main.py
โ โโโ config.py
โ
โโโ README.md
โโโ requirements.txt
โโโ LICENSE
The project follows a modular, object-oriented architecture consisting of two primary modules:
config.py: Stores application settings, including window configuration, visualization parameters, and color definitions.main.py: Implements the grid engine, user interaction, visualization logic, and the A* and Dijkstra pathfinding algorithms.
graph TD
cfg["config.py"] --> app["PathfindingVisualizer"]
node["Node"] --> app
app --> grid["Interactive Grid"]
app --> algo["A* / Dijkstra"]
algo --> vis["Animated Visualization"]
NodeClass: Encapsulates a grid cell's spatial attributes, current visual status, coordinate tracking, boundary neighbor validation, and rendering mechanics.PathfindingVisualizerClass: The central coordinator containing the Pygame application loop, screen drawing pipelines, input handlers, coordinate transformations, and pathfinding solvers.
Ensure you have Python 3.8+ installed.
git clone https://github.com/rhthm/pathfinding-visualizer
cd pathfinding-visualizerpip install -r requirements.txtpython src/main.pyYou can easily modify visual elements and grid scale by editing the properties in config.py:
# window settings
WIDTH = 800
ROWS = 50
TARGET_FPS = 120
# color schemes
DEFAULT_NODE_COLOR = (30, 34, 42)
WALL_COLOR = (15, 17, 21)
GRID_LINE_COLOR = (44, 50, 62)
START_COLOR = (255, 110, 110)
END_COLOR = (80, 250, 123)
OPEN_SET_COLOR = (0, 150, 255)
CLOSED_SET_COLOR = (40, 52, 74)
PATH_COLOR = (189, 147, 249) -
Diagonal Movement: Enable 8-directional traversal using Octile or Chebyshev distance heuristics.
-
Heuristic Options: Support multiple heuristic functions (e.g., Manhattan, Euclidean, Octile) and implement Greedy Best-First Search for comparison.
-
Dynamic Weight Maps: Transition the grid from binary states (walkable vs wall) to weighted terrains (e.g., mud, rough patches), forcing the algorithms to calculate complex cost gradients (
$g(n)$ variations). -
Maze Generation: Implement automatic maze generation algorithms such as Recursive Backtracking and Randomized Kruskal's Algorithm.
-
Interactive UI Controls: Add an on-screen control panel featuring Start, Pause, Resume, Reset, Adjustable Grid Size, Animation Speed Slider, Theme Support, and real-time statistics including Execution Runtime, Path Length, and Explored Node Count.
This project is licensed under the MIT License - see the LICENSE file for details.
Built by rhthm.
