A web app to find the best routes in the Delhi Metro network, built with Flask and Python
Ever struggled to figure out the fastest metro route in Delhi? This web app helps you find the best path between any two metro stations. It calculates:
- Shortest distance route - Gets you there using the least kilometers
- Least line changes - Minimizes the number of times you switch lines
- Fare calculation - Shows both token and smart card prices
- Travel time estimates - Tells you roughly how long it'll take
I built this as a learning project to understand graph algorithms and full-stack web development.
Live features:
- Search 220+ Delhi Metro stations
- Interactive route visualization
- Compare different route options
- Real-time fare calculation
- Works on mobile and desktop
As a sophomore at IIIT Delhi, I wanted to:
- Learn how pathfinding algorithms actually work (not just in theory!)
- Build something practical that people can use
- Get hands-on with Flask and web development
- Understand graph data structures better
Plus, I take the metro often and thought it would be cool to build this myself instead of using Google Maps all the time!
| Component | Technology | Why I chose it |
|---|---|---|
| Backend | Python 3.10 + Flask | Easy to learn, great for APIs |
| Algorithm | Dijkstra's (with heap) | Best for shortest path problems |
| Frontend | HTML/CSS/JavaScript | Keeps it simple, no frameworks needed |
| Data Structure | Adjacency List Graph | Efficient for metro networks |
| Testing | Pytest | Industry standard for Python |
delhi-metro-route-finder/
āāā app.py # Flask backend (API routes, server)
āāā metro_graph.py # Core algorithm (Dijkstra's implementation)
āāā dmrc_api.py # API wrapper (currently uses local data)
āāā static/
ā āāā script.js # Frontend logic
ā āāā style.css # UI styling
āāā templates/
ā āāā index.html # Main webpage
āāā tests/
ā āāā test_metro_graph.py # Unit tests
āāā requirements.txt # Python dependencies
- Python 3.10 or higher (Download here)
- pip (comes with Python)
- A terminal/command prompt
Option 1: Automatic Setup (Linux/Mac)
# Clone the repository git clone https://github.com/Ronak-IIITD/delhi_metro-Route-Finder.git cd delhi-metro-route-finder # Run the setup script (does everything for you!) bash setup.sh
Option 2: Manual Setup (All platforms)
# 1. Clone the repository git clone https://github.com/Ronak-IIITD/delhi_metro-Route-Finder.git cd delhi-metro-route-finder # 2. Create virtual environment python -m venv venv # 3. Activate virtual environment # On Windows: venv\Scripts\activate # On Mac/Linux: source venv/bin/activate # 4. Install dependencies pip install -r requirements.txt # 5. Copy environment variables cp .env.example .env # 6. Run the app python app.py
Once setup is complete:
python app.py
Then open your browser and go to: http://localhost:5000
That's it! š
I implemented Dijkstra's shortest path algorithm with a binary heap (priority queue) for optimal performance:
# Simplified version of what happens: def find_shortest_path(start, end): distances = {station: infinity for all stations} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_station = pop_min(priority_queue) if current_station == end: return reconstruct_path() for neighbor in current_station.neighbors: new_distance = current_distance + edge_weight if new_distance < distances[neighbor]: distances[neighbor] = new_distance push(priority_queue, (new_distance, neighbor))
Time Complexity: O((V + E) log V) where V = stations, E = connections
Space Complexity: O(V)
The metro network is represented as a weighted graph using an adjacency list:
- Nodes = Metro stations
- Edges = Direct connections between stations
- Weights = Distance in kilometers
Example:
graph = { "Rajiv Chowk": { "Patel Chowk": 1.3, # 1.3 km away "Barakhamba Road": 0.7, # 0.7 km away }, ... }
- Shortest Distance: Finds the path with minimum total kilometers
- Least Changes: Minimizes line switching (useful when carrying luggage!)
- Compare Mode: Shows both routes side by side
Based on official DMRC fare structure (2024):
- Up to 2 km ā ā¹10
- 2-5 km ā ā¹20
- 5-12 km ā ā¹30
- 12-21 km ā ā¹40
- 21-32 km ā ā¹50
- Above 32 km ā ā¹60
Smart card users get 10% off!
- Autocomplete with 220+ stations
- Fuzzy matching (works even with typos)
- Keyboard navigation (arrow keys + enter)
- 8 metro lines (Yellow, Blue, Red, Green, Violet, Magenta, Pink, Grey)
- 220+ stations
- Latest network (updated January 2026)
- Data Structures: How to implement and use graphs effectively
- Algorithms: Understanding Dijkstra's algorithm beyond just theory
- Web Development: Building REST APIs with Flask
- Frontend: Making responsive UIs with vanilla JavaScript
- Testing: Writing meaningful unit tests with pytest
-
Getting Dijkstra's right: Initially used a basic queue which was slow (O(V²)). Learned about heaps and optimized it to O((V+E) log V).
-
Handling line changes: Figuring out when users switch metro lines was trickier than expected. Had to track which line you're on at each station.
-
Station name matching: Users type station names differently (e.g., "Rajiv Chowk" vs "rajiv chowk"). Implemented case-insensitive fuzzy search.
-
Test coverage: Writing tests for edge cases (same source/destination, invalid stations, disconnected routes) taught me to think like a user.
Run the test suite:
# Run all tests pytest tests/ -v # Run with coverage report pytest tests/ --cov=metro_graph --cov-report=html
Current test coverage: 37 tests, all passing ā
Tests include:
- Route finding accuracy
- Fare calculation
- Edge cases (invalid input, same station, etc.)
- Performance benchmarks
The backend exposes several REST endpoints:
POST /find_route Content-Type: application/json { "source": "Rajiv Chowk", "destination": "Kashmere Gate", "route_type": "shortest" // or "least_changes" or "all" }
GET /api/search_stations?q=rajiv
GET /api/station_info/Rajiv Chowk
See app.py for complete API documentation.
Things I want to add next:
- Real-time train tracking (if DMRC API becomes available)
- Save favorite routes
- Journey history
- Offline mode with PWA
- Accessibility improvements (screen reader support)
- Hindi language support
- Share route via link
This is a learning project, but I'm open to suggestions! If you find a bug or have an idea:
- Open an issue
- Fork the repo and make your changes
- Submit a pull request
Please be kind - I'm still learning!
- Lines of Code: ~1,500 (Python + JavaScript)
- Development Time: ~2 weeks
- Stations Covered: 220+
- Test Coverage: 37 tests
- Coffee Consumed: Too much ā
- Delhi Metro Rail Corporation (DMRC) for the network data
- My DSA professor for teaching graph algorithms
- Stack Overflow for helping me debug at 2 AM
- My friends who tested the app and found bugs
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ā¤ļø by Ronak Anand
Sophomore @ IIIT Delhi
If you found this helpful, give it a star! ā
- GitHub: @Ronak-IIITD
- Project Link: https://github.com/Ronak-IIITD/delhi_metro-Route-Finder
Feel free to open an issue or reach out if you have questions about the code or want to discuss the project!