-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtsp.cpp
More file actions
56 lines (45 loc) · 1.25 KB
/
tsp.cpp
File metadata and controls
56 lines (45 loc) · 1.25 KB
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
#include<iostream>
using namespace std;
int n = 4, tour_brute[10], cost_brute = 999, v[10];
int G[4][4]={ {0, 1, 3, 6},
{1, 0, 2, 3},
{3, 2, 0, 1},
{6, 3, 1, 0} };
void display(int arr[])
{ for(int q = 0; q < n; ++q) cout<<arr[q]<<" "; }
void tsp_brute(int cur)
{
if(cur == n)
{
int t_cost = 0;
//Calculate cost of current route
for(int i = 0; i < n-1; ++i)
t_cost += G[v[i]][v[i+1]];
t_cost += G[v[n-1]][v[0]]; //Add cost of last-to-first path
//Find minimum of such costs
if(t_cost < cost_brute)
{
cost_brute = t_cost;
for(int j = 0; j < n; ++j)
tour_brute[j] = v[j];
}
}
else
//Swap current node with all other remaining ones and calculate cost
for(int i = cur; i < n; ++i)
{
int temp = v[cur]; v[cur] = v[i]; v[i] = temp;
tsp_brute(cur+1);
temp = v[cur]; v[cur] = v[i]; v[i] = temp;
}
}
int main()
{
//Set a default route
for(int i = 0; i < n; ++i)
v[i] = i;
tsp_brute(0);
display(tour_brute);
cout<<"\nCost: "<<cost_brute<<endl;
return 0;
}