Traveling Salesman is one of the classic NP-Hard problems: finding the optimal solution can take a long time, but there are some great shortcuts available which come close! Algorithmia now brings you a fast, near-optimal way to find the fastest route through multiple cities, thanks to the power of Genetic Algorithms and easily-accessible APIs.

### What is Traveling Salesman?

You want to go on a road trip, visiting a specific set of cities along the way, and you'd like to minimize the amount of driving required to do so. It doesn't make sense to go to New York, then Los Angeles, then Miami: the shorter route is LA-Miami-NYC.

### Why is Traveling Salesman a difficult problem?

It is easy to see the solution for a small number of cities: you just look at all the possible routes and add up the distances (or travel times) for each possible solution. NY-LA-Miami: 5,529 miles. LA-NY-Miami: 4,081. LA-Miami-NY: 4,014.

Unfortunately, as the number of cities grows, the number of comparisons you have to make increases very rapidly. Traveling to 3 cities requires 3 comparisons, but traveling to 4 cities requires 12, 5 cities require 60 comparisons, and so on. For a more complete discussion of the problem, you may want to read up on NP-Completeness, but for now we can see that solving this problem for a large number of cities is very computationally expensive, to the point of being impossible for very large trips.

### How can we "solve" the problem in reasonable time?

When you can't get a perfect solution before the heat-death of the universe, you have to approximate. There are lots of ways to do so, but Genetic Algorithms offer a particularly clever and useful approach. In a nutshell, we start by randomly selecting a few possible solutions. In this case, we several initial travel routes: each one contains all the cities we want to go to, but their order is randomly selected. We calculate the total travel distance for each one, and pick the top few, discarding the rest. These "round one" winners move on to the second round, during which a few of the cities' orders are swapped around. Then, we add more randomly-generated routes, and do it again. We repeat this whole process for many rounds, permuting the winners each time, and adding random new routes as well.

After enough repetitions, good solutions arise, and we pick the best one. It may not be perfect... the algorithm does the best it can within some reasonable time constraints, but we can't be 100% certain that a better one doesn't exist. But most of the time, the route that emerges is only a few percentage points longer than the truly optimal route (and, sometimes, we happen to get the perfect one)!

### How can I use this solution without learning and writing a Genetic Algorithm of my own?

Google Maps, R, and algorithm-implementer Akadal have teamed up to bring Traveling Salesman to any app or script you write, regardless of language or platform, via a simple API call. With the TSP algorithm running on Algorithmia, you simply specify starting and ending points, plus a list of intermediate cities, and it tells you which order you should visit them in.

1. Sign up for a free Algorithmia account which provides 5000 credits to play with every month.

2. Head to the bottom of https://algorithmia.com/algorithms/akadal/TSP to try out the algorithm in real-time

3. Pick your favorite language, copy the code sample, and paste it into your app. Here's an example in JavaScript (replace YOUR_API_KEY with your own):

[code javascript]
var input = {
"points": ["Seattle","Boston","Orlando", "Miami", "Montreal"],
"startpoint": "Los_Angeles",
"endpoint": "Toronto"
};
Algorithmia.client("YOUR_API_KEY")
.pipe(input)
.then(function(output) {
console.log(output);
});
[/code]

4. Receive your results as a simple ordered list, to do with as you wish:

[code javascript]
[
"Los_Angeles",
"Seattle",
"Orlando",
"Miami",
"Boston",
"Montreal",
"Toronto"
]
[/code]

We hope you enjoy using this and the other 3,800+ Algorithms in our marketplace. If you have an awesome algorithm of your own, please help grow the community by adding it to the marketplace and letting us know so we can feature it!