https://github.com/ahmedyar7/pathfinder
This is the project of visual implementation of the searching algorithms BFS, DFS, and A* Algorithms
https://github.com/ahmedyar7/pathfinder
astar bfs-algorithm dfs searching-algorithms
Last synced: 9 months ago
JSON representation
This is the project of visual implementation of the searching algorithms BFS, DFS, and A* Algorithms
- Host: GitHub
- URL: https://github.com/ahmedyar7/pathfinder
- Owner: ahmedyar7
- License: mit
- Created: 2024-08-08T14:09:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-28T12:49:45.000Z (12 months ago)
- Last Synced: 2024-12-28T13:27:46.175Z (12 months ago)
- Topics: astar, bfs-algorithm, dfs, searching-algorithms
- Language: Python
- Homepage: https://www.linkedin.com/feed/update/urn:li:activity:7246523843538763776/
- Size: 249 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# đ§ Pathfinder Visualizer
This repository contains a visual implementation of the **A\*** pathfinding algorithm and other algorithms such as **BFS**, **Dijkstra**, and **Prim's Algorithm**. The implementation uses **Pygame** đšī¸ for the graphical interface and interactive grid-based pathfinding visualization.
| đĸ **BFS Algorithm** | đĩ **DFS Algorithm** | â **A\* Algorithm** |
| ------------------------------------ | ------------------------------------ | ------------------------------------- |
|  |  |  |
## ⨠Features
- đąī¸ **Interactive Grid**: Users can click to set the start and end points and add barriers.
- đ§ **Multiple Algorithms**: Supports A\*, BFS, Dijkstra, and Prim's algorithms for pathfinding.
- đ **Real-time Visualization**: Watch how each algorithm explores the grid and finds the optimal path.
- đ§ **Customizable Grid Size**: A 50x50 grid is used by default, but can be modified.
## âī¸ Installation
To run this project locally, follow these steps:
1. **Clone the repository**:
```bash
git clone https://github.com/ahmedyar7/PathFinder.git
cd PathFinder
```
2. **Install the required dependencies**:
You need to have Python installed along with the `pygame` library.
```bash
pip install pygame
```
3. **Run the program**:
```bash
py main.py
```
## đ ī¸ Usage
After starting the program, an interactive window will open where you can draw the start and end points, barriers, and choose different algorithms to visualize their pathfinding.
### đŽ Basic Interactions:
- **Left-click**: Add a start, end, or barrier.
- **Right-click**: Remove a barrier, start, or end node.
- **Keyboard shortcuts**:
- `SPACE`: Starts pathfinding using the **A\*** algorithm.
- `B`: Starts pathfinding using **BFS**.
- `D`: Starts pathfinding using **Dijkstra's algorithm**.
- `P`: Starts pathfinding using **Prim's algorithm**.
- `R`: Resets the grid.
## đ§ How It Works
### Files Overview
#### `program.py`
This is the main driver file for the project. It handles the game loop đŽ and user interactions with the grid, including mouse clicks đąī¸ and keyboard inputs â¨ī¸. It calls the necessary functions to start different algorithms and updates the grid in real-time.
Key components:
- **`Program` class**: Manages the entire application.
- **`driver` method**: The main loop for drawing and interacting with the grid. It handles mouse and keyboard inputs and triggers the pathfinding algorithms.
- **`get_clicked_pos` method**: Converts mouse clicks into grid coordinates, helping to select the start/end nodes or barriers.
#### `grid.py`
This file contains the code for setting up and rendering the grid đĻ.
Key components:
- **`make_grid` method**: Initializes a 2D grid of "spot" objects, representing each cell in the grid.
- **`draw` method**: Renders the grid and its components (start, end, barriers, path).
- **`draw_grid` method**: Draws the grid lines to visually divide the window into a grid.
#### `algorithm.py`
This file contains the implementation of the four pathfinding algorithms.
Key components:
- **`Algorithm` class**: Contains methods for each algorithm (A\*, BFS, Dijkstra, Prim).
- **`heuristic_function`**: A heuristic function used by the A\* algorithm to estimate the distance between nodes (Manhattan distance đ).
- **`reconstruct_path`**: Backtracks from the end node to the start node after finding the shortest path, marking the path on the grid.
Each algorithm method (e.g., `a_star_algorithm`, `bfs_algorithm`) operates by:
1. Using different techniques to explore the grid.
2. Keeping track of visited nodes and neighbors.
3. Reconstructing and visualizing the optimal path đ¤ī¸.
#### `spot.py`
This file defines the `Spot` class, representing each cell in the grid.
Key components:
- **`Spot` class**: Manages the state of each cell (is it the start node, end node, barrier, open, closed, etc.).
- **`draw` method**: Renders each cell on the screen đ¨.
- **`update_neighbors` method**: Updates the list of neighboring cells for a given spot, which is crucial for pathfinding algorithms.
## đ Algorithms Supported
- **A\* Algorithm**: Uses a heuristic function đ§ to explore nodes, balancing between exploring the shortest known path and the estimated distance to the goal.
- **Dijkstra's Algorithm**: Explores nodes in increasing order of distance from the start, ensuring the shortest path is found (like A\* but without a heuristic).
- **Prim's Algorithm**: Typically used for minimum spanning trees đ˛, here adapted for pathfinding.
- **Breadth-First Search (BFS)**: Explores all possible nodes layer by layer, guaranteeing the shortest path in an unweighted grid.
## đ¯ Controls
- **Left-click**:
- First click: Set start node đĸ.
- Second click: Set end node đ´.
- Subsequent clicks: Set barriers đĻ.
- **Right-click**: Remove a node or barrier â.
- **`SPACE`**: Start **A\*** pathfinding â.
- **`B`**: Start **BFS** pathfinding đĸ.
- **`D`**: Start **Dijkstra's** pathfinding âī¸.
- **`P`**: Start **Prim's** pathfinding đ˛.
- **`R`**: Reset the grid to the initial state đ.
## đ License
This project is open-source and available under the [MIT License](LICENSE). đ