Travelling Salesman Problem (TSP) : Given a set of cities and distances between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exists a tour that visits every city exactly once. Here we know that Hamiltonian Tour exists (because the graph is complete) and in fact, many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle.
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.
The problem is a famous NP-hard problem. There is no polynomial-time known solution for this problem.
Output of Given Graph: minimum weight Hamiltonian Cycle : 10 + 25 + 30 + 15 := 80
In this post, the implementation of a simple solution is discussed.
- Consider city 1 as the starting and ending point. Since the route is cyclic, we can consider any point as a starting point.
- Generate all (n-1)! permutations of cities.
- Calculate the cost of every permutation and keep track of the minimum cost permutation.
- Return the permutation with minimum cost.
Below is the implementation of the above idea
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Traveling Salesman Problem using Genetic Algorithm
- Proof that traveling salesman problem is NP Hard
- Travelling Salesman Problem implementation using BackTracking
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Travelling Salesman Problem | Set 2 (Approximate using MST)
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Push Relabel Algorithm | Set 2 (Implementation)
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Bellman Ford Algorithm (Simple Implementation)
- Implementation of BFS using adjacency matrix
- Implementation of Erdos-Renyi Model on Social Networks
- Implementation of Page Rank using Random Walk method in Python
- Applications of Minimum Spanning Tree Problem
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.