-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathTravellingSalesMen.cpp
91 lines (74 loc) · 2.13 KB
/
TravellingSalesMen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>
#include <utility>
#include <cfloat>
#include <cmath>
#include <algorithm>
using namespace std;
// a structure to represent a weighted edge in graph
struct Edge
{
int src, dest;
double weight;
};
// a structure to represent a connected, directed and weighted graph
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges
vector<Edge> edges;
// Constructor
Graph(int V, int E) : V(V), E(E) {}
// Utility function to add an edge
void add_edge(int src, int dest, double weight)
{
Edge edge;
edge.src = src;
edge.dest = dest;
edge.weight = weight;
edges.push_back(edge);
}
};
// The main function that calulates the shortest distance between every pair of vertices
double TSP(Graph& graph, vector<int>& path)
{
int V = graph.V;
vector<int> vertex_set;
for (int i = 0; i < V; i++)
vertex_set.push_back(i);
// store minimum weight Hamiltonian Cycle
double min_path_weight = DBL_MAX;
do {
// store current Path weight(cost)
double current_path_weight = 0;
// compute current path weight
int k = vertex_set[0];
for (int i = 0; i < V - 1; i++) {
int j = vertex_set[i + 1];
current_path_weight += graph.edges[k * V + j].weight;
k = j;
}
current_path_weight += graph.edges[k * V + vertex_set[0]].weight;
// update minimum
min_path_weight = min(min_path_weight, current_path_weight);
} while (next_permutation(vertex_set.begin(), vertex_set.end()));
return min_path_weight;
}
// Driver program to test above functions
int main()
{
// create a graph
int V = 4;
Graph graph(V, V * (V - 1));
// Example graph
// (0)--10--(1)--15--(2)--20--(3)--12--(0)
graph.add_edge(0, 1, 10);
graph.add_edge(0, 3, 12);
graph.add_edge(1, 2, 15);
graph.add_edge(2, 3, 20);
vector<int> path;
double min_path_weight = TSP(graph, path);
cout << "Minimum path weight: " << min_path_weight << endl;
return 0;
}