Skip to content

An efficient shortest pathfinding system for transportation networks using Dijkstra's algorithm. Built with Django (backend), PostgreSQL (map data storage and path queries), and MapLibre GL JS in Next.js (interactive OpenStreetMap-based frontend). Optimized to compute shortest paths directly in SQL without loading full graphs into memory.

Notifications You must be signed in to change notification settings

Mayu-infinite/Pathfinding-DSA-project

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

58 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pathfinding System with Django, Next.js, and PostgreSQL

Project Overview

This project implements a Shortest Pathfinding System using Dijkstra's Algorithm and A*. The backend is built with Django, the frontend with Next.js, and the shortest path computation is performed efficiently using file-based caching. The project integrates OpenStreetMap (OSM) data for geospatial mapping and visualization.


πŸ“ Folder Structure

project_root/
│── backend/            # Django Backend
β”‚   β”œβ”€β”€ shortest_path/  # Django Project
β”‚   β”‚   β”œβ”€β”€ manage.py       # Django project manager
β”‚   β”‚   β”œβ”€β”€ shortest_path/  # Main Django app
β”‚   β”‚   β”œβ”€β”€ maps/           # Django routes app
β”‚   β”‚   β”‚   β”œβ”€β”€ models.py   # Database models
β”‚   β”‚   β”‚   β”œβ”€β”€ views.py    # API views
β”‚   β”œβ”€β”€ utils/              # Utils to populate the data into the database
│── frontend/           # Next.js Frontend
β”‚   β”œβ”€β”€ components/     # React components
β”‚   β”œβ”€β”€ pages/          # Next.js pages
β”‚   β”œβ”€β”€ public/         # Static assets
β”‚   β”œβ”€β”€ styles/         # Global styles
β”‚   β”œβ”€β”€ utils/          # Helper functions
β”‚   β”œβ”€β”€ services/       # API calls and integrations
│── docs/               # Documentation
│── README.md           # Project documentation

πŸš€ Setting Up the Project

1️⃣ Setting up Django Backend

Step 1: Create a Virtual Environment

pip install venv
python -m venv venv

Step 2: Activate Virtual Environment

Windows:

venv\Scripts\activate

Linux/macOS:

source venv/bin/activate

Step 3: Install Dependencies

cd backend
pip install -r requirements.txt

2️⃣ Setting up PostgreSQL Database

Step 1: Install PostgreSQL

Download PostgreSQL from the official site: https://www.postgresql.org/download/

Step 2: Setup the Database

  • Open pgAdmin and create a new database named pathfindingdb.
  • Set up environment variables in .env:
DB_NAME=pathfindingdb
DB_USER=your_username
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
  • Apply migrations:
python manage.py migrate

Step 3: Populate the Database with OSM Data

Run the following utility script:

python backend/utils/util_pune.py

This will populate the database with geospatial data.


3️⃣ Setting up Next.js Frontend

Step 1: Install Node.js and npm

Download and install Node.js from https://nodejs.org/.

Step 2: Install Dependencies

cd frontend
npm install

Step 3: Set Up Environment Variables

Create a .env.local file in the frontend directory:

NEXT_PUBLIC_API_BASE_URL=http://localhost:8000/api

Step 4: Run the Frontend

npm run dev

The Next.js application will be available at http://localhost:3000/.


4️⃣ Features of the Frontend

  • Interactive Map: Displays OpenStreetMap with path visualization.
  • Shortest Path Calculation: Users can select two points, and the shortest path is computed and displayed.
  • Live Updates: The frontend dynamically fetches routes from the backend API.
  • Custom Markers: Start and end points are marked on the map.
  • Mobile-Friendly UI: Responsive design for mobile and desktop users.

πŸ”₯ Implementing the Shortest Path Algorithm

Using File-Based Caching for Performance

We use Django’s file-based caching to cache graph data and prevent repeated database reads, speeding up the algorithm.

Steps Implemented for Caching:

  • Graph Data Caching:
    • An adjacency list representation of the graph is cached in the filesystem.
    • If cache is missing or expired, data is loaded from the database and cached again.

Django Settings

# settings.py
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': os.path.join(BASE_DIR, 'graph_cache'),
    }
}

Make sure the graph_cache/ directory exists and is writable.

Graph Loading Function

from django.core.cache import cache

def load_graph():
    graph = cache.get("graph_data")
    if graph:
        return graph

    # Load graph from DB if not cached
    nodes = Node.objects.all()
    edges = Edge.objects.all()
    graph = {node.id: [] for node in nodes}
    for edge in edges:
        graph[edge.start_node_id].append((edge.end_node_id, edge.weight))
        graph[edge.end_node_id].append((edge.start_node_id, edge.weight))

    cache.set("graph_data", graph)
    return graph

πŸ“Œ Future Enhancements

  • Optimizing pathfinding by integrating Dijkstra directly into PostgreSQL using recursive SQL queries.
  • Implementing an API to fetch routes dynamically from the frontend.
  • Enhancing UI with interactive map features using MapLibre GL JS.

πŸ›  Tech Stack

  • Backend: Django, Django REST Framework
  • Frontend: Next.js, React, MapLibre GL JS
  • Database: PostgreSQL + PostGIS
  • Pathfinding: Dijkstra’s Algorithm, A*
  • Geospatial Data: OpenStreetMap (OSM), GeoJSON
  • Caching: Django File-Based Caching

πŸ“½οΈ Demo

Watch demo video


πŸ’‘ Contributors

  • Prasangeet Dongre (B23CH1033) - Project Lead
  • Prakhar Chauhan (B23BB1032)
  • Rajas Kadu (B23CH1039)
  • Mayuri R. Pujari (B23ES1026)

About

An efficient shortest pathfinding system for transportation networks using Dijkstra's algorithm. Built with Django (backend), PostgreSQL (map data storage and path queries), and MapLibre GL JS in Next.js (interactive OpenStreetMap-based frontend). Optimized to compute shortest paths directly in SQL without loading full graphs into memory.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 64.2%
  • Python 29.6%
  • CSS 6.2%