forked from sth144/christofides-algorithm-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
75 lines (61 loc) · 1.79 KB
/
main.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
/*************************************************************************
Title: main.cpp
Description: main method for our Christofides implementation
Authors: Sean Hinds, Ryan Hong, Jeff Herlitz
Date: 08/16/17
Changes:
- cities coordinate changed from int to double
- removed useless path_vals
*************************************************************************/
#include <iostream>
#include "tsp.h"
//#include "twoOpt.h"
int main(int argc, char** argv) {
if(argc < 2){
exit(-1);
}
// Read file names from input
string input, output;
input = output = argv[1];
output.append(".tour");
// Create new tsp object
TSP tsp(input, output);
cout << "tsp created" << endl;
int tsp_size = tsp.get_size();
// Fill N x N matrix with distances between nodes
cout << "Fillmatrix started" << endl;
tsp.fillMatrix();
cout << "Filled Matrix" << endl;
// Find a MST T in graph G
tsp.findMST();
cout << "MST created" << endl;
// Find a minimum weighted matching M for odd vertices in T
tsp.perfectMatching();
cout << "Matching completed" << endl;
// Loop through each index and find shortest path
TSP::distance_t best = TSP::DINF;
int bestIndex = -1;
for (long t = 0; t < tsp_size; t++) {
TSP::distance_t result = tsp.findBestPath(t);
if (result < best) {
bestIndex = t;
best = result;
}
}
cout << "BestPath completed " << bestIndex << endl;
//Create path for best tour
tsp.euler_tour(bestIndex,tsp.circuit);
tsp.make_hamiltonian(tsp.circuit,tsp.pathLength);
/*
tsp.euler_tour(0, tsp.circuit);
cout << "euler completed" << endl;
tsp.make_hamiltonian(tsp.circuit, tsp.pathLength);
cout << "ham completed" << endl;
*/
// Store best path
//tsp.create_tour(bestIndex);
cout << "Final length: " << tsp.pathLength << endl;
// Print to file
tsp.printPath();
tsp.printResult();
}