A full-stack web application that implements Dijkstra's Algorithm to find the shortest path between locations on a graph. Built with React, Node.js, Express, and MongoDB.
- Graph Data Structure: Represent maps using nodes (locations) and edges (roads with distances)
- Dijkstra's Algorithm: Find shortest paths using a Min-Heap priority queue
- RESTful API: Backend endpoints for nodes, edges, and path calculation
- Clean UI: React components for adding locations, roads, and querying routes
- Real-time Results: Display path and total distance instantly
- Frontend: React.js with CSS
- Backend: Node.js + Express
- Database: MongoDB
- Algorithm: Dijkstra's Algorithm with Min-Heap implementation
smart-route-finder/
├── backend/
│ ├── models/
│ │ ├── Node.js # Location model
│ │ └── Edge.js # Road model
│ ├── routes/
│ │ └── graphRoutes.js # API endpoints
│ ├── utils/
│ │ └── dijkstra.js # Dijkstra algorithm implementation
│ ├── server.js # Express server
│ ├── .env # Environment variables
│ └── package.json
├── frontend/
│ ├── public/
│ │ └── index.html # React entry point
│ ├── src/
│ │ ├── components/
│ │ │ ├── AddNode.js # Add location component
│ │ │ ├── AddEdge.js # Add road component
│ │ │ └── FindPath.js # Find path component
│ │ ├── api.js # API client
│ │ ├── App.js # Main app component
│ │ ├── App.css # Styles
│ │ ├── index.js # React DOM render
│ │ └── index.html # HTML template
│ ├── package.json
│ └── node_modules/
└── README.md
- Node.js (v14+)
- MongoDB (installed and running)
- npm or yarn
mongod --dbpath C:\data\dbcd backend
npm install
npm startBackend runs on http://localhost:5000
cd frontend
npm install
npm startFrontend runs on http://localhost:3000
- GET
/api/nodes- Get all locations - POST
/api/nodes- Add location- Body:
{ "name": "CityA" }
- Body:
- POST
/api/edges- Add road- Body:
{ "source": "nodeId", "destination": "nodeId", "weight": 10 }
- Body:
- POST
/api/shortest-path- Find shortest route- Body:
{ "sourceId": "nodeId", "destinationId": "nodeId" } - Response:
{ "path": ["A", "B", "C"], "totalDistance": 25 }
- Body:
- Open
http://localhost:3000 - Add Locations: Enter location names
- Add Roads: Select source/destination and distance
- Find Route: Select start/end points and click "Find Shortest Path"
- View the shortest route and total distance
Implemented with Min-Heap Priority Queue:
- Initialize all distances to ∞ (except start = 0)
- Add start node to priority queue
- Process nodes in order of shortest distance
- Update neighbor distances if shorter path found
- Reconstruct path using previous node map
- Time Complexity: O((V + E) log V)
Create test data:
# Add nodes (get returned IDs)
curl -X POST -H "Content-Type: application/json" -d "{\"name\":\"A\"}" http://localhost:5000/api/nodes
curl -X POST -H "Content-Type: application/json" -d "{\"name\":\"B\"}" http://localhost:5000/api/nodes
# Add edge (use node IDs from above)
curl -X POST -H "Content-Type: application/json" -d "{\"source\":\"<ID_A>\",\"destination\":\"<ID_B>\",\"weight\":10}" http://localhost:5000/api/edges
# Find path
curl -X POST -H "Content-Type: application/json" -d "{\"sourceId\":\"<ID_A>\",\"destinationId\":\"<ID_B>\"}" http://localhost:5000/api/shortest-path| Issue | Solution |
|---|---|
| MongoDB connection error | Ensure mongod is running: mongod --dbpath C:\data\db |
| React "index.html not found" | Frontend files are now complete and ready to start |
| CORS errors | Backend has CORS enabled; check both servers running |
| Port in use | Change PORT in backend .env or stop existing process |
ISC
- Node.js (v14 or higher)
- MongoDB (v4 or higher)
- npm (v6 or higher)
-
Clone the repository:
git clone <repository-url> cd smart-route-finder -
Backend setup:
cd backend npm install -
Frontend setup:
cd ../frontend npm install
Create a .env file in the backend directory with the following content:
MONGODB_URI=mongodb://localhost:27017/smartroutefinder
PORT=5000
-
Start MongoDB (if not running as a service):
mongod -
Start the backend server:
cd backend npm startThe server will run on
http://localhost:5000 -
Start the frontend development server:
cd frontend npm startThe application will run on
http://localhost:3000
GET /api/nodes- Get all locationsPOST /api/nodes- Add a new location- Body:
{ "name": "Location Name" }
- Body:
POST /api/edges- Add a new road- Body:
{ "source": "nodeId", "destination": "nodeId", "weight": 10 }
- Body:
POST /api/shortest-path- Find shortest path between two locations- Body:
{ "sourceId": "nodeId", "destinationId": "nodeId" }
- Body:
- Open the application in your browser (
http://localhost:3000) - Add locations using the "Add Location" form
- Add roads between locations using the "Add Road" form (specify source, destination, and distance)
- Select source and destination locations in the "Find Shortest Path" section
- Click "Find Shortest Path" to see the route and total distance
You can add the following sample data to test the application:
Locations (Nodes):
- A
- B
- C
- D
- E
Roads (Edges):
- A to B: 4
- A to C: 2
- B to C: 1
- B to D: 5
- C to D: 8
- C to E: 10
- D to E: 2
Test Case: Find shortest path from A to E:
- Expected Path: A → C → B → D → E
- Expected Total Distance: 2 + 1 + 5 + 2 = 10
The backend implements Dijkstra's Algorithm using a Priority Queue (Min Heap) for efficient extraction of the minimum distance node.
Key aspects:
- Time Complexity: O((V + E) log V) where V is vertices and E is edges
- Space Complexity: O(V)
- The algorithm finds the shortest path in a weighted graph with non-negative weights
The graph is stored in MongoDB with two collections:
- Nodes: Stores location information (id, name)
- Edges: Stores road information (source, destination, weight)
When calculating the shortest path, the backend:
- Retrieves all nodes and edges from the database
- Builds an adjacency list representation of the graph
- Applies Dijkstra's Algorithm to find the shortest path
- Returns the path with location names and total distance
- Visual graph display using a library like D3.js or vis.js
- Dynamic weight updates to simulate traffic conditions
- Multiple route alternatives (k-shortest paths)
- User authentication and saved maps
- Mobile-responsive design
- Deployment instructions for production (Heroku, AWS, etc.)
This project is open source and available under the MIT License."# Smart-Road-Finder"