Travelling Salesman Problem Visualization
Overview
A real-time visualization built in Processing (Java) that solves the Travelling Salesman Problem using two fundamentally different approaches displayed side by side. The left half runs a Genetic Algorithm that evolves a population of candidate routes through selection, crossover, and mutation, while the right half executes a Brute Force search that enumerates every possible permutation.
Both algorithms operate on the same set of randomly placed cities, allowing a direct visual comparison of convergence speed and solution quality. Green paths indicate the best route found so far, while white paths show the current attempt being evaluated.
The Problem
Travelling Salesman Problem (TSP)
Given cities, the Travelling Salesman Problem asks: what is the shortest route that visits every city exactly once and returns to the starting city? Despite its simple statement, TSP is one of the most studied NP-hard problems in combinatorial optimization.
A brute force approach must evaluate every possible permutation of cities. For cities, the number of distinct routes is:
For the 25 cities used, that means:
Even at billions of evaluations per second, exhaustively checking all routes would take longer than the age of the universe. This astronomical complexity motivates the use of metaheuristic algorithms like the Genetic Algorithm, which trade guaranteed optimality for practical run-time by exploring the search space intelligently.
Brute Force Approach
Lexicographic Permutation Enumeration
The brute force solver uses the lexicographic next-permutation algorithm to systematically enumerate every possible route without repetition. Starting from the identity permutation, each step produces the next permutation in lexicographic order by finding the rightmost ascent, swapping with the smallest larger element, and reversing the suffix.
Every generated permutation is evaluated as a candidate route — the total Euclidean distance is computed and compared against the current best. The display shows the current permutation number out of the total , the percentage completion, and the best distance found so far.
While this approach guarantees finding the optimal solution, its time complexity makes it computationally infeasible for anything beyond a handful of cities. For 25 cities, the percentage counter effectively never moves — a powerful visual demonstration of factorial growth.
Genetic Algorithm
Population-Based Metaheuristic
The Genetic Algorithm (GA) is a population-based metaheuristic inspired by natural selection. A population of 800 candidate routes evolves over successive generations, with fitter individuals more likely to pass their genetic material to the next generation.
Each route's fitness is computed using an inverse distance function with a high exponent to amplify differences between good and bad solutions:
Fitness values are normalized across the population so they sum to 1, enabling roulette wheel selection where the probability of selecting an individual is proportional to its normalized fitness. Two parents are selected this way, and their offspring inherits genetic material from both.
Mutation operates by swapping random adjacent cities in the route with a mutation rate of per gene — intentionally high to maintain diversity and prevent premature convergence. The display shows the current best of each generation in white and the overall best ever found in green, along with the generation count and best distance.
Unlike brute force, the GA converges to a near-optimal solution within seconds to minutes, demonstrating the power of evolutionary search on combinatorial problems.
Side-by-Side Comparison
8 Cities Comparison: Instant Exhaustive Solving
For small problem sizes like 8 cities, the search space is extremely small:
With such a limited number of candidate routes, the brute force solver can enumerate every single permutation in a fraction of a second. Here the loop is running at 60fps, therefoore it takes some time.
On the right side of the screen, you can see the percentage completion counter progress in real-time. In this small-scale scenario, brute force is takes some time but gurantees finding a route, while the genetic algorithm on the left also easily converges within a very short time.
12 Cities Comparison: Noticeable Exhaustive Computation
When the city count increases to 12 cities, the search space grows significantly:
At this scale, the brute force solver must perform hundreds of millions of distance calculations.
The percentage counter doesnt increase at all over the course of half minute time. The genetic algorithm converges on the optimal route within couple dozen generations, showcasing that while brute force can still be used, heuristic methods are starting to show their advantage in speed.
17 Cities Comparison: The Factorial Wall
At 17 cities, we hit the combinatorial wall. The number of possible routes jumps to:
Even if the system could check millions of routes per second, exhaustively searching the space would take several years.
As a result, the brute force percentage counter remains virtually frozen at a steady 0.00%, and the best route found by brute force is highly suboptimal and chaotic. The genetic algorithm, however, successfully navigates this massive search space to find a highly optimized route in seconds, proving the necessity of evolutionary search.
25 Cities Comparison: Cosmic Complexity
With 25 cities, the complexity becomes truly astronomical:
This number of routes is comparable to the number of grains of sand on Earth.
The brute force completion percentage remains completely static at 0.00%, showing absolutely no progress, and its path remains extremely long and chaotic. Meanwhile, the genetic algorithm rapidly converges, dropping the total path distance from a chaotic starting state to a much cleaner path. However we can visually observe that this is not the optimal path, as loops can be seen.
Technology Stack
Source Code
TSP Solver & Visualization
A Processing (Java) application that visualizes brute-force and genetic algorithm strategies solving the same set of cities side-by-side.