From b30cd93c6d32ca22b40bc7fa98c9be5855074009 Mon Sep 17 00:00:00 2001 From: namishagoel <57214627+namishagoel@users.noreply.github.com> Date: Fri, 29 Oct 2021 14:48:30 +0530 Subject: [PATCH] Create TravellingSalesmanProblem --- TravellingSalesmanProblem | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 TravellingSalesmanProblem diff --git a/TravellingSalesmanProblem b/TravellingSalesmanProblem new file mode 100644 index 0000000..beb9859 --- /dev/null +++ b/TravellingSalesmanProblem @@ -0,0 +1,41 @@ +#include +using namespace std; +#define V 4 + +int travllingSalesmanProblem(int graph[][V], int s) +{ + vector vertex; + for (int i = 0; i < V; i++) + if (i != s) + vertex.push_back(i); + + int min_path = INT_MAX; + do { + + int current_pathweight = 0; + + 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]; + + min_path = min(min_path, current_pathweight); + + } while ( + next_permutation(vertex.begin(), vertex.end())); + + return min_path; +} + +int main() +{ + int graph[][V] = { { 0, 10, 15, 20 }, + { 10, 0, 35, 25 }, + { 15, 35, 0, 30 }, + { 20, 25, 30, 0 } }; + int s = 0; + cout << travellingSalesmanProblem(graph, s) << endl; + return 0; +}