-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtsp.cpp
More file actions
369 lines (289 loc) · 8.71 KB
/
tsp.cpp
File metadata and controls
369 lines (289 loc) · 8.71 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*************************************************************************
Title: TSP.cpp
Description: TSP class implementation file for our Christofides implementation
Authors: Sean Hinds, Ryan Hong, Jeff Herlitz
Date: 08/16/17
*************************************************************************/
#include "tsp.h"
//Constructor
TSP::TSP(string in, string out){
iFile = in;
oFile = out;
ifstream inStream;
inStream.open(iFile.c_str(), ios::in);
if(!inStream){
cerr << "Can't open input file " << iFile << endl;
exit(1);
}
//READ DATA
int c, x, y;
int count = 0;
while(!inStream.eof()){
inStream >> c >> x >> y;
count++;
struct City newCity = {x,y};
cities.push_back(newCity);
}
count--;
cout << "cities created" << endl;
inStream.close();
//Initialize member variables
n = count;
graph = new int*[n];
for(int i = 0; i < n; i++){
graph[i] = new int[n];
for(int j = 0; j < n; j++){
graph[i][j] = 0;
}
}
cost = new int*[n];
for(int i = 0; i < n; i++){
cost[i] = new int[n];
}
path_vals = new int*[n];
for(int i = 0; i < n; i++){
path_vals[i] = new int[n];
}
adjlist = new vector<int>[n];
for(int i = 0; i < cities.size(); i++){
struct City cur = cities[i];
}
}
//Destructor
TSP::~TSP(){
for(int i = 0; i < n; i++){
delete [] graph[i];
delete [] cost[i];
delete [] path_vals[i];
}
delete [] path_vals;
delete [] graph;
delete [] cost;
delete [] adjlist;
}
int TSP::get_distance(struct TSP::City c1, struct TSP::City c2){
int dx = pow((float)(c1.x - c2.x),2);
int dy = pow((float)(c1.y - c2.y),2);
return floor((float)(sqrt(dx+dy) + .5));
}
void TSP::fillMatrix(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
graph[i][j] = graph[j][i] = get_distance(cities[i],cities[j]);
}
}
}
/******************************************************************************
This function uses Prim's algorithm to determine a minimum spanning tree on
the graph
******************************************************************************/
void TSP::findMST() {
int *key = new int[n];
bool *included = new bool[n];
int *parent = new int[n];
for (int i = 0; i < n; i++) {
// set each key to infinity
key[i] = std::numeric_limits<int>::max();
// node node yet included in MST
included[i] = false;
}
// root of MST has distance of 0 and no parent
key[0] = 0;
parent[0] = -1;
for (int i = 0; i < n - 1; i++) {
// find closes vertex not already in tree
int k = getMinIndex(key, included);
// set included to true for this vertex
included[k] = true;
// examine each unexamined vertex adjacent to most recently added
for (int j = 0; j < n; j++) {
// node exists, is unexamined, and graph[k][j] less than previous
// key for u
if (graph[k][j] && included[j] == false && graph[k][j] < key[j]) {
// update parent
parent[j] = k;
// update key
key[j] = graph[k][j];
}
}
}
// construct a tree by forming adjacency matrices
for (int i = 0; i < n; i++) {
int j = parent[i];
if (j != -1) {
adjlist[i].push_back(j);
adjlist[j].push_back(i);
}
}
}
/******************************************************************************
find the index of the closest unexamined node
******************************************************************************/
int TSP::getMinIndex(int key[], bool mst[]) {
// initialize min and min_index
int min = std::numeric_limits<int>::max();
int min_index;
// iterate through each vertex
for (int i = 0; i < n; i++) {
// if vertex hasn't been visited and has a smaller key than min
if (mst[i] == false && key[i] < min) {
// reassign min and min_index to the values from this node
min = key[i];
min_index = i;
}
}
return min_index;
}
/******************************************************************************
find all vertices of odd degree in the MST. Store them in an subgraph O
******************************************************************************/
void TSP::findOdds() {
for (int i = 0; i < n; i++) {
// if degree of vertex i is odd
if ((adjlist[i].size() % 2) != 0) {
// push vertex to odds, which is a representation of subgraph O
odds.push_back(i);
}
}
}
void TSP::perfectMatching() {
/************************************************************************************
find a perfect matching M in the subgraph O using greedy algorithm but not minimum
*************************************************************************************/
int closest, length; //int d;
std::vector<int>::iterator tmp, first;
// Find nodes with odd degrees in T to get subgraph O
findOdds();
// for each odd node
while (!odds.empty()) {
first = odds.begin();
vector<int>::iterator it = odds.begin() + 1;
vector<int>::iterator end = odds.end();
length = std::numeric_limits<int>::max();
for (; it != end; ++it) {
// if this node is closer than the current closest, update closest and length
if (graph[*first][*it] < length) {
length = graph[*first][*it];
closest = *it;
tmp = it;
}
} // two nodes are matched, end of list reached
adjlist[*first].push_back(closest);
adjlist[closest].push_back(*first);
odds.erase(tmp);
odds.erase(first);
}
}
//find an euler circuit
void TSP::euler_tour(int start, vector<int> &path){
//Create copy of adj. list
vector<int> *tempList = new vector<int>[n];
for(int i = 0; i < n; i++){
tempList[i].resize(adjlist[i].size());
tempList[i] = adjlist[i];
}
stack<int> stack;
int pos = start;
path.push_back(start);
while(!stack.empty() || tempList[pos].size() > 0){
//Current node has no neighbors
if(tempList[pos].empty()){
//add to circuit
path.push_back(pos);
//remove last vertex from stack and set it to current
pos = stack.top();
stack.pop();
}
//If current node has neighbors
else{
//Add vertex to stack
stack.push(pos);
//Take a neighbor
int neighbor = tempList[pos].back();
//Remove edge between neighbor and current vertex
tempList[pos].pop_back();
for(int i = 0; i < tempList[neighbor].size(); i++){
if(tempList[neighbor][i] == pos){
tempList[neighbor].erase(tempList[neighbor].begin()+i);
}
}
//Set neighbor as current vertex
pos = neighbor;
}
}
path.push_back(pos);
}
//Make euler tour Hamiltonian
void TSP::make_hamiltonian(vector<int> &path, int &pathCost){
//remove visited nodes from Euler tour
bool *visited = new bool[n];
for(int i = 0; i < n; i++){
visited[i] = 0;
}
pathCost = 0;
int root = path.front();
vector<int>::iterator cur = path.begin();
vector<int>::iterator iter = path.begin()+1;
visited[root] = 1;
//iterate through circuit
bool addMore = true;
while(iter != path.end()){
if(!visited[*iter]){
pathCost += graph[*cur][*iter];
cur = iter;
visited[*cur] = 1;
iter = cur + 1;
}
else{
iter = path.erase(iter);
}
}
//Add distance to root
if ( iter != path.end() ){
pathCost += graph[*cur][*iter];
}
}
int TSP::findBestPath(int start){
vector<int> path;
euler_tour(start, path);
int length;
make_hamiltonian(path, length);
return length;
}
void TSP::printResult(){
ofstream outputStream;
outputStream.open(oFile.c_str(), ios::out);
outputStream << pathLength << endl;
for (vector<int>::iterator it = circuit.begin(); it != circuit.end(); ++it) {
outputStream << *it << endl;
}
outputStream.close();
};
void TSP::printPath(){
cout << endl;
for (vector<int>::iterator it = circuit.begin(); it != circuit.end()-1; ++it) {
cout << *it << " to " << *(it+1) << " ";
cout << graph[*it][*(it+1)] << endl;
}
cout << *(circuit.end()-1) << " to " << circuit.front();
cout << "\nLength: " << pathLength << endl << endl;
};
void TSP::printEuler() {
for (vector<int>::iterator it = circuit.begin(); it != circuit.end(); ++it)
cout << *it << endl;
}
void TSP::printAdjList() {
for (int i = 0; i < n; i++) {
cout << i << ": "; //print which vertex's edge list follows
for (int j = 0; j < (int)adjlist[i].size(); j++) {
cout << adjlist[i][j] << " "; //print each item in edge list
}
cout << endl;
}
};
void TSP::printCities(){
cout << endl;
int i = 0;
for (vector<City>::iterator it = cities.begin(); it != cities.end(); ++it)
cout << i++ << ": " << it->x << " " << it->y << endl;
}