Efficient and Scalable TSP Solver for Real-World Geographic Points using the Haversine Formula
- 🗺️ TSP (Traveling Salesman Problem) solver for latitude-longitude locations
- ⚡ Nearest Neighbor heuristic forsSupports thousands of points
- 📐 Haversine formula for accurate Earth distance
- 📦 JSON import/export for locations & routes
- 🏙️ Built-in random city location generator for realistic simulations and demos
-
Location Generation (
tsp-generator.js)- Instantly generates 100 random points for each of Jakarta, Yogyakarta, and Surabaya
- Each point is labeled (
Jakarta 1, ...,Surabaya 100), with a special center (Jakarta Central) - Output:
cities-locations.json
-
Distance Calculation
- All routing uses the accurate Haversine formula (see code), considering Earth curvature for precise km/m calculations
-
TSP Route Solving (
tsp-solver.js)- Reads your city/location JSON
- Applies the Nearest Neighbor heuristic to quickly compute a "good" round-trip route (start and end at same city)
- Output: Ordered route and total distance in
tsp-result.json
- Haversine: Measures true shortest path between two points on a sphere (the Earth)
- Nearest Neighbor TSP:
- Start at any city
- At each step, travel to the nearest unvisited city
- Repeat until all cities are visited, then return to the starting point
- Scales to thousands of points (brute force only works for ≤ 8)
git clone https://github.com/NeaByteLab/Haversine-TSP.git
cd Haversine-TSP
npm installnode tsp-generator.jsOutput:
cities-locations.json🗃️
node tsp-solver.jsOutput: tsp-result.json 🏁
[
{ "label": "Jakarta Central", "lat": -6.2, "lon": 106.8167 },
{ "label": "Jakarta 1", "lat": -6.21, "lon": 106.81 },
...,
{ "label": "Surabaya 100", "lat": -7.30, "lon": 112.77 }
]{
"route": [
{ "label": "Jakarta Central", "lat": -6.2, "lon": 106.8167 },
...
],
"totalDistance": 3145.53
}- 🚚 Logistics & delivery route optimization
- 🏞️ Tour, field work, and multi-city planning
- 🧪 Geographic research, simulations, and demos
- 📈 Route benchmarking and performance testing
- The Nearest Neighbor heuristic is extremely fast and often very good, but not mathematically optimal
- For datasets with ≤8 points, a brute-force method can guarantee the shortest possible route
- For large datasets (100+), use this heuristic or add post-processing (2-Opt, etc) for better results
MIT License © 2025 NeaByteLab