Active4 years, 8 months ago
I am learning TSP and found this recursive solution to TSP
Travelling salesman problem is the most notorious computational problem. We can use brute-force approach to evaluate every possible tour and select the best one. For n number of vertices in a graph, there are (n - 1)! Number of possibilities. Instead of brute-force using dynamic programming approach.
I could not understand How masking is working , How can change it to Dynamic programming solution without using recursion , please help me.
user4392540user4392540
closed as unclear what you're asking by Borgleader, Mat, Pradhan, kraskevich, Paul HankinDec 26 '14 at 8:25
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1 Answer
Here is a traditional TSP problem ,and here is the solution for it . I think it may be helpful to you.
Yao YingjieYao Yingjie
Not the answer you're looking for? Browse other questions tagged c++algorithmtraveling-salesman or ask your own question.
Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns back to the starting point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltoninan cycle problem is to find if there exist 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 figure on 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.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltoninan cycle problem is to find if there exist 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 figure on 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 know solution for this problem.
Examples:
Examples:
In this post, implementation of simple solution is discussed.
- Consider city 1 as the starting and ending point. Since route is cyclic, we can consider any point as starting point.
- Generate all (n-1)! permutations of cities.
- Calculate cost of every permutation and keep track of minimum cost permutation.
- Return the permutation with minimum cost.
Below c++ implementation of above idea
// problem using naive approach. using namespace std; int travllingSalesmanProblem( int graph[][V], int s) // store all vertex apart from source vertex for ( int i = 0; i < V; i++) vertex.push_back(i); // store minimum weight Hamiltonian Cycle. do { // store current Path weight(cost) int k = s; current_pathweight += graph[k][vertex[i]]; } min_path = min(min_path, current_pathweight); } while (next_permutation(vertex.begin(), vertex.end())); return min_path; int main() // matrix representation of graph { 10, 0, 35, 25 }, { 20, 25, 30, 0 } }; cout << travllingSalesmanProblem(graph, s) << endl; } |
Output:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. 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.