Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- README.md +75 -14
- algorithms.py +203 -0
- app.py +766 -0
- graph_builder.py +227 -0
- map_utils.py +122 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,14 +1,75 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AP Smart Route Finder — Gradio Edition
|
| 2 |
+
|
| 3 |
+
A clean, single-file route planner for Andhra Pradesh cities with:
|
| 4 |
+
- ✅ Dropdown city selector (70+ AP cities)
|
| 5 |
+
- ✅ All paths found and listed (sorted shortest to longest)
|
| 6 |
+
- ✅ Shortest path highlighted on real OpenStreetMap
|
| 7 |
+
- ✅ Click city markers on map for Malls, Restaurants, Petrol Bunks, Landmarks
|
| 8 |
+
- ✅ Stats bar (total km, number of paths, hops)
|
| 9 |
+
- ✅ Simple Gradio UI — no Streamlit needed
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
## 🚀 Setup (3 steps)
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
# 1. Install dependencies
|
| 17 |
+
pip install gradio networkx numpy folium
|
| 18 |
+
|
| 19 |
+
# 2. Run
|
| 20 |
+
python app_gradio.py
|
| 21 |
+
|
| 22 |
+
# 3. Open browser
|
| 23 |
+
# → http://localhost:7860
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## How to Use
|
| 29 |
+
|
| 30 |
+
1. **Select Starting City** from the dropdown (e.g. Vijayawada)
|
| 31 |
+
2. **Select Destination City** (e.g. Visakhapatnam)
|
| 32 |
+
3. Click **🔍 Find Best Route**
|
| 33 |
+
|
| 34 |
+
### What you'll see:
|
| 35 |
+
| Section | Description |
|
| 36 |
+
|---|---|
|
| 37 |
+
| **Stats Bar** | Total km, paths found, hops, nearby cities |
|
| 38 |
+
| **Map** | Real OSM map — orange line = shortest, blue dashes = alternates |
|
| 39 |
+
| **Route Details** | Step-by-step breakdown with distances |
|
| 40 |
+
| **All Paths** | Every route found, sorted by distance |
|
| 41 |
+
| **POI Panels** | Petrol bunks, malls, restaurants, landmarks for source & destination |
|
| 42 |
+
|
| 43 |
+
### Map Popups
|
| 44 |
+
Click any 📍 marker on the map to see:
|
| 45 |
+
- ⛽ Petrol Bunks near that city
|
| 46 |
+
- 🏬 Malls & Shopping
|
| 47 |
+
- 🍽️ Restaurants
|
| 48 |
+
- 🏛️ Landmarks
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## File Structure
|
| 53 |
+
|
| 54 |
+
```
|
| 55 |
+
app_gradio.py ← Single file — everything is here
|
| 56 |
+
requirements.txt ← pip install -r requirements.txt
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## Algorithm
|
| 62 |
+
The app uses two complementary algorithms internally and picks the best confirmed result:
|
| 63 |
+
- **Dijkstra** — guaranteed optimal, O(E + V log V)
|
| 64 |
+
- **Grover Amplitude Amplification** — quantum-inspired search, O(√N)
|
| 65 |
+
|
| 66 |
+
The result shown is always the shortest confirmed path — no algorithm labels shown to the user.
|
| 67 |
+
|
| 68 |
+
## Cities Covered
|
| 69 |
+
70+ cities including all district HQs, major towns across:
|
| 70 |
+
- North Andhra: Visakhapatnam, Srikakulam, Vizianagaram
|
| 71 |
+
- Godavari: Rajahmundry, Kakinada, Eluru, Bhimavaram
|
| 72 |
+
- Krishna: Vijayawada, Machilipatnam, Gudivada
|
| 73 |
+
- Guntur: Guntur, Tenali, Narasaraopet
|
| 74 |
+
- South Coastal: Nellore, Ongole, Kavali
|
| 75 |
+
- Rayalaseema: Kurnool, Kadapa, Anantapur, Chittoor, Tirupati
|
algorithms.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
algorithms.py
|
| 3 |
+
─────────────
|
| 4 |
+
Two shortest-path algorithms:
|
| 5 |
+
1. Dijkstra — classical O(E + V log V), always optimal.
|
| 6 |
+
2. Grover Search — proper quantum amplitude-amplification simulation
|
| 7 |
+
over the discrete path search space.
|
| 8 |
+
Uses a numpy state-vector that is iteratively
|
| 9 |
+
phase-flipped (oracle) and diffused (inversion
|
| 10 |
+
about the mean), then sampled.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import time
|
| 14 |
+
import math
|
| 15 |
+
import random
|
| 16 |
+
import numpy as np
|
| 17 |
+
import networkx as nx
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 21 |
+
# 1. DIJKSTRA (classical)
|
| 22 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 23 |
+
|
| 24 |
+
def run_dijkstra(G: nx.Graph, source: str, target: str) -> dict:
|
| 25 |
+
"""
|
| 26 |
+
Standard Dijkstra via NetworkX.
|
| 27 |
+
Returns a result dict with path, cost, time_ms, and step log.
|
| 28 |
+
"""
|
| 29 |
+
t0 = time.perf_counter()
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
path = nx.dijkstra_path(G, source, target, weight="weight")
|
| 33 |
+
cost = nx.dijkstra_path_length(G, source, target, weight="weight")
|
| 34 |
+
except nx.NetworkXNoPath:
|
| 35 |
+
return {"path": [], "cost": None, "time_ms": 0,
|
| 36 |
+
"error": "No path found", "steps": []}
|
| 37 |
+
except nx.NodeNotFound as e:
|
| 38 |
+
return {"path": [], "cost": None, "time_ms": 0,
|
| 39 |
+
"error": str(e), "steps": []}
|
| 40 |
+
|
| 41 |
+
elapsed = (time.perf_counter() - t0) * 1000
|
| 42 |
+
|
| 43 |
+
# Build a human-readable relaxation log (simulated from the final path)
|
| 44 |
+
steps = []
|
| 45 |
+
running = 0
|
| 46 |
+
for i in range(len(path) - 1):
|
| 47 |
+
u, v = path[i], path[i + 1]
|
| 48 |
+
w = G[u][v]["weight"]
|
| 49 |
+
running += w
|
| 50 |
+
steps.append({
|
| 51 |
+
"step": i + 1,
|
| 52 |
+
"edge": f"{u} → {v}",
|
| 53 |
+
"edge_km": w,
|
| 54 |
+
"cumulative_km": round(running, 1),
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
return {
|
| 58 |
+
"path": path,
|
| 59 |
+
"cost": round(cost, 1),
|
| 60 |
+
"time_ms": round(elapsed, 3),
|
| 61 |
+
"error": None,
|
| 62 |
+
"steps": steps,
|
| 63 |
+
"complexity": "O(E + V log V)",
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 68 |
+
# 2. GROVER QUANTUM SEARCH SIMULATION
|
| 69 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 70 |
+
|
| 71 |
+
def _collect_paths(G: nx.Graph, source: str, target: str,
|
| 72 |
+
max_paths: int = 512):
|
| 73 |
+
"""
|
| 74 |
+
Enumerate simple paths (DFS). Capped at max_paths to keep the
|
| 75 |
+
state-vector tractable; shortest paths are prepended so quality
|
| 76 |
+
solutions are always in the search space.
|
| 77 |
+
"""
|
| 78 |
+
try:
|
| 79 |
+
optimal = nx.dijkstra_path(G, source, target, weight="weight")
|
| 80 |
+
except Exception:
|
| 81 |
+
optimal = None
|
| 82 |
+
|
| 83 |
+
paths = []
|
| 84 |
+
gen = nx.all_simple_paths(G, source, target, cutoff=8)
|
| 85 |
+
for p in gen:
|
| 86 |
+
paths.append(p)
|
| 87 |
+
if len(paths) >= max_paths:
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
if optimal and optimal not in paths:
|
| 91 |
+
paths.insert(0, optimal)
|
| 92 |
+
|
| 93 |
+
return paths
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _path_cost(G: nx.Graph, path) -> float:
|
| 97 |
+
return sum(G[path[i]][path[i + 1]]["weight"]
|
| 98 |
+
for i in range(len(path) - 1))
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def _grover_iterations(N: int) -> int:
|
| 102 |
+
"""Optimal Grover iterations ≈ π/4 · √N."""
|
| 103 |
+
return max(1, round((math.pi / 4) * math.sqrt(N)))
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def run_grover(G: nx.Graph, source: str, target: str,
|
| 107 |
+
max_paths: int = 256) -> dict:
|
| 108 |
+
"""
|
| 109 |
+
Grover Amplitude Amplification over the path search space.
|
| 110 |
+
|
| 111 |
+
State vector:
|
| 112 |
+
|ψ⟩ = Σ αᵢ |pathᵢ⟩ (complex amplitudes, Σ|αᵢ|² = 1)
|
| 113 |
+
|
| 114 |
+
Oracle:
|
| 115 |
+
Flips the sign of all "good" states — i.e., paths whose cost
|
| 116 |
+
is at most (1 + ε) × optimal cost. A 10 % margin is used so
|
| 117 |
+
near-optimal paths are also marked.
|
| 118 |
+
|
| 119 |
+
Diffuser (inversion about the mean):
|
| 120 |
+
αᵢ ← 2·μ − αᵢ where μ = mean amplitude.
|
| 121 |
+
|
| 122 |
+
After ⌊π/4 · √N⌋ iterations the amplitude of good states is
|
| 123 |
+
amplified from O(1/√N) to O(1), so a single measurement (sample)
|
| 124 |
+
finds a good path with high probability.
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
+
t0 = time.perf_counter()
|
| 128 |
+
|
| 129 |
+
paths = _collect_paths(G, source, target, max_paths=max_paths)
|
| 130 |
+
N = len(paths)
|
| 131 |
+
|
| 132 |
+
if N == 0:
|
| 133 |
+
return {"path": [], "cost": None, "time_ms": 0,
|
| 134 |
+
"error": "No paths found", "iterations": 0,
|
| 135 |
+
"history": [], "N": 0}
|
| 136 |
+
|
| 137 |
+
# ── cost vector ──────────────────────────────────────────────────────────
|
| 138 |
+
costs = np.array([_path_cost(G, p) for p in paths], dtype=np.float64)
|
| 139 |
+
optimal_cost = costs.min()
|
| 140 |
+
|
| 141 |
+
# Mark "good" states: cost ≤ optimal × 1.10 (10 % tolerance)
|
| 142 |
+
threshold = optimal_cost * 1.10
|
| 143 |
+
oracle_mask = (costs <= threshold).astype(np.float64)
|
| 144 |
+
|
| 145 |
+
# ── initial uniform superposition ────────────────────────────────────────
|
| 146 |
+
amplitudes = np.ones(N, dtype=np.float64) / math.sqrt(N)
|
| 147 |
+
|
| 148 |
+
num_iter = _grover_iterations(N)
|
| 149 |
+
history = []
|
| 150 |
+
oracle_calls = 0
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
for it in range(num_iter):
|
| 154 |
+
oracle_calls += 1
|
| 155 |
+
# Oracle: flip sign of good states
|
| 156 |
+
amplitudes = amplitudes * (1 - 2 * oracle_mask)
|
| 157 |
+
|
| 158 |
+
# Diffuser: inversion about the mean
|
| 159 |
+
mean_amp = amplitudes.mean()
|
| 160 |
+
amplitudes = 2 * mean_amp - amplitudes
|
| 161 |
+
|
| 162 |
+
# Renormalise (floating-point safety)
|
| 163 |
+
norm = np.linalg.norm(amplitudes)
|
| 164 |
+
if norm > 1e-12:
|
| 165 |
+
amplitudes /= norm
|
| 166 |
+
|
| 167 |
+
# Probability distribution from amplitudes
|
| 168 |
+
probs = amplitudes ** 2
|
| 169 |
+
probs = np.clip(probs, 0, None)
|
| 170 |
+
probs /= probs.sum()
|
| 171 |
+
|
| 172 |
+
# Most-probable path at this iteration
|
| 173 |
+
best_idx = int(np.argmax(probs))
|
| 174 |
+
history.append({
|
| 175 |
+
"iteration": it + 1,
|
| 176 |
+
"best_path": paths[best_idx],
|
| 177 |
+
"best_cost": round(costs[best_idx], 1),
|
| 178 |
+
"prob_best": round(float(probs[best_idx]), 4),
|
| 179 |
+
"good_prob_total": round(float(probs[oracle_mask == 1].sum()), 4),
|
| 180 |
+
})
|
| 181 |
+
|
| 182 |
+
# ── Final measurement: sample according to probability ───────────────────
|
| 183 |
+
probs = amplitudes ** 2
|
| 184 |
+
probs = np.clip(probs, 0, None)
|
| 185 |
+
probs /= probs.sum()
|
| 186 |
+
|
| 187 |
+
sampled_idx = int(np.random.choice(N, p=probs))
|
| 188 |
+
sampled_path = paths[sampled_idx]
|
| 189 |
+
sampled_cost = round(costs[sampled_idx], 1)
|
| 190 |
+
|
| 191 |
+
elapsed = (time.perf_counter() - t0) * 1000
|
| 192 |
+
|
| 193 |
+
return {
|
| 194 |
+
"path": sampled_path,
|
| 195 |
+
"cost": sampled_cost,
|
| 196 |
+
"time_ms": round(elapsed, 3),
|
| 197 |
+
"error": None,
|
| 198 |
+
"iterations": num_iter,
|
| 199 |
+
"N": N,
|
| 200 |
+
"history": history,
|
| 201 |
+
"complexity": f"O(√N) ≈ {num_iter} Grover iterations for N={N} paths",
|
| 202 |
+
"optimal_cost": round(optimal_cost, 1),
|
| 203 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,766 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
AP Smart Route Finder — Gradio Edition
|
| 3 |
+
Run: pip install gradio networkx numpy folium
|
| 4 |
+
python app_gradio.py
|
| 5 |
+
"""
|
| 6 |
+
from algorithms import run_dijkstra, run_grover
|
| 7 |
+
import math, random, time
|
| 8 |
+
import numpy as np
|
| 9 |
+
import networkx as nx
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 13 |
+
# CITY DATA
|
| 14 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 15 |
+
AP_CITIES = {
|
| 16 |
+
"Visakhapatnam": (17.6868, 83.2185),
|
| 17 |
+
"Vijayawada": (16.5062, 80.6480),
|
| 18 |
+
"Guntur": (16.3067, 80.4365),
|
| 19 |
+
"Nellore": (14.4426, 79.9865),
|
| 20 |
+
"Kurnool": (15.8281, 78.0373),
|
| 21 |
+
"Rajahmundry": (16.9891, 81.7837),
|
| 22 |
+
"Tirupati": (13.6288, 79.4192),
|
| 23 |
+
"Kadapa": (14.4674, 78.8241),
|
| 24 |
+
"Kakinada": (16.9891, 82.2475),
|
| 25 |
+
"Anantapur": (14.6819, 77.6006),
|
| 26 |
+
"Amaravati": (16.5730, 80.3564),
|
| 27 |
+
"Eluru": (16.7107, 81.0952),
|
| 28 |
+
"Ongole": (15.5057, 80.0499),
|
| 29 |
+
"Srikakulam": (18.2949, 83.8938),
|
| 30 |
+
"Vizianagaram": (18.1066, 83.3956),
|
| 31 |
+
"Chittoor": (13.2172, 79.1003),
|
| 32 |
+
"Machilipatnam": (16.1875, 81.1386),
|
| 33 |
+
"Narasaraopet": (16.2346, 80.0490),
|
| 34 |
+
"Tenali": (16.2435, 80.6404),
|
| 35 |
+
"Mangalagiri": (16.4307, 80.5525),
|
| 36 |
+
"Bhimavaram": (16.5449, 81.5212),
|
| 37 |
+
"Tadepalligudem": (16.8133, 81.5271),
|
| 38 |
+
"Bapatla": (15.9049, 80.4671),
|
| 39 |
+
"Chirala": (15.8264, 80.3538),
|
| 40 |
+
"Narasapuram": (16.4344, 81.6958),
|
| 41 |
+
"Palasa": (18.7726, 84.4083),
|
| 42 |
+
"Bobbili": (18.5744, 83.3588),
|
| 43 |
+
"Parvathipuram": (18.7828, 83.4253),
|
| 44 |
+
"Rajam": (18.4730, 83.6469),
|
| 45 |
+
"Anakapalle": (17.6910, 83.0000),
|
| 46 |
+
"Yellamanchili": (17.5543, 82.8555),
|
| 47 |
+
"Madanapalle": (13.5545, 78.5011),
|
| 48 |
+
"Hindupur": (13.8294, 77.4911),
|
| 49 |
+
"Dharmavaram": (14.4113, 77.7181),
|
| 50 |
+
"Tadipatri": (14.9101, 78.0094),
|
| 51 |
+
"Guntakal": (15.1712, 77.3726),
|
| 52 |
+
"Adoni": (15.6279, 77.2744),
|
| 53 |
+
"Nandyal": (15.4786, 78.4836),
|
| 54 |
+
"Proddatur": (14.7500, 78.5500),
|
| 55 |
+
"Rajampet": (14.1931, 79.1621),
|
| 56 |
+
"Badvel": (14.7455, 79.0643),
|
| 57 |
+
"Pulivendula": (14.4224, 78.2277),
|
| 58 |
+
"Allagadda": (15.1417, 78.5304),
|
| 59 |
+
"Srisailam": (16.0833, 78.8833),
|
| 60 |
+
"Markapur": (15.7407, 79.2682),
|
| 61 |
+
"Kandukur": (15.2143, 79.9057),
|
| 62 |
+
"Kavali": (14.9165, 79.9932),
|
| 63 |
+
"Gudur": (14.1459, 79.8516),
|
| 64 |
+
"Sullurpeta": (13.7630, 79.8878),
|
| 65 |
+
"Venkatagiri": (13.9617, 79.5812),
|
| 66 |
+
"Srikalahasti": (13.7500, 79.6983),
|
| 67 |
+
"Puttur": (13.4408, 79.5493),
|
| 68 |
+
"Pileru": (13.6583, 79.1167),
|
| 69 |
+
"Punganur": (13.3674, 78.5805),
|
| 70 |
+
"Kuppam": (12.7500, 78.3333),
|
| 71 |
+
"Peddapuram": (17.0786, 82.1360),
|
| 72 |
+
"Samalkot": (17.0582, 82.1761),
|
| 73 |
+
"Tuni": (17.3586, 82.5476),
|
| 74 |
+
"Amalapuram": (16.5771, 82.0056),
|
| 75 |
+
"Razole": (16.4784, 81.8413),
|
| 76 |
+
"Mandapeta": (16.8667, 81.9333),
|
| 77 |
+
"Kovvur": (17.0167, 81.7333),
|
| 78 |
+
"Nidadavolu": (17.0578, 81.6728),
|
| 79 |
+
"Tanuku": (16.8600, 81.6800),
|
| 80 |
+
"Palakollu": (16.5155, 81.7301),
|
| 81 |
+
"Nuzvid": (16.7853, 80.8459),
|
| 82 |
+
"Gudivada": (16.4327, 80.9943),
|
| 83 |
+
"Vuyyuru": (16.3618, 80.8459),
|
| 84 |
+
"Repalle": (16.0186, 80.8306),
|
| 85 |
+
"Vinukonda": (16.0500, 79.7333),
|
| 86 |
+
"Sattenapalle": (16.3932, 80.1528),
|
| 87 |
+
"Macherla": (16.4795, 79.4352),
|
| 88 |
+
"Chilakaluripet": (16.0875, 80.1689),
|
| 89 |
+
"Ongole": (15.5057, 80.0499),
|
| 90 |
+
"Narasannapeta": (18.4148, 84.0419),
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
CITY_POIS = {
|
| 94 |
+
"Visakhapatnam": {
|
| 95 |
+
"malls": ["CMR Central Mall", "Jagadamba Junction Mall", "Pothys Mall"],
|
| 96 |
+
"restaurants": ["Dwaraka Hotel", "Bamboo Bay", "Sea View Restaurant", "Hotel Meghalaya"],
|
| 97 |
+
"landmarks": ["Kailasagiri", "Rishikonda Beach", "INS Kurusura Museum", "Araku Valley"],
|
| 98 |
+
"petrol_bunks": ["HP Petrol Bunk - MVP Colony", "Indian Oil - Dwaraka Nagar", "BPCL - Gajuwaka", "Shell - Vizag Steel Area"],
|
| 99 |
+
},
|
| 100 |
+
"Vijayawada": {
|
| 101 |
+
"malls": ["PVP Square", "One Town Mall", "AMB Mall"],
|
| 102 |
+
"restaurants": ["Hotel Swagath", "Minerva Coffee Shop", "Babai Hotel", "Curry Leaf"],
|
| 103 |
+
"landmarks": ["Kanaka Durga Temple", "Prakasam Barrage", "Undavalli Caves", "Bhavani Island"],
|
| 104 |
+
"petrol_bunks": ["HP Bunk - Governorpet", "Indian Oil - Benz Circle", "BPCL - Eluru Road", "Shell - Autonagar"],
|
| 105 |
+
},
|
| 106 |
+
"Guntur": {
|
| 107 |
+
"malls": ["KLM Fashion Mall", "Big Bazaar Guntur"],
|
| 108 |
+
"restaurants": ["Nagarjuna Restaurant", "Hotel Dwaraka", "Udupi Hotel"],
|
| 109 |
+
"landmarks": ["Undavalli Caves", "Amaravati Museum", "Kondaveedu Fort"],
|
| 110 |
+
"petrol_bunks": ["HP - Brodipet", "Indian Oil - Arundelpet", "BPCL - Naaz Centre"],
|
| 111 |
+
},
|
| 112 |
+
"Tirupati": {
|
| 113 |
+
"malls": ["Leela Mahal Mall", "Star Mall Tirupati"],
|
| 114 |
+
"restaurants": ["Hotel Bliss", "Annapurna Hotel", "Bhimas Deluxe"],
|
| 115 |
+
"landmarks": ["Tirumala Temple", "Sri Padmavathi Temple", "Chandragiri Fort"],
|
| 116 |
+
"petrol_bunks": ["HP - Alipiri", "Indian Oil - Renigunta Road", "BPCL - Tiruchanur"],
|
| 117 |
+
},
|
| 118 |
+
"Nellore": {
|
| 119 |
+
"malls": ["Leela Mall", "City Center Mall"],
|
| 120 |
+
"restaurants": ["Hotel Sreyas", "Kamat Hotel", "Spice Garden"],
|
| 121 |
+
"landmarks": ["Pulicat Lake", "Mypad Beach", "Sri Ranganathaswamy Temple"],
|
| 122 |
+
"petrol_bunks": ["HP - Grand Trunk Road", "Indian Oil - Pogathota", "BPCL - Dargamitta"],
|
| 123 |
+
},
|
| 124 |
+
"Rajahmundry": {
|
| 125 |
+
"malls": ["Rajahmundry Mall", "City Center"],
|
| 126 |
+
"restaurants": ["Hotel Anand", "Haritha Hotel", "Bay Leaf"],
|
| 127 |
+
"landmarks": ["Godavari Bridge", "Dowleswaram Barrage", "Papikondalu"],
|
| 128 |
+
"petrol_bunks": ["HP - Morampudi", "Indian Oil - Innespeta", "BPCL - Godavari Bund"],
|
| 129 |
+
},
|
| 130 |
+
"Kadapa": {
|
| 131 |
+
"malls": ["Urban Square Mall"],
|
| 132 |
+
"restaurants": ["Hotel Sree Ram", "Haritha Hotel Kadapa"],
|
| 133 |
+
"landmarks": ["Gandikota Fort", "Vontimitta Temple"],
|
| 134 |
+
"petrol_bunks": ["HP - Railway Station Road", "Indian Oil - Beside Bus Stand", "BPCL - Cuddapah Bypass"],
|
| 135 |
+
},
|
| 136 |
+
"Kurnool": {
|
| 137 |
+
"malls": ["Kurnool Central Mall", "Forum Mall"],
|
| 138 |
+
"restaurants": ["Hotel Srinivasa", "Minerva Restaurant"],
|
| 139 |
+
"landmarks": ["Belum Caves", "Tungabhadra Dam", "Srisailam Temple"],
|
| 140 |
+
"petrol_bunks": ["HP - NH-44", "Indian Oil - Dinne Bypass", "BPCL - Budhawarpet"],
|
| 141 |
+
},
|
| 142 |
+
"Anantapur": {
|
| 143 |
+
"malls": ["Anantapur City Center"],
|
| 144 |
+
"restaurants": ["Hotel Surya", "MTR Anantapur"],
|
| 145 |
+
"landmarks": ["Lepakshi Temple", "Thimmamma Marrimanu"],
|
| 146 |
+
"petrol_bunks": ["HP - Bangalore Road", "Indian Oil - Subash Road", "BPCL - Anantapur Bypass"],
|
| 147 |
+
},
|
| 148 |
+
"Chittoor": {
|
| 149 |
+
"malls": ["Chittoor Mall"],
|
| 150 |
+
"restaurants": ["Hotel Viswa", "Hotel Mayuri"],
|
| 151 |
+
"landmarks": ["Horsley Hills", "Kailasakona Waterfall"],
|
| 152 |
+
"petrol_bunks": ["HP - Madanapalle Road", "Indian Oil - Punganur Road"],
|
| 153 |
+
},
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
DEFAULT_POIS = {
|
| 157 |
+
"malls": ["Local Shopping Centre", "Town Market"],
|
| 158 |
+
"restaurants": ["Udupi Hotel", "Local Dhaba", "Annapoorna Restaurant"],
|
| 159 |
+
"landmarks": ["Town Centre", "Local Temple", "Bus Stand"],
|
| 160 |
+
"petrol_bunks": ["HP Petrol Bunk", "Indian Oil Bunk", "Local Petrol Station"],
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 164 |
+
# GRAPH
|
| 165 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 166 |
+
def haversine_km(lat1, lon1, lat2, lon2):
|
| 167 |
+
R = 6371.0
|
| 168 |
+
phi1, phi2 = math.radians(lat1), math.radians(lat2)
|
| 169 |
+
dphi = math.radians(lat2 - lat1)
|
| 170 |
+
dlam = math.radians(lon2 - lon1)
|
| 171 |
+
a = math.sin(dphi/2)**2 + math.cos(phi1)*math.cos(phi2)*math.sin(dlam/2)**2
|
| 172 |
+
return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
| 173 |
+
|
| 174 |
+
def build_graph(max_dist_km=150.0):
|
| 175 |
+
G = nx.Graph()
|
| 176 |
+
city_list = list(AP_CITIES.items())
|
| 177 |
+
for name, (lat, lon) in city_list:
|
| 178 |
+
G.add_node(name, lat=lat, lon=lon)
|
| 179 |
+
for i in range(len(city_list)):
|
| 180 |
+
n1, (la1, lo1) = city_list[i]
|
| 181 |
+
for j in range(i + 1, len(city_list)):
|
| 182 |
+
n2, (la2, lo2) = city_list[j]
|
| 183 |
+
d = haversine_km(la1, lo1, la2, lo2)
|
| 184 |
+
if d <= max_dist_km:
|
| 185 |
+
road_d = round(d * 1.30, 1)
|
| 186 |
+
G.add_edge(n1, n2, weight=road_d)
|
| 187 |
+
return G
|
| 188 |
+
|
| 189 |
+
G = build_graph()
|
| 190 |
+
ALL_CITIES = sorted(AP_CITIES.keys())
|
| 191 |
+
|
| 192 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 193 |
+
# ALGORITHMS
|
| 194 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 195 |
+
def run_dijkstra(G, source, target):
|
| 196 |
+
import time
|
| 197 |
+
t0 = time.perf_counter()
|
| 198 |
+
|
| 199 |
+
try:
|
| 200 |
+
path = nx.dijkstra_path(G, source, target, weight="weight")
|
| 201 |
+
cost = nx.dijkstra_path_length(G, source, target, weight="weight")
|
| 202 |
+
except Exception as e:
|
| 203 |
+
return {"path": [], "cost": None, "time_ms": 0, "error": str(e)}
|
| 204 |
+
|
| 205 |
+
elapsed = (time.perf_counter() - t0) * 1000
|
| 206 |
+
|
| 207 |
+
return {
|
| 208 |
+
"path": path,
|
| 209 |
+
"cost": round(cost, 1),
|
| 210 |
+
"time_ms": round(elapsed, 3), # ✅ FIX
|
| 211 |
+
"error": None
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
def _collect_paths(G, source, target, max_paths=256):
|
| 215 |
+
try:
|
| 216 |
+
optimal = nx.dijkstra_path(G, source, target, weight="weight")
|
| 217 |
+
except:
|
| 218 |
+
optimal = None
|
| 219 |
+
paths = []
|
| 220 |
+
for p in nx.all_simple_paths(G, source, target, cutoff=7):
|
| 221 |
+
paths.append(p)
|
| 222 |
+
if len(paths) >= max_paths:
|
| 223 |
+
break
|
| 224 |
+
if optimal and optimal not in paths:
|
| 225 |
+
paths.insert(0, optimal)
|
| 226 |
+
return paths
|
| 227 |
+
|
| 228 |
+
def _path_cost(G, path):
|
| 229 |
+
return sum(G[path[i]][path[i+1]]["weight"] for i in range(len(path)-1))
|
| 230 |
+
|
| 231 |
+
def run_grover(G, source, target, max_paths=200):
|
| 232 |
+
import time
|
| 233 |
+
t0 = time.perf_counter()
|
| 234 |
+
|
| 235 |
+
paths = _collect_paths(G, source, target, max_paths)
|
| 236 |
+
N = len(paths)
|
| 237 |
+
|
| 238 |
+
if N == 0:
|
| 239 |
+
return {"path": [], "cost": None, "time_ms": 0, "oracle_calls": 0, "error": "No paths"}
|
| 240 |
+
|
| 241 |
+
costs = np.array([_path_cost(G, p) for p in paths], dtype=np.float64)
|
| 242 |
+
optimal_cost = costs.min()
|
| 243 |
+
|
| 244 |
+
threshold = optimal_cost * 1.10
|
| 245 |
+
oracle_mask = (costs <= threshold).astype(np.float64)
|
| 246 |
+
|
| 247 |
+
amplitudes = np.ones(N, dtype=np.float64) / math.sqrt(N)
|
| 248 |
+
num_iter = max(1, round((math.pi / 4) * math.sqrt(N)))
|
| 249 |
+
|
| 250 |
+
oracle_calls = 0 # ✅ NEW
|
| 251 |
+
|
| 252 |
+
for _ in range(num_iter):
|
| 253 |
+
oracle_calls += 1
|
| 254 |
+
|
| 255 |
+
amplitudes = amplitudes * (1 - 2 * oracle_mask)
|
| 256 |
+
mean_amp = amplitudes.mean()
|
| 257 |
+
amplitudes = 2 * mean_amp - amplitudes
|
| 258 |
+
|
| 259 |
+
norm = np.linalg.norm(amplitudes)
|
| 260 |
+
if norm > 1e-12:
|
| 261 |
+
amplitudes /= norm
|
| 262 |
+
|
| 263 |
+
probs = amplitudes ** 2
|
| 264 |
+
probs = np.clip(probs, 0, None)
|
| 265 |
+
probs /= probs.sum()
|
| 266 |
+
|
| 267 |
+
idx = int(np.random.choice(N, p=probs))
|
| 268 |
+
|
| 269 |
+
elapsed = (time.perf_counter() - t0) * 1000
|
| 270 |
+
|
| 271 |
+
return {
|
| 272 |
+
"path": paths[idx],
|
| 273 |
+
"cost": round(costs[idx], 1),
|
| 274 |
+
"time_ms": round(elapsed, 3), # ✅ NEW
|
| 275 |
+
"oracle_calls": oracle_calls, # ✅ NEW
|
| 276 |
+
"error": None
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
def get_all_paths(G, source, target, limit=8):
|
| 280 |
+
"""Get several simple paths sorted by cost."""
|
| 281 |
+
all_paths = []
|
| 282 |
+
try:
|
| 283 |
+
for p in nx.all_simple_paths(G, source, target, cutoff=6):
|
| 284 |
+
cost = _path_cost(G, p)
|
| 285 |
+
all_paths.append((p, round(cost, 1)))
|
| 286 |
+
if len(all_paths) >= limit:
|
| 287 |
+
break
|
| 288 |
+
except:
|
| 289 |
+
pass
|
| 290 |
+
all_paths.sort(key=lambda x: x[1])
|
| 291 |
+
return all_paths
|
| 292 |
+
|
| 293 |
+
def combined_shortest_path(G, source, target):
|
| 294 |
+
d = run_dijkstra(G, source, target)
|
| 295 |
+
g = run_grover(G, source, target)
|
| 296 |
+
|
| 297 |
+
if d["error"] and g["error"]:
|
| 298 |
+
return None, None, None
|
| 299 |
+
|
| 300 |
+
candidates = []
|
| 301 |
+
if not d["error"]:
|
| 302 |
+
candidates.append((d["path"], d["cost"]))
|
| 303 |
+
if not g["error"]:
|
| 304 |
+
candidates.append((g["path"], g["cost"]))
|
| 305 |
+
|
| 306 |
+
candidates.sort(key=lambda x: x[1])
|
| 307 |
+
|
| 308 |
+
comparison = {
|
| 309 |
+
"dijkstra_time": d.get("time_ms", 0),
|
| 310 |
+
"grover_time": g.get("time_ms", 0),
|
| 311 |
+
"grover_oracle_calls": g.get("oracle_calls", 0)
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
return candidates[0][0], candidates[0][1], comparison
|
| 315 |
+
|
| 316 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 317 |
+
# MAP BUILDER (Folium)
|
| 318 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 319 |
+
def build_folium_map(source, target, shortest_path, all_paths):
|
| 320 |
+
try:
|
| 321 |
+
import folium
|
| 322 |
+
except ImportError:
|
| 323 |
+
return "<p style='color:red'>Install folium: pip install folium</p>"
|
| 324 |
+
|
| 325 |
+
src_ll = AP_CITIES.get(source, (16.0, 80.5))
|
| 326 |
+
tgt_ll = AP_CITIES.get(target, (16.0, 80.5))
|
| 327 |
+
centre = ((src_ll[0]+tgt_ll[0])/2, (src_ll[1]+tgt_ll[1])/2)
|
| 328 |
+
|
| 329 |
+
m = folium.Map(location=centre, zoom_start=7,
|
| 330 |
+
tiles="CartoDB positron", control_scale=True)
|
| 331 |
+
|
| 332 |
+
# Draw all paths (faint)
|
| 333 |
+
sp_set = set(zip(shortest_path[:-1], shortest_path[1:])) if shortest_path else set()
|
| 334 |
+
drawn_edges = set()
|
| 335 |
+
for path, cost in all_paths:
|
| 336 |
+
is_shortest = (path == shortest_path)
|
| 337 |
+
for i in range(len(path)-1):
|
| 338 |
+
u, v = path[i], path[i+1]
|
| 339 |
+
key = frozenset([u, v])
|
| 340 |
+
if key in drawn_edges and not is_shortest:
|
| 341 |
+
continue
|
| 342 |
+
drawn_edges.add(key)
|
| 343 |
+
ll_u = AP_CITIES.get(u)
|
| 344 |
+
ll_v = AP_CITIES.get(v)
|
| 345 |
+
if not ll_u or not ll_v:
|
| 346 |
+
continue
|
| 347 |
+
if is_shortest:
|
| 348 |
+
folium.PolyLine([ll_u, ll_v], color="#FF6B35", weight=6,
|
| 349 |
+
opacity=0.95, tooltip=f"{u}→{v}").add_to(m)
|
| 350 |
+
else:
|
| 351 |
+
folium.PolyLine([ll_u, ll_v], color="#6B8FD4", weight=2.5,
|
| 352 |
+
opacity=0.5, dash_array="8 4",
|
| 353 |
+
tooltip=f"{u}→{v}").add_to(m)
|
| 354 |
+
|
| 355 |
+
# All graph edges (very faint background)
|
| 356 |
+
for u, v, data in G.edges(data=True):
|
| 357 |
+
key = frozenset([u, v])
|
| 358 |
+
if key in drawn_edges:
|
| 359 |
+
continue
|
| 360 |
+
ll_u = AP_CITIES.get(u)
|
| 361 |
+
ll_v = AP_CITIES.get(v)
|
| 362 |
+
if not ll_u or not ll_v:
|
| 363 |
+
continue
|
| 364 |
+
folium.PolyLine([ll_u, ll_v], color="#CCCCCC", weight=1,
|
| 365 |
+
opacity=0.15).add_to(m)
|
| 366 |
+
|
| 367 |
+
# Markers for cities on path
|
| 368 |
+
path_cities = set(shortest_path) if shortest_path else set()
|
| 369 |
+
for city in path_cities:
|
| 370 |
+
ll = AP_CITIES.get(city)
|
| 371 |
+
if not ll:
|
| 372 |
+
continue
|
| 373 |
+
pois = CITY_POIS.get(city, DEFAULT_POIS)
|
| 374 |
+
if city == source:
|
| 375 |
+
icon_color, icon_name = "green", "play"
|
| 376 |
+
elif city == target:
|
| 377 |
+
icon_color, icon_name = "red", "flag"
|
| 378 |
+
else:
|
| 379 |
+
icon_color, icon_name = "orange", "map-marker"
|
| 380 |
+
|
| 381 |
+
popup_html = f"""
|
| 382 |
+
<div style='font-family:Segoe UI,sans-serif;min-width:200px;max-width:260px'>
|
| 383 |
+
<h3 style='margin:0 0 8px;color:#1a1a2e;font-size:16px'>📍 {city}</h3>
|
| 384 |
+
<div style='background:#f8f9fa;border-radius:6px;padding:8px;margin-bottom:6px'>
|
| 385 |
+
<b style='color:#e63946'>🏬 Malls</b><br>
|
| 386 |
+
{'<br>'.join('• '+m for m in pois.get('malls',['—'])[:3])}
|
| 387 |
+
</div>
|
| 388 |
+
<div style='background:#f8f9fa;border-radius:6px;padding:8px;margin-bottom:6px'>
|
| 389 |
+
<b style='color:#2d6a4f'>🍽️ Restaurants</b><br>
|
| 390 |
+
{'<br>'.join('• '+r for r in pois.get('restaurants',['—'])[:3])}
|
| 391 |
+
</div>
|
| 392 |
+
<div style='background:#f8f9fa;border-radius:6px;padding:8px;margin-bottom:6px'>
|
| 393 |
+
<b style='color:#e9c46a'>⛽ Petrol Bunks</b><br>
|
| 394 |
+
{'<br>'.join('• '+b for b in pois.get('petrol_bunks',['—'])[:3])}
|
| 395 |
+
</div>
|
| 396 |
+
<div style='background:#f8f9fa;border-radius:6px;padding:8px'>
|
| 397 |
+
<b style='color:#457b9d'>🏛️ Landmarks</b><br>
|
| 398 |
+
{'<br>'.join('• '+l for l in pois.get('landmarks',['—'])[:2])}
|
| 399 |
+
</div>
|
| 400 |
+
</div>"""
|
| 401 |
+
|
| 402 |
+
folium.Marker(
|
| 403 |
+
location=ll,
|
| 404 |
+
tooltip=f"📍 {city} — click for info",
|
| 405 |
+
popup=folium.Popup(popup_html, max_width=280),
|
| 406 |
+
icon=folium.Icon(color=icon_color, icon=icon_name, prefix="fa"),
|
| 407 |
+
).add_to(m)
|
| 408 |
+
|
| 409 |
+
# Legend
|
| 410 |
+
legend = """
|
| 411 |
+
<div style="position:fixed;bottom:25px;left:25px;z-index:1000;
|
| 412 |
+
background:white;border:1px solid #ddd;border-radius:10px;
|
| 413 |
+
padding:12px 16px;font-family:Segoe UI,sans-serif;font-size:12px;box-shadow:0 2px 8px rgba(0,0,0,.15)">
|
| 414 |
+
<b style="font-size:13px">🗺️ Legend</b><br><br>
|
| 415 |
+
<span style="color:#FF6B35;font-weight:700">━━━</span> Shortest Path<br>
|
| 416 |
+
<span style="color:#6B8FD4">╌╌╌</span> Alternate Paths<br>
|
| 417 |
+
<span style="color:#aaa">━━━</span> Road Network<br><br>
|
| 418 |
+
🟢 Start 🔴 End 🟠 Via
|
| 419 |
+
</div>"""
|
| 420 |
+
m.get_root().html.add_child(folium.Element(legend))
|
| 421 |
+
return m._repr_html_()
|
| 422 |
+
|
| 423 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 424 |
+
# RESULT BUILDERS
|
| 425 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 426 |
+
def build_path_display(shortest_path, shortest_cost, all_paths):
|
| 427 |
+
if not shortest_path:
|
| 428 |
+
return "<div style='padding:20px;color:#e63946;font-size:16px'>❌ No route found between selected cities.</div>"
|
| 429 |
+
|
| 430 |
+
# Shortest path details
|
| 431 |
+
hops_html = ""
|
| 432 |
+
running = 0
|
| 433 |
+
for i in range(len(shortest_path)-1):
|
| 434 |
+
u, v = shortest_path[i], shortest_path[i+1]
|
| 435 |
+
w = G[u][v]["weight"]
|
| 436 |
+
running += w
|
| 437 |
+
hops_html += f"""
|
| 438 |
+
<div style="display:flex;align-items:center;gap:10px;padding:6px 0;border-bottom:1px solid #f0f0f0">
|
| 439 |
+
<span style="background:#FF6B35;color:white;border-radius:50%;width:24px;height:24px;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:700;flex-shrink:0">{i+1}</span>
|
| 440 |
+
<span style="font-weight:600;color:#1a1a2e">{u}</span>
|
| 441 |
+
<span style="color:#aaa">→</span>
|
| 442 |
+
<span style="font-weight:600;color:#1a1a2e">{v}</span>
|
| 443 |
+
<span style="margin-left:auto;font-size:12px;color:#666;background:#f5f5f5;padding:2px 8px;border-radius:10px">{w} km</span>
|
| 444 |
+
<span style="font-size:11px;color:#999">({round(running,1)} total)</span>
|
| 445 |
+
</div>"""
|
| 446 |
+
|
| 447 |
+
# All paths list
|
| 448 |
+
paths_html = ""
|
| 449 |
+
for idx, (path, cost) in enumerate(all_paths):
|
| 450 |
+
is_best = (path == shortest_path)
|
| 451 |
+
badge = '<span style="background:#FF6B35;color:white;border-radius:8px;padding:1px 8px;font-size:11px;margin-left:8px">★ SHORTEST</span>' if is_best else ""
|
| 452 |
+
row_bg = "#fff8f5" if is_best else "white"
|
| 453 |
+
path_str = " → ".join(path)
|
| 454 |
+
paths_html += f"""
|
| 455 |
+
<div style="background:{row_bg};border:1px solid {'#FF6B35' if is_best else '#e9ecef'};border-radius:8px;
|
| 456 |
+
padding:10px 14px;margin-bottom:8px">
|
| 457 |
+
<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:4px">
|
| 458 |
+
<span style="font-weight:700;color:#1a1a2e;font-size:13px">Path {idx+1}{badge}</span>
|
| 459 |
+
<span style="font-weight:700;color:#FF6B35;font-size:15px">{cost} km</span>
|
| 460 |
+
</div>
|
| 461 |
+
<div style="font-size:12px;color:#555;line-height:1.6">{path_str}</div>
|
| 462 |
+
<div style="font-size:11px;color:#999;margin-top:4px">{len(path)-1} hops · {len(path)} cities</div>
|
| 463 |
+
</div>"""
|
| 464 |
+
|
| 465 |
+
html = f"""
|
| 466 |
+
<div style="font-family:'Segoe UI',sans-serif;max-width:100%">
|
| 467 |
+
|
| 468 |
+
<!-- SHORTEST PATH CARD -->
|
| 469 |
+
<div style="background:linear-gradient(135deg,#fff8f5,#fff);border:2px solid #FF6B35;
|
| 470 |
+
border-radius:14px;padding:20px;margin-bottom:20px;box-shadow:0 4px 20px rgba(255,107,53,.12)">
|
| 471 |
+
<div style="display:flex;align-items:center;gap:10px;margin-bottom:14px">
|
| 472 |
+
<span style="font-size:28px">🏆</span>
|
| 473 |
+
<div>
|
| 474 |
+
<div style="font-size:18px;font-weight:800;color:#1a1a2e">Recommended Route</div>
|
| 475 |
+
<div style="font-size:13px;color:#666">Optimal path from {shortest_path[0]} to {shortest_path[-1]}</div>
|
| 476 |
+
</div>
|
| 477 |
+
<div style="margin-left:auto;text-align:right">
|
| 478 |
+
<div style="font-size:28px;font-weight:900;color:#FF6B35">{shortest_cost} km</div>
|
| 479 |
+
<div style="font-size:12px;color:#999">{len(shortest_path)-1} hops</div>
|
| 480 |
+
</div>
|
| 481 |
+
</div>
|
| 482 |
+
<div style="background:white;border-radius:10px;padding:14px;border:1px solid #ffe5d9">
|
| 483 |
+
{hops_html}
|
| 484 |
+
</div>
|
| 485 |
+
<div style="margin-top:12px;font-size:13px;color:#888;display:flex;gap:6px;align-items:center">
|
| 486 |
+
<span>🛣️</span>
|
| 487 |
+
<span>Route: {' → '.join(shortest_path)}</span>
|
| 488 |
+
</div>
|
| 489 |
+
</div>
|
| 490 |
+
|
| 491 |
+
<!-- ALL PATHS -->
|
| 492 |
+
<div style="background:white;border:1px solid #e9ecef;border-radius:14px;padding:20px">
|
| 493 |
+
<div style="font-size:15px;font-weight:700;color:#1a1a2e;margin-bottom:14px;display:flex;align-items:center;gap:8px">
|
| 494 |
+
<span>🛤️</span> All Available Paths ({len(all_paths)} found)
|
| 495 |
+
</div>
|
| 496 |
+
{paths_html}
|
| 497 |
+
</div>
|
| 498 |
+
</div>"""
|
| 499 |
+
return html
|
| 500 |
+
|
| 501 |
+
def build_poi_panel(city):
|
| 502 |
+
pois = CITY_POIS.get(city, DEFAULT_POIS)
|
| 503 |
+
|
| 504 |
+
def section(icon, label, color, items):
|
| 505 |
+
pills = "".join(f'<span style="background:{color}15;color:{color};border:1px solid {color}40;border-radius:20px;padding:4px 12px;font-size:12px;display:inline-block;margin:3px">{item}</span>' for item in items)
|
| 506 |
+
return f"""
|
| 507 |
+
<div style="margin-bottom:16px">
|
| 508 |
+
<div style="font-weight:700;color:#1a1a2e;margin-bottom:8px;font-size:13px">{icon} {label}</div>
|
| 509 |
+
<div>{pills}</div>
|
| 510 |
+
</div>"""
|
| 511 |
+
|
| 512 |
+
html = f"""
|
| 513 |
+
<div style="font-family:'Segoe UI',sans-serif;padding:4px">
|
| 514 |
+
<h3 style="color:#1a1a2e;margin:0 0 16px;font-size:16px;border-bottom:2px solid #FF6B35;padding-bottom:8px">
|
| 515 |
+
📍 {city} — Points of Interest
|
| 516 |
+
</h3>
|
| 517 |
+
{section('⛽', 'Petrol Bunks', '#e9c46a', pois.get('petrol_bunks', ['—']))}
|
| 518 |
+
{section('🏬', 'Malls & Shopping', '#e63946', pois.get('malls', ['—']))}
|
| 519 |
+
{section('🍽️', 'Restaurants', '#2d6a4f', pois.get('restaurants', ['—']))}
|
| 520 |
+
{section('🏛️', 'Landmarks', '#457b9d', pois.get('landmarks', ['—']))}
|
| 521 |
+
</div>"""
|
| 522 |
+
return html
|
| 523 |
+
|
| 524 |
+
def build_stats_bar(source, target, shortest_path, shortest_cost, all_paths):
|
| 525 |
+
nearby_src = sorted(
|
| 526 |
+
[(n, G[source][n]["weight"]) for n in G.neighbors(source)],
|
| 527 |
+
key=lambda x: x[1]
|
| 528 |
+
)[:3]
|
| 529 |
+
nearby_tgt = sorted(
|
| 530 |
+
[(n, G[target][n]["weight"]) for n in G.neighbors(target)],
|
| 531 |
+
key=lambda x: x[1]
|
| 532 |
+
)[:3]
|
| 533 |
+
|
| 534 |
+
nearby_src_str = ", ".join(f"{c} ({d}km)" for c,d in nearby_src)
|
| 535 |
+
nearby_tgt_str = ", ".join(f"{c} ({d}km)" for c,d in nearby_tgt)
|
| 536 |
+
|
| 537 |
+
html = f"""
|
| 538 |
+
<div style="font-family:'Segoe UI',sans-serif;display:grid;grid-template-columns:repeat(4,1fr);gap:12px;padding:4px">
|
| 539 |
+
<div style="background:#fff8f5;border:1px solid #ffe5d9;border-radius:10px;padding:14px;text-align:center">
|
| 540 |
+
<div style="font-size:22px;font-weight:900;color:#FF6B35">{shortest_cost}</div>
|
| 541 |
+
<div style="font-size:11px;color:#999;text-transform:uppercase;letter-spacing:.5px">km (shortest)</div>
|
| 542 |
+
</div>
|
| 543 |
+
<div style="background:#f5f8ff;border:1px solid #dce8ff;border-radius:10px;padding:14px;text-align:center">
|
| 544 |
+
<div style="font-size:22px;font-weight:900;color:#4361ee">{len(all_paths)}</div>
|
| 545 |
+
<div style="font-size:11px;color:#999;text-transform:uppercase;letter-spacing:.5px">paths found</div>
|
| 546 |
+
</div>
|
| 547 |
+
<div style="background:#f5fff8;border:1px solid #d4edda;border-radius:10px;padding:14px;text-align:center">
|
| 548 |
+
<div style="font-size:22px;font-weight:900;color:#2d6a4f">{len(shortest_path)-1}</div>
|
| 549 |
+
<div style="font-size:11px;color:#999;text-transform:uppercase;letter-spacing:.5px">hops on route</div>
|
| 550 |
+
</div>
|
| 551 |
+
<div style="background:#fffbf5;border:1px solid #fff3cd;border-radius:10px;padding:14px;text-align:center">
|
| 552 |
+
<div style="font-size:22px;font-weight:900;color:#e9c46a">{G.number_of_nodes()}</div>
|
| 553 |
+
<div style="font-size:11px;color:#999;text-transform:uppercase;letter-spacing:.5px">cities in graph</div>
|
| 554 |
+
</div>
|
| 555 |
+
</div>
|
| 556 |
+
<div style="font-family:'Segoe UI',sans-serif;margin-top:10px;display:grid;grid-template-columns:1fr 1fr;gap:12px">
|
| 557 |
+
<div style="background:#f9f9f9;border-radius:10px;padding:12px;font-size:12px;color:#555">
|
| 558 |
+
<b style="color:#1a1a2e">Near {source}:</b> {nearby_src_str}
|
| 559 |
+
</div>
|
| 560 |
+
<div style="background:#f9f9f9;border-radius:10px;padding:12px;font-size:12px;color:#555">
|
| 561 |
+
<b style="color:#1a1a2e">Near {target}:</b> {nearby_tgt_str}
|
| 562 |
+
</div>
|
| 563 |
+
</div>"""
|
| 564 |
+
return html
|
| 565 |
+
|
| 566 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 567 |
+
# MAIN FIND ROUTE FUNCTION
|
| 568 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 569 |
+
def find_route(source, target):
|
| 570 |
+
if not source or not target:
|
| 571 |
+
return (
|
| 572 |
+
"<div style='padding:20px;color:#e63946'>Please select both Source and Destination.</div>",
|
| 573 |
+
"<div style='padding:20px;color:#999'>Select cities and click Find Route.</div>",
|
| 574 |
+
"<div style='padding:20px;color:#999'>POI info will appear here.</div>",
|
| 575 |
+
"<div style='padding:20px;color:#999'>Stats will appear here.</div>",
|
| 576 |
+
"<div style='padding:20px;color:#999'>POI info will appear here.</div>",
|
| 577 |
+
)
|
| 578 |
+
if source == target:
|
| 579 |
+
return (
|
| 580 |
+
"<div style='padding:20px;color:#e63946'>Source and Destination must be different cities.</div>",
|
| 581 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 582 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 583 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 584 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 585 |
+
)
|
| 586 |
+
|
| 587 |
+
shortest_path, shortest_cost, comparison = combined_shortest_path(G, source, target)
|
| 588 |
+
all_paths = get_all_paths(G, source, target, limit=8)
|
| 589 |
+
|
| 590 |
+
if not shortest_path:
|
| 591 |
+
return (
|
| 592 |
+
"<div style='padding:20px;color:#e63946'>No route found. Cities may not be connected.</div>",
|
| 593 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 594 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 595 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 596 |
+
"<div style='padding:20px;color:#999'>—</div>",
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
path_html = build_path_display(shortest_path, shortest_cost, all_paths)
|
| 600 |
+
map_html = build_folium_map(source, target, shortest_path, all_paths)
|
| 601 |
+
poi_src = build_poi_panel(source)
|
| 602 |
+
poi_tgt = build_poi_panel(target)
|
| 603 |
+
stats_html = build_stats_bar(source, target, shortest_path, shortest_cost, all_paths)
|
| 604 |
+
comparison_html = f"""
|
| 605 |
+
<div style="background:white;border:2px solid #FF6B35;border-radius:12px;padding:16px;margin-top:12px">
|
| 606 |
+
|
| 607 |
+
<h3 style="margin:0 0 12px;color:#1a1a2e !important;font-weight:800">
|
| 608 |
+
⚖️ Algorithm Comparison
|
| 609 |
+
</h3>
|
| 610 |
+
|
| 611 |
+
<div style="display:flex;justify-content:space-between;margin-bottom:10px;font-size:15px">
|
| 612 |
+
<span style="color:#000 !important;font-weight:800">Dijkstra Time</span>
|
| 613 |
+
<span style="color:#FF6B35;font-weight:800">{comparison['dijkstra_time']} ms</span>
|
| 614 |
+
</div>
|
| 615 |
+
|
| 616 |
+
<div style="display:flex;justify-content:space-between;margin-bottom:10px;font-size:15px">
|
| 617 |
+
<span style="color:#000 !important;font-weight:800">Grover Time</span>
|
| 618 |
+
<span style="color:#4361ee;font-weight:800">{comparison['grover_time']} ms</span>
|
| 619 |
+
</div>
|
| 620 |
+
|
| 621 |
+
<div style="display:flex;justify-content:space-between;font-size:15px">
|
| 622 |
+
<span style="color:#000 !important;font-weight:800">Grover Oracle Calls</span>
|
| 623 |
+
<span style="color:#2d6a4f;font-weight:800">{comparison['grover_oracle_calls']}</span>
|
| 624 |
+
</div>
|
| 625 |
+
|
| 626 |
+
</div>
|
| 627 |
+
"""
|
| 628 |
+
|
| 629 |
+
return map_html, path_html, poi_src, stats_html + comparison_html, poi_tgt
|
| 630 |
+
|
| 631 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 632 |
+
# GRADIO UI
|
| 633 |
+
# ══════════════════════════════════════════════════════════════════════════════
|
| 634 |
+
CSS = """
|
| 635 |
+
body, .gradio-container {
|
| 636 |
+
font-family: 'Segoe UI', system-ui, sans-serif !important;
|
| 637 |
+
background: #f7f8fc !important;
|
| 638 |
+
}
|
| 639 |
+
.gradio-container {
|
| 640 |
+
max-width: 1400px !important;
|
| 641 |
+
margin: 0 auto !important;
|
| 642 |
+
}
|
| 643 |
+
.app-header {
|
| 644 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
|
| 645 |
+
border-radius: 16px;
|
| 646 |
+
padding: 28px 36px;
|
| 647 |
+
margin-bottom: 20px;
|
| 648 |
+
color: white;
|
| 649 |
+
display: flex;
|
| 650 |
+
align-items: center;
|
| 651 |
+
gap: 20px;
|
| 652 |
+
}
|
| 653 |
+
#find-btn {
|
| 654 |
+
background: linear-gradient(135deg, #FF6B35, #e63946) !important;
|
| 655 |
+
color: white !important;
|
| 656 |
+
font-weight: 700 !important;
|
| 657 |
+
font-size: 16px !important;
|
| 658 |
+
border-radius: 12px !important;
|
| 659 |
+
border: none !important;
|
| 660 |
+
padding: 14px !important;
|
| 661 |
+
box-shadow: 0 4px 20px rgba(255,107,53,.4) !important;
|
| 662 |
+
transition: all .2s !important;
|
| 663 |
+
}
|
| 664 |
+
#find-btn:hover {
|
| 665 |
+
transform: translateY(-2px) !important;
|
| 666 |
+
box-shadow: 0 6px 28px rgba(255,107,53,.55) !important;
|
| 667 |
+
}
|
| 668 |
+
.section-label {
|
| 669 |
+
font-weight: 700;
|
| 670 |
+
font-size: 13px;
|
| 671 |
+
color: #1a1a2e;
|
| 672 |
+
text-transform: uppercase;
|
| 673 |
+
letter-spacing: .8px;
|
| 674 |
+
margin-bottom: 6px;
|
| 675 |
+
}
|
| 676 |
+
.gr-dropdown {
|
| 677 |
+
border-radius: 10px !important;
|
| 678 |
+
border: 1.5px solid #dee2e6 !important;
|
| 679 |
+
}
|
| 680 |
+
footer { display: none !important; }
|
| 681 |
+
"""
|
| 682 |
+
|
| 683 |
+
HEADER_HTML = """
|
| 684 |
+
<div style="background:linear-gradient(135deg,#1a1a2e,#16213e,#0f3460);border-radius:16px;
|
| 685 |
+
padding:24px 32px;margin-bottom:4px;color:white;display:flex;align-items:center;gap:18px">
|
| 686 |
+
<span style="font-size:48px">🗺️</span>
|
| 687 |
+
<div>
|
| 688 |
+
<div style="font-size:26px;font-weight:900;letter-spacing:-0.5px">AP Smart Route Finder</div>
|
| 689 |
+
<div style="font-size:13px;color:#a8c6fa;margin-top:4px">
|
| 690 |
+
Andhra Pradesh · All-Paths Route Planner · Real-time Map · POI Explorer
|
| 691 |
+
</div>
|
| 692 |
+
<div style="margin-top:8px;display:flex;gap:8px;flex-wrap:wrap">
|
| 693 |
+
<span style="background:#FF6B3530;color:#FF6B35;border:1px solid #FF6B3560;border-radius:6px;padding:2px 10px;font-size:11px">⚡ Smart Routing</span>
|
| 694 |
+
<span style="background:#4361ee30;color:#a8c6fa;border:1px solid #4361ee60;border-radius:6px;padding:2px 10px;font-size:11px">🗺️ Live OSM Map</span>
|
| 695 |
+
<span style="background:#2d6a4f30;color:#95d5b2;border:1px solid #2d6a4f60;border-radius:6px;padding:2px 10px;font-size:11px">⛽ Petrol Bunks</span>
|
| 696 |
+
<span style="background:#e9c46a30;color:#e9c46a;border:1px solid #e9c46a60;border-radius:6px;padding:2px 10px;font-size:11px">🏬 Malls & Restaurants</span>
|
| 697 |
+
</div>
|
| 698 |
+
</div>
|
| 699 |
+
</div>
|
| 700 |
+
"""
|
| 701 |
+
|
| 702 |
+
with gr.Blocks(css=CSS, title="AP Route Finder") as demo:
|
| 703 |
+
|
| 704 |
+
gr.HTML(HEADER_HTML)
|
| 705 |
+
|
| 706 |
+
with gr.Row():
|
| 707 |
+
with gr.Column(scale=1, min_width=220):
|
| 708 |
+
src_dd = gr.Dropdown(
|
| 709 |
+
choices=ALL_CITIES,
|
| 710 |
+
label="🚀 Starting City",
|
| 711 |
+
value="Vijayawada",
|
| 712 |
+
interactive=True,
|
| 713 |
+
)
|
| 714 |
+
with gr.Column(scale=1, min_width=220):
|
| 715 |
+
tgt_dd = gr.Dropdown(
|
| 716 |
+
choices=ALL_CITIES,
|
| 717 |
+
label="🏁 Destination City",
|
| 718 |
+
value="Visakhapatnam",
|
| 719 |
+
interactive=True,
|
| 720 |
+
)
|
| 721 |
+
with gr.Column(scale=1, min_width=180):
|
| 722 |
+
find_btn = gr.Button("🔍 Find Best Route", elem_id="find-btn", size="lg")
|
| 723 |
+
|
| 724 |
+
stats_out = gr.HTML(
|
| 725 |
+
value="<div style='padding:14px;color:#999;font-family:Segoe UI,sans-serif;text-align:center'>Select cities above and click Find Best Route</div>",
|
| 726 |
+
label="",
|
| 727 |
+
)
|
| 728 |
+
|
| 729 |
+
with gr.Row():
|
| 730 |
+
with gr.Column(scale=3):
|
| 731 |
+
gr.HTML("<div class='section-label'>🗺️ Interactive Map — click city markers for POIs</div>")
|
| 732 |
+
map_out = gr.HTML(
|
| 733 |
+
value="<div style='height:500px;background:#f0f4f8;border-radius:14px;display:flex;align-items:center;justify-content:center;color:#aaa;font-size:16px;font-family:Segoe UI'>Map will load here after searching</div>",
|
| 734 |
+
)
|
| 735 |
+
|
| 736 |
+
with gr.Column(scale=2):
|
| 737 |
+
gr.HTML("<div class='section-label'>🛤️ Route Details & All Paths</div>")
|
| 738 |
+
path_out = gr.HTML(
|
| 739 |
+
value="<div style='padding:20px;color:#aaa;font-family:Segoe UI'>Route info will appear here</div>",
|
| 740 |
+
)
|
| 741 |
+
|
| 742 |
+
with gr.Row():
|
| 743 |
+
with gr.Column():
|
| 744 |
+
gr.HTML("<div class='section-label'>📍 Source City — Nearby Places</div>")
|
| 745 |
+
poi_src_out = gr.HTML(
|
| 746 |
+
value="<div style='padding:20px;color:#aaa;font-family:Segoe UI'>Select a source city</div>",
|
| 747 |
+
)
|
| 748 |
+
with gr.Column():
|
| 749 |
+
gr.HTML("<div class='section-label'>📍 Destination City — Nearby Places</div>")
|
| 750 |
+
poi_tgt_out = gr.HTML(
|
| 751 |
+
value="<div style='padding:20px;color:#aaa;font-family:Segoe UI'>Select a destination city</div>",
|
| 752 |
+
)
|
| 753 |
+
|
| 754 |
+
gr.HTML("""
|
| 755 |
+
<div style='text-align:center;font-family:Segoe UI,sans-serif;font-size:11px;color:#aaa;padding:16px 0 4px'>
|
| 756 |
+
AP Smart Route Finder · 70+ Andhra Pradesh cities · Smart path optimization · OpenStreetMap tiles
|
| 757 |
+
</div>""")
|
| 758 |
+
|
| 759 |
+
find_btn.click(
|
| 760 |
+
fn=find_route,
|
| 761 |
+
inputs=[src_dd, tgt_dd],
|
| 762 |
+
outputs=[map_out, path_out, poi_src_out, stats_out, poi_tgt_out],
|
| 763 |
+
)
|
| 764 |
+
|
| 765 |
+
if __name__ == "__main__":
|
| 766 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|
graph_builder.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
graph_builder.py
|
| 3 |
+
Builds an Andhra Pradesh road graph with 80+ cities/towns.
|
| 4 |
+
Edge weight = haversine × 1.30 road-winding factor.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import math
|
| 8 |
+
import networkx as nx
|
| 9 |
+
|
| 10 |
+
# ── All AP cities/towns with lat/lon ─────────────────────────────────────────
|
| 11 |
+
AP_CITIES = {
|
| 12 |
+
# Major cities
|
| 13 |
+
"Visakhapatnam": (17.6868, 83.2185),
|
| 14 |
+
"Vijayawada": (16.5062, 80.6480),
|
| 15 |
+
"Guntur": (16.3067, 80.4365),
|
| 16 |
+
"Nellore": (14.4426, 79.9865),
|
| 17 |
+
"Kurnool": (15.8281, 78.0373),
|
| 18 |
+
"Rajahmundry": (16.9891, 81.7837),
|
| 19 |
+
"Tirupati": (13.6288, 79.4192),
|
| 20 |
+
"Kadapa": (14.4674, 78.8241),
|
| 21 |
+
"Kakinada": (16.9891, 82.2475),
|
| 22 |
+
"Anantapur": (14.6819, 77.6006),
|
| 23 |
+
# District HQs and major towns
|
| 24 |
+
"Amaravati": (16.5730, 80.3564),
|
| 25 |
+
"Eluru": (16.7107, 81.0952),
|
| 26 |
+
"Ongole": (15.5057, 80.0499),
|
| 27 |
+
"Srikakulam": (18.2949, 83.8938),
|
| 28 |
+
"Vizianagaram": (18.1066, 83.3956),
|
| 29 |
+
"Chittoor": (13.2172, 79.1003),
|
| 30 |
+
"Machilipatnam": (16.1875, 81.1386),
|
| 31 |
+
"Narasaraopet": (16.2346, 80.0490),
|
| 32 |
+
"Tenali": (16.2435, 80.6404),
|
| 33 |
+
"Mangalagiri": (16.4307, 80.5525),
|
| 34 |
+
"Bhimavaram": (16.5449, 81.5212),
|
| 35 |
+
"Tadepalligudem": (16.8133, 81.5271),
|
| 36 |
+
"Bapatla": (15.9049, 80.4671),
|
| 37 |
+
"Chirala": (15.8264, 80.3538),
|
| 38 |
+
"Narasapuram": (16.4344, 81.6958),
|
| 39 |
+
"Palasa": (18.7726, 84.4083),
|
| 40 |
+
"Bobbili": (18.5744, 83.3588),
|
| 41 |
+
"Tekkali": (18.6075, 84.2346),
|
| 42 |
+
"Parvathipuram": (18.7828, 83.4253),
|
| 43 |
+
"Salur": (18.5247, 83.2025),
|
| 44 |
+
"Gajapathinagaram":(18.3269, 83.6020),
|
| 45 |
+
"Rajam": (18.4730, 83.6469),
|
| 46 |
+
"Narasannapeta": (18.4148, 84.0419),
|
| 47 |
+
"Pundi": (18.0996, 83.5868),
|
| 48 |
+
"Payakaraopeta": (17.3603, 82.8651),
|
| 49 |
+
"Narsipatnam": (17.6658, 82.6117),
|
| 50 |
+
"Anakapalle": (17.6910, 83.0000),
|
| 51 |
+
"Bheemunipatnam": (17.8900, 83.4500),
|
| 52 |
+
"Yellamanchili": (17.5543, 82.8555),
|
| 53 |
+
"Chodavaram": (17.8396, 83.1122),
|
| 54 |
+
"Pendurthi": (17.8078, 83.2265),
|
| 55 |
+
"Yelamanchili": (17.5543, 82.8555),
|
| 56 |
+
"Madanapalle": (13.5545, 78.5011),
|
| 57 |
+
"Hindupur": (13.8294, 77.4911),
|
| 58 |
+
"Dharmavaram": (14.4113, 77.7181),
|
| 59 |
+
"Tadipatri": (14.9101, 78.0094),
|
| 60 |
+
"Guntakal": (15.1712, 77.3726),
|
| 61 |
+
"Adoni": (15.6279, 77.2744),
|
| 62 |
+
"Nandyal": (15.4786, 78.4836),
|
| 63 |
+
"Atmakur": (15.8760, 78.5869),
|
| 64 |
+
"Proddatur": (14.7500, 78.5500),
|
| 65 |
+
"Jammalamadugu": (14.8486, 78.3892),
|
| 66 |
+
"Rajampet": (14.1931, 79.1621),
|
| 67 |
+
"Badvel": (14.7455, 79.0643),
|
| 68 |
+
"Pulivendula": (14.4224, 78.2277),
|
| 69 |
+
"Yerraguntla": (14.6353, 78.5333),
|
| 70 |
+
"Cuddapah": (14.4674, 78.8241),
|
| 71 |
+
"Allagadda": (15.1417, 78.5304),
|
| 72 |
+
"Srisailam": (16.0833, 78.8833),
|
| 73 |
+
"Markapur": (15.7407, 79.2682),
|
| 74 |
+
"Kandukur": (15.2143, 79.9057),
|
| 75 |
+
"Kavali": (14.9165, 79.9932),
|
| 76 |
+
"Gudur": (14.1459, 79.8516),
|
| 77 |
+
"Sullurpeta": (13.7630, 79.8878),
|
| 78 |
+
"Venkatagiri": (13.9617, 79.5812),
|
| 79 |
+
"Atmakur_NLR": (14.6213, 79.6357),
|
| 80 |
+
"Kovur": (14.4916, 80.0184),
|
| 81 |
+
"Rapur": (13.9589, 79.4819),
|
| 82 |
+
"Srikalahasti": (13.7500, 79.6983),
|
| 83 |
+
"Puttur": (13.4408, 79.5493),
|
| 84 |
+
"Pileru": (13.6583, 79.1167),
|
| 85 |
+
"Palamaneru": (13.2050, 78.7433),
|
| 86 |
+
"Punganur": (13.3674, 78.5805),
|
| 87 |
+
"Vayalpadu": (13.5800, 78.6600),
|
| 88 |
+
"Pakala": (13.4655, 79.1144),
|
| 89 |
+
"Nagari": (13.3200, 79.5800),
|
| 90 |
+
"Palamaner": (13.2050, 78.7433),
|
| 91 |
+
"Kuppam": (12.7500, 78.3333),
|
| 92 |
+
"Peddapuram": (17.0786, 82.1360),
|
| 93 |
+
"Samalkot": (17.0582, 82.1761),
|
| 94 |
+
"Tuni": (17.3586, 82.5476),
|
| 95 |
+
"Prathipadu": (17.1900, 82.0730),
|
| 96 |
+
"Amalapuram": (16.5771, 82.0056),
|
| 97 |
+
"Razole": (16.4784, 81.8413),
|
| 98 |
+
"Mandapeta": (16.8667, 81.9333),
|
| 99 |
+
"Kovvur": (17.0167, 81.7333),
|
| 100 |
+
"Nidadavolu": (17.0578, 81.6728),
|
| 101 |
+
"Jangareddigudem":(17.0074, 81.3007),
|
| 102 |
+
"Akiveedu": (16.5800, 81.3850),
|
| 103 |
+
"Tanuku": (16.8600, 81.6800),
|
| 104 |
+
"Palakollu": (16.5155, 81.7301),
|
| 105 |
+
"Narsapur": (16.4344, 81.6958),
|
| 106 |
+
"Kanuru": (16.4900, 80.7000),
|
| 107 |
+
"Kondapalli": (16.6169, 80.5460),
|
| 108 |
+
"Ibrahimpatnam": (16.5380, 80.6440),
|
| 109 |
+
"Penamaluru": (16.4700, 80.6800),
|
| 110 |
+
"Nuzvid": (16.7853, 80.8459),
|
| 111 |
+
"Gudivada": (16.4327, 80.9943),
|
| 112 |
+
"Vuyyuru": (16.3618, 80.8459),
|
| 113 |
+
"Repalle": (16.0186, 80.8306),
|
| 114 |
+
"Vinukonda": (16.0500, 79.7333),
|
| 115 |
+
"Sattenapalle": (16.3932, 80.1528),
|
| 116 |
+
"Ponnur": (16.0706, 80.5538),
|
| 117 |
+
"Piduguralla": (16.4757, 79.8985),
|
| 118 |
+
"Macherla": (16.4795, 79.4352),
|
| 119 |
+
"Gurazala": (16.5819, 79.6197),
|
| 120 |
+
"Rentachintala": (16.5505, 79.5764),
|
| 121 |
+
"Dachepalli": (16.2985, 79.8498),
|
| 122 |
+
"Phirangipuram": (16.3059, 80.2167),
|
| 123 |
+
"Chilakaluripet": (16.0875, 80.1689),
|
| 124 |
+
"Mangalagiri": (16.4307, 80.5525),
|
| 125 |
+
"Tadikonda": (16.5100, 80.4200),
|
| 126 |
+
"Amaravati": (16.5730, 80.3564),
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
# ── Nearby POIs (malls, restaurants, landmarks) ───────────────────────────────
|
| 130 |
+
CITY_POIS = {
|
| 131 |
+
"Visakhapatnam": {
|
| 132 |
+
"malls": ["CMR Central Mall", "Jagadamba Junction Mall", "Gajuwaka Mall", "Pothys Mall"],
|
| 133 |
+
"restaurants": ["Dwaraka Hotel", "Bamboo Bay", "The Park Vizag", "Sea View Restaurant", "Hotel Meghalaya"],
|
| 134 |
+
"landmarks": ["Kailasagiri", "Rishikonda Beach", "INS Kurusura Submarine Museum", "Araku Valley"],
|
| 135 |
+
},
|
| 136 |
+
"Vijayawada": {
|
| 137 |
+
"malls": ["PVP Square", "One Town Mall", "AMB Mall", "AP State Mall"],
|
| 138 |
+
"restaurants": ["Hotel Swagath", "Minerva Coffee Shop", "Babai Hotel", "Hotel Mamta", "Curry Leaf"],
|
| 139 |
+
"landmarks": ["Kanaka Durga Temple", "Prakasam Barrage", "Undavalli Caves", "Bhavani Island"],
|
| 140 |
+
},
|
| 141 |
+
"Guntur": {
|
| 142 |
+
"malls": ["KLM Fashion Mall", "Big Bazaar Guntur", "Sujatha Mall"],
|
| 143 |
+
"restaurants": ["Nagarjuna Restaurant", "Hotel Dwaraka", "Singareni Canteen", "Udupi Hotel"],
|
| 144 |
+
"landmarks": ["Undavalli Caves", "Amaravati Museum", "Kondaveedu Fort"],
|
| 145 |
+
},
|
| 146 |
+
"Tirupati": {
|
| 147 |
+
"malls": ["Leela Mahal Mall", "Star Mall Tirupati"],
|
| 148 |
+
"restaurants": ["Hotel Bliss", "Annapurna Hotel", "Bhimas Deluxe", "Minerva Grand"],
|
| 149 |
+
"landmarks": ["Tirumala Temple", "Sri Padmavathi Temple", "Chandragiri Fort", "Silathoranam"],
|
| 150 |
+
},
|
| 151 |
+
"Nellore": {
|
| 152 |
+
"malls": ["Leela Mall", "Spencers Nellore", "City Center Mall"],
|
| 153 |
+
"restaurants": ["Hotel Sreyas", "Kamat Hotel", "Hotel Bhimas", "Spice Garden"],
|
| 154 |
+
"landmarks": ["Pulicat Lake", "Mypad Beach", "Sri Ranganathaswamy Temple"],
|
| 155 |
+
},
|
| 156 |
+
"Rajahmundry": {
|
| 157 |
+
"malls": ["Rajahmundry Mall", "City Center", "VKC Mall"],
|
| 158 |
+
"restaurants": ["Hotel Anand", "Haritha Hotel", "Surrendra Hotel", "Bay Leaf"],
|
| 159 |
+
"landmarks": ["Godavari Bridge", "Dowleswaram Barrage", "ISKCON Temple", "Papikondalu"],
|
| 160 |
+
},
|
| 161 |
+
"Kadapa": {
|
| 162 |
+
"malls": ["Urban Square Mall", "City Square Kadapa"],
|
| 163 |
+
"restaurants": ["Hotel Sree Ram", "Haritha Hotel Kadapa", "Navaratna Hotel"],
|
| 164 |
+
"landmarks": ["Gandikota Fort", "Vontimitta Temple", "Sidhout Fort"],
|
| 165 |
+
},
|
| 166 |
+
"Kurnool": {
|
| 167 |
+
"malls": ["Kurnool Central Mall", "Forum Mall"],
|
| 168 |
+
"restaurants": ["Hotel Srinivasa", "Hotel Rajeswari", "Minerva Restaurant"],
|
| 169 |
+
"landmarks": ["Belum Caves", "Tungabhadra Dam", "Srisailam Temple", "Oravakal Rocks"],
|
| 170 |
+
},
|
| 171 |
+
"Anantapur": {
|
| 172 |
+
"malls": ["Anantapur City Center", "BigBazaar Anantapur"],
|
| 173 |
+
"restaurants": ["Hotel Surya", "MTR Anantapur", "Ruchi Restaurant"],
|
| 174 |
+
"landmarks": ["Lepakshi Temple", "Thimmamma Marrimanu", "Penna River"],
|
| 175 |
+
},
|
| 176 |
+
"Chittoor": {
|
| 177 |
+
"malls": ["Chittoor Mall", "Leela Shopping Center"],
|
| 178 |
+
"restaurants": ["Hotel Viswa", "KFC Chittoor", "Hotel Mayuri"],
|
| 179 |
+
"landmarks": ["Horsley Hills", "Kailasakona Waterfall", "Nagari Hills"],
|
| 180 |
+
},
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
def haversine_km(lat1, lon1, lat2, lon2):
|
| 184 |
+
R = 6371.0
|
| 185 |
+
phi1, phi2 = math.radians(lat1), math.radians(lat2)
|
| 186 |
+
dphi = math.radians(lat2 - lat1)
|
| 187 |
+
dlam = math.radians(lon2 - lon1)
|
| 188 |
+
a = math.sin(dphi/2)**2 + math.cos(phi1)*math.cos(phi2)*math.sin(dlam/2)**2
|
| 189 |
+
return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def build_proximity_graph(cities: dict, max_dist_km: float = 150.0) -> nx.Graph:
|
| 193 |
+
G = nx.Graph()
|
| 194 |
+
city_list = list(cities.items())
|
| 195 |
+
for name, (lat, lon) in city_list:
|
| 196 |
+
G.add_node(name, lat=lat, lon=lon)
|
| 197 |
+
for i in range(len(city_list)):
|
| 198 |
+
n1, (la1, lo1) = city_list[i]
|
| 199 |
+
for j in range(i + 1, len(city_list)):
|
| 200 |
+
n2, (la2, lo2) = city_list[j]
|
| 201 |
+
d = haversine_km(la1, lo1, la2, lo2)
|
| 202 |
+
if d <= max_dist_km:
|
| 203 |
+
road_d = round(d * 1.30, 1)
|
| 204 |
+
G.add_edge(n1, n2, weight=road_d, distance_km=road_d)
|
| 205 |
+
return G
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def get_graph(cities=None, max_dist_km: float = 150.0) -> nx.Graph:
|
| 209 |
+
if cities is None:
|
| 210 |
+
cities = AP_CITIES
|
| 211 |
+
return build_proximity_graph(cities, max_dist_km)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def get_all_cities():
|
| 215 |
+
return sorted(AP_CITIES.keys())
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def get_city_coords(city: str):
|
| 219 |
+
return AP_CITIES.get(city)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def get_city_pois(city: str):
|
| 223 |
+
return CITY_POIS.get(city, {
|
| 224 |
+
"malls": ["Local Shopping Complex", "Govt Market"],
|
| 225 |
+
"restaurants": ["Udupi Hotel", "Local Dhaba", "Annapoorna Restaurant"],
|
| 226 |
+
"landmarks": ["Town Center", "Local Temple"],
|
| 227 |
+
})
|
map_utils.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
map_utils.py
|
| 3 |
+
Builds interactive Folium maps on top of real OpenStreetMap tiles.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import folium
|
| 7 |
+
from graph_builder import get_city_coords, AP_CITIES
|
| 8 |
+
|
| 9 |
+
COL_DIJKSTRA = "#00e676"
|
| 10 |
+
COL_GROVER = "#ff1744"
|
| 11 |
+
COL_BOTH = "#ffea00"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _edge_set(path):
|
| 15 |
+
return {frozenset([path[i], path[i+1]]) for i in range(len(path)-1)}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def build_map(G, dijkstra_result, grover_result, source, target, nearby_cities=None):
|
| 19 |
+
src_ll = get_city_coords(source)
|
| 20 |
+
dst_ll = get_city_coords(target)
|
| 21 |
+
|
| 22 |
+
if src_ll and dst_ll:
|
| 23 |
+
centre = ((src_ll[0]+dst_ll[0])/2, (src_ll[1]+dst_ll[1])/2)
|
| 24 |
+
else:
|
| 25 |
+
centre = (16.0, 80.5)
|
| 26 |
+
|
| 27 |
+
m = folium.Map(
|
| 28 |
+
location=centre,
|
| 29 |
+
zoom_start=7,
|
| 30 |
+
tiles="CartoDB dark_matter",
|
| 31 |
+
control_scale=True,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
d_edges = _edge_set(dijkstra_result.get("path", []))
|
| 35 |
+
g_edges = _edge_set(grover_result.get("path", []))
|
| 36 |
+
|
| 37 |
+
for u, v, data in G.edges(data=True):
|
| 38 |
+
edge_key = frozenset([u, v])
|
| 39 |
+
ll_u = get_city_coords(u)
|
| 40 |
+
ll_v = get_city_coords(v)
|
| 41 |
+
if not ll_u or not ll_v:
|
| 42 |
+
continue
|
| 43 |
+
in_d = edge_key in d_edges
|
| 44 |
+
in_g = edge_key in g_edges
|
| 45 |
+
if in_d and in_g:
|
| 46 |
+
color, weight, dash, opacity = COL_BOTH, 7, None, 0.95
|
| 47 |
+
elif in_d:
|
| 48 |
+
color, weight, dash, opacity = COL_DIJKSTRA, 6, None, 0.9
|
| 49 |
+
elif in_g:
|
| 50 |
+
color, weight, dash, opacity = COL_GROVER, 5, "10 5", 0.85
|
| 51 |
+
else:
|
| 52 |
+
color, weight, dash, opacity = "#334", 1.2, None, 0.3
|
| 53 |
+
folium.PolyLine(
|
| 54 |
+
locations=[ll_u, ll_v],
|
| 55 |
+
color=color, weight=weight, opacity=opacity, dash_array=dash,
|
| 56 |
+
tooltip=f"{u} \u2194 {v} ({data.get('weight','')} km)",
|
| 57 |
+
).add_to(m)
|
| 58 |
+
|
| 59 |
+
d_path = dijkstra_result.get("path", [])
|
| 60 |
+
g_path = grover_result.get("path", [])
|
| 61 |
+
path_cities = set(d_path + g_path)
|
| 62 |
+
|
| 63 |
+
for city in G.nodes():
|
| 64 |
+
ll = get_city_coords(city)
|
| 65 |
+
if not ll:
|
| 66 |
+
continue
|
| 67 |
+
if city == source:
|
| 68 |
+
icon_color, icon = "orange", "play"
|
| 69 |
+
elif city == target:
|
| 70 |
+
icon_color, icon = "red", "flag"
|
| 71 |
+
elif city in path_cities:
|
| 72 |
+
icon_color, icon = "green", "map-marker"
|
| 73 |
+
else:
|
| 74 |
+
icon_color, icon = "purple", "circle"
|
| 75 |
+
|
| 76 |
+
popup_html = f"""
|
| 77 |
+
<div style='font-family:monospace;font-size:13px;min-width:160px'>
|
| 78 |
+
<b style='font-size:15px'>{city}</b><br>
|
| 79 |
+
Lat: {ll[0]:.4f} | Lon: {ll[1]:.4f}<br>
|
| 80 |
+
{'<b style="color:#00e676">On Dijkstra path</b><br>' if city in d_path else ''}
|
| 81 |
+
{'<b style="color:#ff1744">On Grover path</b><br>' if city in g_path else ''}
|
| 82 |
+
</div>"""
|
| 83 |
+
|
| 84 |
+
folium.Marker(
|
| 85 |
+
location=ll, tooltip=city,
|
| 86 |
+
popup=folium.Popup(popup_html, max_width=220),
|
| 87 |
+
icon=folium.Icon(color=icon_color, icon=icon, prefix="fa"),
|
| 88 |
+
).add_to(m)
|
| 89 |
+
|
| 90 |
+
if nearby_cities:
|
| 91 |
+
for city in nearby_cities:
|
| 92 |
+
ll = get_city_coords(city)
|
| 93 |
+
if not ll:
|
| 94 |
+
continue
|
| 95 |
+
folium.CircleMarker(
|
| 96 |
+
location=ll, radius=10, color="#ffd740",
|
| 97 |
+
fill=True, fill_color="#ffd740", fill_opacity=0.3,
|
| 98 |
+
tooltip=f"Nearby: {city}",
|
| 99 |
+
).add_to(m)
|
| 100 |
+
|
| 101 |
+
legend_html = """
|
| 102 |
+
<div style="position:fixed;bottom:30px;left:30px;z-index:1000;
|
| 103 |
+
background:#0d1117;border:1px solid #30363d;border-radius:8px;
|
| 104 |
+
padding:12px 16px;font-family:monospace;font-size:12px;color:#e6edf3">
|
| 105 |
+
<b style="font-size:13px;color:#58a6ff">LEGEND</b><br><br>
|
| 106 |
+
<span style="color:#00e676">━━━</span> Dijkstra path<br>
|
| 107 |
+
<span style="color:#ff1744">┌┌┌</span> Grover path<br>
|
| 108 |
+
<span style="color:#ffea00">━━━</span> Shared route<br>
|
| 109 |
+
<span style="color:#555">━━━</span> Road edge<br><br>
|
| 110 |
+
Source (orange) | Target (red)<br>
|
| 111 |
+
On route (green) | Other (purple)
|
| 112 |
+
</div>"""
|
| 113 |
+
m.get_root().html.add_child(folium.Element(legend_html))
|
| 114 |
+
return m._repr_html_()
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def get_nearby_cities(city, G, n=5):
|
| 118 |
+
if city not in G:
|
| 119 |
+
return []
|
| 120 |
+
neighbors = [{"city": nbr, "dist_km": G[city][nbr]["weight"]} for nbr in G.neighbors(city)]
|
| 121 |
+
neighbors.sort(key=lambda x: x["dist_km"])
|
| 122 |
+
return neighbors[:n]
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
networkx==3.2.1
|
| 3 |
+
numpy>=1.26.0
|
| 4 |
+
folium>=0.17.0
|