-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravelling-salesman.cpp
More file actions
173 lines (149 loc) · 5.22 KB
/
travelling-salesman.cpp
File metadata and controls
173 lines (149 loc) · 5.22 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<bits/stdc++.h>
using namespace std;
struct Path{
vector<int> path;
};
class comparision{
//for cost comparision
public:
bool operator()(Path p1, Path p2) const
{
int size_p1 = p1.path.size();
int size_p2 = p2.path.size();
return (
p1.path[size_p1 - 1] + p1.path[size_p1 -2]
>
p2.path[size_p2 - 1] + p2.path[size_p2 -2]
);
}
};
class TSP{
private:
int cities, edges;
int **edgeWeights;
int minCost = INT_MAX;
int start;
Path States;
priority_queue<Path, vector<Path>, comparision> pq;
vector<int> solutions;
public:
TSP(int cities){
this->cities = cities;
edges = (cities * (cities-1))/2;
edgeWeights = new int*[cities];
for(int i=0; i<cities; ++i){
edgeWeights[i] = new int[cities];
}
input();
}
void input(){
cout<<"city1 city2 edgeWeight ? \n";
// cout<<edges<<endl;
for(int i=0; i<edges; ++i){
int u, v, cost;
cin>>u>>v>>cost;
edgeWeights[u][v] = cost;
edgeWeights[v][u] = cost;
if(minCost > cost){
minCost = cost;
}
}
cout<<"Starting city? ";
cin>>start;
}
void A_star(){
for(int i=0; i<cities; ++i){
if(i == start)
States.path.push_back(1);
else if(i != cities)
States.path.push_back(-1);
}
States.path.push_back(0);
States.path.push_back(0);
int t = 0;
pq.push(States);
for(int i=0; i<=cities+1; ++i){
if(i==cities || i==cities+1)
solutions.push_back(100000);
else
solutions.push_back(-1);
}
while (!pq.empty())
{
Path tpath;
tpath = pq.top();
pq.pop();
if(isGoal(tpath.path)){
++t;
if( solutions[solutions.size() - 1] + solutions[solutions.size() - 2] > tpath.path[tpath.path.size() -1] + tpath.path[tpath.path.size() -2])
solutions = tpath.path;
}
int max=0, maxi=0;
for(int i=0; i<tpath.path.size()-2; ++i){
if(tpath.path[i] > max){
max = tpath.path[i];
maxi = i;
}
}
int visits = 0;
if( max < tpath.path.size()-1){
for(int i=0; i<cities; ++i){
int gn = tpath.path[tpath.path.size() -1 ] + edgeWeights[maxi][i];
int hn = minCost * (cities - max - 1) + edgeWeights[maxi][i];
if(edgeWeights[i][maxi] > 0){
Path tmpPath;
// vector<int> tmp;
for(int x=0; x<tpath.path.size(); ++x){
tmpPath.path.push_back(tpath.path[x]);
}
if(tmpPath.path[i]==-1){
tmpPath.path[i] = max + 1;
tmpPath.path[tmpPath.path.size() - 1] = gn;
tmpPath.path[tmpPath.path.size() - 2] = hn;
pq.push(tmpPath);
visits++;
}
}
}
}
if (visits == 0 && max < tpath.path.size() - 1) {
int gn = tpath.path[tpath.path.size() - 1] + edgeWeights[maxi][start];
int hn = minCost * (cities - max - 1) + edgeWeights[maxi][start];
Path tmpPath;
tmpPath.path = tpath.path;
tmpPath.path[start] = max + 1;
tmpPath.path[tmpPath.path.size() - 1] = gn;
tmpPath.path[tmpPath.path.size() - 2] = hn;
pq.push(tmpPath);
}
}
}
bool isGoal(vector<int> path){
return path[start] == path.size() -1 ;
}
void printResult(){
cout << "Path: " << start << " -> ";
for (int i = 2; i <= solutions.size() - 1; i++) {
for (int j = 0; j < solutions.size() - 2; j++) {
if (solutions[j] == i){
if(solutions.size() - 1 != i){
cout << j << " -> ";
}else{
cout<<j;
}
}
}
}
cout << "\nTravelling cost: " << solutions[solutions.size() - 1] << "\n";
}
};
int main(){
int cities;
cout<<"Total number of cities ? ";
cin>>cities;
TSP *tsp = new TSP(cities);
// tsp->input();
tsp->A_star();
tsp->printResult();
return 0;
}