From 4eac0cfe8a0bc6d1b48733c4124bad3aef5075c2 Mon Sep 17 00:00:00 2001 From: Aayush Kumar <61198949+Aayush-eng@users.noreply.github.com> Date: Fri, 1 Oct 2021 10:36:45 +0530 Subject: [PATCH] Travelling salesperson problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.  --- Travelling_salesperson.cpp | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Travelling_salesperson.cpp diff --git a/Travelling_salesperson.cpp b/Travelling_salesperson.cpp new file mode 100644 index 0000000..9947535 --- /dev/null +++ b/Travelling_salesperson.cpp @@ -0,0 +1,101 @@ + + + +#include + +using namespace std; + +#define V 4 + + + +// implementation of traveling Salesman Problem + +int travllingSalesmanProblem(int graph[][V], int s) + +{ + + // store all vertex apart from source vertex + + vector vertex; + + for (int i = 0; i < V; i++) + + if (i != s) + + vertex.push_back(i); + + + + // store minimum weight Hamiltonian Cycle. + + int min_path = INT_MAX; + + do { + + + + // store current Path weight(cost) + + int current_pathweight = 0; + + + + // compute current path weight + + int k = s; + + for (int i = 0; i < vertex.size(); i++) { + + current_pathweight += graph[k][vertex[i]]; + + k = vertex[i]; + + } + + current_pathweight += graph[k][s]; + + + + // update minimum + + min_path = min(min_path, current_pathweight); + + + + } while ( + + next_permutation(vertex.begin(), vertex.end())); + + + + return min_path; + +} + + + +// Driver Code + +int main() + +{ + + // matrix representation of graph + + int graph[][V] = { { 0, 10, 15, 20 }, + + { 10, 0, 35, 25 }, + + { 15, 35, 0, 30 }, + + { 20, 25, 30, 0 } }; + + int s = 0; + + cout << travllingSalesmanProblem(graph, s) << endl; + + return 0; + +} +