Skip to content
Go back

TSP Algorithm: Solving the Traveling Salesman Problem with Genetic Algorithms

6 min read

This post is best viewed using the light theme.

What is the Traveling Salesman Problem?

Ever wondered why the delivery driver drives past your house? Or why your GPS sometimes takes you on what seems like a weird route? That’s exactly what the Traveling Salesman Problem (TSP) tries to solve!

In simple terms: “Given a list of cities and the distances between them, what’s the shortest possible route that visits each city exactly once and returns to the starting point?”

It sounds easy, but it’s actually one of the most famous problems in computer science. With just 13 cities, there are over 6 billion possible routes to check!

Why Use Genetic Algorithms?

Genetic algorithms are perfect for TSP because:

This post shows how genetic algorithms can solve TSP using real US city coordinates. We’ll compare our results with Google’s OR-Tools to see how well we do!

The Cities We’re Working With

Here are the 13 US cities we’ll be optimizing routes for:

CityCoordinatesCodeDescription
New York40, -74AStarting point
Los Angeles34, -118BWest Coast hub
Chicago41, -87CMidwest center
Minneapolis44, -93DNorthern city
Denver39, -104EMountain region
Dallas32, -96FSouthern hub
Seattle47, -122GPacific Northwest
Boston42, -71HNortheast
San Francisco37, -122IBay Area
St. Louis38, -90JGateway to West
Houston29, -95KTexas coast
Phoenix33, -111LDesert Southwest
Salt Lake City40, -111MMountain West

Total: 13 cities - enough to make it interesting but not overwhelming!

How the Algorithm Works

The Route Representation

Instead of storing full city names, we use letters A-M. So a route like “A→H→C→D→E→M→G→I→B→L→K→F→J→A” means: New York → Boston → Chicago → Minneapolis → Denver → Salt Lake City → Seattle → San Francisco → Los Angeles → Phoenix → Houston → Dallas → St. Louis → New York

Distance Calculation

We use Euclidean distance (straight-line distance) between coordinates, not road distance. This makes calculations faster and gives us a good approximation.

The Genetic Algorithm Process

  1. Start with random routes (population of 20 routes)
  2. Calculate fitness (shorter routes = better fitness)
  3. Select parents using different methods
  4. Create children by combining parts of parent routes
  5. Mutate some routes randomly
  6. Repeat for 500 generations

Interactive TSP Solver

Try the interactive demo below! You can experiment with different genetic algorithm settings:

Choose Your Settings

Cross Over Method:

Selection Method:

Click “Run” to see how different settings affect the solution!

Quick Tips:

Results and Analysis

How Different Methods Perform

Roulette Wheel selection tends to create more dramatic fitness spikes - you’ll see the blue line jump around more!

The Optimal Solution

Google’s OR-Tools found the best possible route: New York → Boston → Chicago → Minneapolis → Denver → Salt Lake City → Seattle → San Francisco → Los Angeles → Phoenix → Houston → Dallas → St. Louis → New York

Total distance: 7,293 miles

Our genetic algorithm won’t always find this exact solution, but it gets pretty close and does it much faster than checking all possible routes!

What I Learned Building This

Key Insights

Real-World Applications

TSP isn’t just academic! It’s used everywhere:

Genetic algorithms are perfect for these real-world problems because they can find good solutions quickly, even when the perfect solution would take too long to calculate.


Share this post on:

Previous Post
Statechart Diagrams: Visualizing Complex System Behavior
Next Post
Design Patterns in Practice: Singleton and Observer with a Lucky Dip Machine