Search
 
SCRIPT & CODE EXAMPLE
 

CPP

travelling salesman problem c++

// CPP program to implement traveling salesman
// problem using naive approach.
#include <bits/stdc++.h>
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<int> 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Visual studio code include path not working c++ 
Cpp :: print stack without pop c++ 
Cpp :: input full line as input in cpp 
Cpp :: how to find the length of an array in cpp 
Cpp :: c++ #include 
Cpp :: size() in c++ SET 
Cpp :: c++ program to convert character to ascii 
Cpp :: function overriding in c++ 
Cpp :: c++ class template 
Cpp :: C++ :: 
Cpp :: explicit c++ 
Cpp :: how to create a c++ templeate 
Cpp :: how to make a function in c++ 
Cpp :: prime number c++ 
Cpp :: cpp execute command 
Cpp :: CRED Coins codechef solution in c++ 
Cpp :: one away coding question 
Cpp :: Integer Moves codeforces solution 
Cpp :: array copx c++ 
Cpp :: initialize 2d vector c++ 
Cpp :: intlen in c++ 
Cpp :: Reverse a linked list geeksforgeeks in c++ 
Cpp :: Abstract factory C++ code 
Cpp :: c++ include < vs "" 
Cpp :: evennumbers 1 to 100 
Cpp :: right shift in c++ 
Cpp :: cpp custom exception 
Cpp :: c++ power of two 
Cpp :: convert uppercase to lowercase 
Cpp :: how togreper 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =