5 Best Algorithms for the Travelling Salesman Problem
By Rohit Sharma
Updated on Apr 01, 2025 | 8 min read | 1.99K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Apr 01, 2025 | 8 min read | 1.99K+ views
Share:
The travelling salesman problem (TSP) is one of the classic problems that asks to find the shortest route possible for a salesman to visit all the cities at once and return to the starting point. By listening, the problem seems easy to solve. However, in reality, there’s no exact algorithm, that can solve the travelling salesman problem.
Over the years, numerous algorithms have been introduced, numerous methodologies are implemented. But, this NP-hard problem became more and more complex, as the cities increase.
But, you don’t need to fret, as we’ve got the best 5 algorithms for the travelling salesman problem, that balances speed and accuracy.
Get data science certification from the World’s top Universities. Learn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
Popular Data Science Programs
We’re going to dive deep into the top 5 algorithms for travelling salesman problem, analyzing them on the basis of time complexity, and their specific use case.
The Brute Force algorithm is the simplest and most direct approach to solving the TSP. It involves generating all possible permutations of the cities and calculating the total travel distance for each permutation. The solution is the permutation that yields the minimum distance.
How it Works for Travelling Salesman Problem:
Time Complexity:
The time complexity of the Brute Force algorithm is O(n!) because it generates and checks all permutations of the cities. This grows factorially with the number of cities, making it highly inefficient for larger instances of TSP.
Must Explore: What are Data Structures & Algorithm
When to Use for Travelling Salesman Problem:
The Brute Force approach is only feasible for small-sized instances, typically with fewer than 10 cities. As the number of cities increases, the algorithm becomes computationally impractical due to its exponential growth in complexity.
The Held-Karp algorithm is a more efficient solution to the TSP that uses dynamic programming. It improves upon the Brute Force approach by breaking the problem down into smaller subproblems and storing intermediate results, significantly reducing the number of redundant calculations.
How it Works for Travelling Salesman Problem:
dp(S,i) = min (dp (S∖{i},j) + dist (j,i)) ∀j∈S∖{i}
Also Explore: Top 14 Most Common Data Mining Algorithms You Should Know
Time Complexity:
The time complexity of the Held-Karp algorithm is O(n² * 2^n), where n is the number of cities. This is a significant improvement over brute force, but the algorithm still becomes computationally expensive for larger numbers of cities (typically beyond 20-25 cities).
When to Use for Travelling Salesman Problem:
The Held-Karp algorithm is effective for solving TSPs with a moderate number of cities (around 20 to 25). It provides an exact optimal solution and is often used in smaller, well-defined problems where precision is essential.
The Greedy Algorithm is a simple heuristic method that builds a solution step-by-step by always selecting the locally optimal choice, assuming that this will lead to a globally optimal solution. The Greedy approach is fast but does not guarantee an optimal solution.
How it Works for Travelling Salesman Problem:
Time Complexity:
The time complexity of the Greedy algorithm is O(n²), where n is the number of cities. This is because, for each city, the algorithm needs to evaluate the distances to all other cities.
When to Use for Travelling Salesman problem:
The Greedy Algorithm is suitable for large-scale problems where a quick, approximate solution is needed. It is used in real-world situations where finding an exact solution is not necessary and computational resources are limited.
Additionally, the Greedy algorithm does not guarantee an optimal solution for travelling salesman problem. In many cases, it gets stuck in local minima and may produce significantly worse solutions than the optimal one.
Also Read: Top 10 Data Structures & Algorithm Interview Questions & Answers
The Genetic Algorithm (GA) is an optimization method inspired by the process of natural selection. It uses a population of candidate solutions and evolves them over multiple generations using processes like selection, crossover, and mutation for the travelling salesman problem.
How it Works for Travelling Salesman Problem:
Time Complexity:
The time complexity of the Genetic Algorithm depends on the number of generations g, population size p, and the number of cities n. It can be estimated as O(g * p * n), where g is the number of generations, p is the population size, and n is the number of cities.
Travelling salesman problem use case:
Genetic algorithms are ideal when you need an approximate solution for large instances of the TSP. They are particularly useful in scenarios where finding an exact solution is computationally prohibitive, and a good-enough solution is acceptable.
Furthermore, genetic algorithms do not guarantee optimality for travelling salesman problem. Their performance depends heavily on the choice of parameters (such as mutation rate, population size, and number of generations) and may converge to a suboptimal solution.
Ant Colony Optimization (ACO) is a bio-inspired algorithm based on the behavior of ants searching for food. Ants leave pheromones on the paths they take, and subsequent ants are more likely to follow stronger pheromone trails, eventually converging on the shortest path.
How it Works for Travelling Salesman Problem:
Time Complexity:
The time complexity of ACO is approximately O(a * i * n), where “a” is the number of ants, “I” is the number of iterations, and “n” is the number of cities. The complexity depends on the number of ants and iterations, which are tunable parameters.
Travelling Salesman Problem Use Case:
ACO is useful for large-scale TSP instances, where finding an optimal solution is computationally expensive. It provides a good balance between computational efficiency and solution quality and is often used in real-world routing and logistics problems.
In addition, it also has a limitation. ACO is an approximate method and may not always converge to the optimal solution. Its performance depends on the parameter settings (e.g., pheromone evaporation rate, number of ants) and problem characteristics.
The Travelling Salesman Problem is a challenging optimization problem with wide-ranging real-world applications. While the Brute Force algorithm guarantees an optimal solution, its exponential time complexity makes it impractical for large problems. The Held-Karp algorithm offers a more efficient exact solution for moderately sized instances, but still suffers from exponential growth for larger datasets.
For larger problems or when exact solutions are not required, Greedy Algorithms, Genetic Algorithms, and Ant Colony Optimization provide feasible approximate solutions. Each of these heuristic and metaheuristic methods offers a different balance of computational efficiency and solution quality, making them suitable for various practical applications, from logistics and routing to manufacturing and data analysis.
Choosing the best algorithm depends on the problem size, available computational resources, and the acceptable trade-off between optimality and efficiency.
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
The Travelling Salesman Problem (TSP) asks for the shortest possible route that allows a salesman to visit all cities once and return to the starting point. It is an NP-hard problem that becomes increasingly complex as the number of cities grows.
The Brute Force algorithm generates all possible city permutations, calculates the distance for each, and selects the permutation with the smallest total distance. It’s simple but inefficient for large numbers of cities due to its O(n!) time complexity.
The Brute Force algorithm is best suited for small instances of TSP, typically fewer than 10 cities. It is impractical for larger instances due to its exponential time complexity.
The Held-Karp algorithm is a dynamic programming approach that reduces the complexity of TSP compared to brute force. It solves the problem in O(n² * 2^n) time by solving smaller subproblems and storing intermediate results.
The Held-Karp algorithm is effective for moderately sized problems (about 20-25 cities). It guarantees an optimal solution but still becomes computationally expensive for larger datasets.
The Greedy algorithm is a heuristic method that builds the solution step by step, always choosing the nearest unvisited city. While fast, it doesn't guarantee an optimal solution and can get stuck in local minima.
The Greedy algorithm is suitable for large-scale problems where a quick, approximate solution is acceptable. It's useful when computational resources are limited, but optimality is not a critical factor.
The Genetic Algorithm mimics natural selection, using a population of candidate solutions. Over multiple generations, solutions evolve using selection, crossover, and mutation. It can find good-enough solutions but doesn't guarantee optimality.
The Genetic Algorithm is ideal for large TSP instances where an exact solution is impractical. It offers a balance between solution quality and computational efficiency, especially for problems where a near-optimal solution is acceptable.
ACO is inspired by the behavior of ants in nature. It uses a population of ants to explore the solution space, updating pheromone trails to favor shorter paths. The algorithm is good for large-scale TSPs but doesn't always guarantee the optimal solution.
ACO is suited for large, real-world TSP problems like logistics and routing where finding an exact solution is too computationally expensive. It balances solution quality with efficiency but may not always converge to the optimal path.
834 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources