I have a list of 80 towns each with their geographical longitude and latitude. I need to find the shortest closed route (taking into account the curvature of the earth). I was thinking I could calculate the distance between each one (using great-circle distance formula) and then list the ones with the shortest distances (that would be 6400 calculations). The thing is I don't have much programming experience but I know a small bit of C# and PHP. Could someone tell point me in the right direction of what I should learn about to solve this problem (eg. should I learn more about php and mysql to use a database or something to sort out the distances?)
-
1You can "cheat" and use google maps to get that info. – Itay Moav -Malimovka Dec 11 '11 at 23:58
3 Answers
This problem is known as the Travelling salesman problem and is NP hard - to solve it deterministically and get the optimal route you can only use brute force, as an alternative you can use heuristics that most of the time produce a great result, but may be sub-optimal.

- 158,293
- 28
- 286
- 335
Dijkstras algorithm is best one for this problems with less complexity.TSP need very hard work and tree size is huge for 80 city(It is impractical for this no of cities as there are n! possibilities for n cities).I suggest first draw the graph of the cities and find out possible routes then apply Dijkstras.A * algorithm is best but it is difficult for beginners to implements on problems like here.

- 2,543
- 1
- 29
- 47
There are a number of ways to work out distances given lat/long. I don't think that you will need much programming experience to implement one.
EDIT
If the number of cities is static at 80 and you know how to calculate the distance between cities then a simple option is to store this information in a database; with a structure like:
--City
CityId
CityName
Latitude
Longitude
--CityCityDistance
StartCityId
FinishCityId
RouteDistance
Your CityCityDistance table would only have a few thousand rows (if I remember how maths works) and then if you ever needed to know the city that is closest to another city the sql would be something like (in t-sql):
select top 1 ccd.FinishCityId, c.CityName
from CityCityDistance ccd
join City c on ccd.StartCityId = c.CityId
where ccd.RouteDistance = (select min(RouteDistance) from CityCityDistance where StartCityId = <chosen city id> )

- 8,919
- 4
- 38
- 59
-
I can find the distance between two points no problem. I don't know how to go about managing all the coordinates and sorting out the distances. – Darkphenom Dec 12 '11 at 00:27
-
Given that all the combinations are only a few thousand in total I would consider storing them all in a database and then extracting them using sql - or store them in xml and process that. – amelvin Dec 12 '11 at 00:45