Challenge

Project - Traveling salesman problem

Ans:

#include <bits/stdc++.h>

usinmg namespace std;

#define V 4

int travellingsalesmanproblem(int graph[] [V],

{

vector <int> 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];

// update minimum

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) <<end1;

return 0;

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.