-
Notifications
You must be signed in to change notification settings - Fork 19
/
tsp.h
161 lines (111 loc) · 2.93 KB
/
tsp.h
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
//==================================================================
// File : tsp.h
// Author : rsagalyn
// Date : Aug 18, 2013
// Description :
//==================================================================
#ifndef MWM_H_
#define MWM_H_
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <pthread.h>
#include <queue>
#include <stack>
#include <string>
#include <stdio.h>
#include <vector>
#include "twoOpt.h"
using namespace std;
// Toggle printing debugging info to console
#define DEBUG 0
// Number of threads to use to fill N x N cost matrix
#define THREADS 1
// Calcualte lowest index controlled by thread id
#define START_AT(id,p,n) ((id)*(n)/(p))
// Calculate highest index controlled by thread id
#define END_AT(id,p,n) (START_AT((id)+1,p,n)-1)
class TSP
{
private:
// x and y coords of a node
struct City
{
int x;
int y;
};
// Filename supplied on command line to read from
string inFname;
// Program-generated filename to output to
string outFname;
// List of odd nodes
vector<int>odds;
// Smaller cost matrix used to store distances between odd nodes
// Used to find minimum matching on odd nodes
int **cost;
// Initialization function
void getNodeCount();
// Find odd vertices in graph
void findOdds();
// Prim helper function
int minKey(int key[], bool mstSet[]);
protected:
public:
// Number of nodes
int n;
// euler circuit
vector<int>circuit;
// Store cities and coords read in from file
vector<City>cities;
// Full n x n cost matrix of distances between each city
int **graph;
// Current shortest path length
int pathLength;
// Adjacency list
// Array of n dynamic arrays, each holding a list of nodes it's index is attached to
vector<int> *adjlist;
int start_idx[THREADS];
int end_idx[THREADS];
// n x 2 array to store length of TSP path starting at each node
// col 0 => starting index col 1 => path length from that node
int **path_vals;
// Constructor
TSP(string in, string out);
// Destructor
~TSP();
// Calculate distance
int get_distance(struct City c1, struct City c2);
// Initialization functions
void readCities();
void fillMatrix_threads();
// Find MST using Prim's algorithm
void findMST_old();
// Find perfect matching
void perfect_matching();
// Find best node to start euler at
// Doesn't create tour, just checks
int find_best_path(int);
// Create tour starting at specified node
void create_tour(int);
// Private functions implemented by create_tour() and find_best_path()
void euler (int pos, vector<int> &);
//void euler(int);
void make_hamilton(vector<int> &, int&);
// Calls twoOpt function
void make_shorter();
// Debugging functions
void printCities();
void printAdjList();
void printResult();
void printEuler();
void printPath();
// Get node count
int get_size() {return n;};
};
#endif /* MWM_H_ */