-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreedyTSP.hpp
361 lines (289 loc) · 10.2 KB
/
GreedyTSP.hpp
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
/*
1) Calculate weights of all edges
2) Sort all edges by weight
3) Repeatedly add minimum weight edges that won't make cycle
*/
#ifndef GREEDY_TSP_HPP
#define GREEDY_TSP_HPP
#include <iostream>
#include <cmath>
#include <vector>
#include <list>
#include <chrono>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <utility>
#include <queue>
#include <unordered_map>
#include <unordered_set>
class Node {
public:
int id; // unique identifier for city
double x, y; // coordinates
Node* neighbor1;
Node* neighbor2;
int neighbor_count;
Node() = default;
// parameterized constructor
Node(int _id, double _x, double _y) : id(_id), x(_x), y(_y), neighbor1(nullptr), neighbor2(nullptr), neighbor_count(0) {}
// used to calculate Euclidian distance (root(x^2 + y^2))
// between this node and another node
double distance(const Node& other) const {
double dx = x - other.x;
double dy = y - other.y;
return std::round(std::sqrt(dx * dx + dy * dy)); // euclidean distance
}
void addNeighbor(Node* &other){
if(neighbor1 == nullptr){
neighbor1 = other;
neighbor_count++;
}
else if(neighbor2 == nullptr){
neighbor2 = other;
neighbor_count++;
}
else{
// throw exception, can't add 3rd neighbor
throw std::runtime_error("Can't add 3rd neighbor");
}
}
void removeNeighbor(Node* &other){
if(neighbor1 == other){
neighbor1 = nullptr;
}
else if(neighbor2 == other){
neighbor2 = nullptr;
}
else{
// throw exception, can't add 3rd neighbor
std::cout << "Error: Tried to remove neighbor that isn't there.\n";
throw std::runtime_error("Error: Tried to remove neighbor that isn't there.");
}
}
int neighborCount(){
return neighbor_count;
}
};
class Edge {
public:
// unordered pair of nodes
std::pair<Node*, Node*> edge;
int edge_weight;
Edge(Node* _n1, Node* _n2): edge(std::make_pair(_n1,_n2)), edge_weight(_n1->distance(*_n2)){}
bool operator<(const Edge& other) const {
return edge_weight > other.edge_weight;
}
};
class GreedyTSP {
private:
std::priority_queue<Edge> edges;
// std::list<Node> tour;
double totalDistance;
int tour_edge_count;
std::unordered_map<int, Node*> table;
public:
GreedyTSP() = default;
// Constructor
GreedyTSP(const std::vector<Node>& nodes){
// add nodes to direct address table
for (auto n : nodes){
Node * newNode = new Node(n.id,n.x,n.y);
table.insert(std::make_pair(n.id,newNode));
// std::cout<< table.at(n.id).x;
}
totalDistance = 0.0;
tour_edge_count = table.size();
}
~GreedyTSP(){
for (auto& pair : table){
delete pair.second;
pair.second = nullptr;
}
}
// print outputs (ids visited in order, distance and duration)
void printResult(long long duration) const {
// print tour from node 1 back to node 1
Node *tmp = table.at(1);
Node *start = tmp; // Store the starting node
std::unordered_set<int> visited;
do{
std::cout << tmp->id << " ";
visited.insert(tmp->id);
// std::cout << visited.count(tmp->neighbor1->id) << " ";
if (visited.count(tmp->neighbor1->id) < 1){
tmp = tmp->neighbor1;
}
else if(visited.count(tmp->neighbor2->id) < 1) {
tmp = tmp->neighbor2;
}
else{
// std::cout << "CHECK\n";
break;
}
}while(visited.size() != table.size());
std::cout << start->id;
std::cout << "\nTotal Distance: " << totalDistance << "\n";
std::cout << "Time in ms: " << duration << "\n";
}
bool hasCycleUtil(Node* current, Node* parent, std::unordered_set<Node*>& visited, Node* target) {
if (visited.count(current)) {
return true; // Cycle detected
}
visited.insert(current);
if (current == target) {
return false; // Target node reached without a cycle
}
// Recursively explore neighbors:
if (current->neighbor1 != nullptr && current->neighbor1 != parent) {
if (hasCycleUtil(current->neighbor1, current, visited, target)) {
return true;
}
}
if (current->neighbor2 != nullptr && current->neighbor2 != parent) {
if (hasCycleUtil(current->neighbor2, current, visited, target)) {
return true;
}
}
return false; // No cycle found from this node
}
bool hasCycle(Node* np1, Node* np2) {
std::unordered_set<Node*> visited;
return hasCycleUtil(np1, nullptr, visited, np2) || hasCycleUtil(np2, nullptr, visited, np1);
}
// function checks if an edge can be added between two nodes
// based on two conditions:
// 1) if either node already has 2 edges we can't add an edge
// 2) if adding an edge between these nodes creates a cycle, we can't add edge
bool checkValidEdge(Node* np1, Node* np2) {
if (np1 == nullptr || np2 == nullptr) {
std::cout << "ERROR\n";
throw std::runtime_error("Error");
}
if (np1->neighbor_count == 2 || np2->neighbor_count == 2) {
return false;
}
// Check if np1 already has np2 as a neighbor
if (np1->neighbor1 == np2 || np1->neighbor2 == np2) {
return false;
}
// Check if np2 already has np1 as a neighbor
if (np2->neighbor1 == np1 || np2->neighbor2 == np1) {
return false;
}
// Check if adding nodes as neighbors creates a cycle
if (hasCycle(np1, np2)) {
return false; // Edge would create a cycle
}
return true;
}
// main function
void run() {
auto start = std::chrono::high_resolution_clock::now();
// Create an ordered list of all edges sorted by weight
// Create edges between all unvisited nodes:
for (auto it1 = table.begin(); it1 != table.end(); ++it1) {
for (auto it2 = std::next(it1); it2 != table.end(); ++it2) {
// Create an edge between the two nodes:
Edge edge(it1->second, it2->second); // Note: Using Node objects directly in Edge constructor
edges.push(edge);
}
}
int current_edges = 0;
// add minimum edges until you reach maximum number of edges possible |E| = |V| for tour
while (current_edges < tour_edge_count && !edges.empty()) {
Node* n1 = edges.top().edge.first;
Node* n2 = edges.top().edge.second;
if (checkValidEdge(n1, n2)) {
n1->addNeighbor(n2);
n2->addNeighbor(n1);
current_edges++;
std::cout << "Edge from " << n1->id << " to " << n2->id << " of weight " << edges.top().edge_weight << "\n";
totalDistance += edges.top().edge_weight;
}
edges.pop();
}
// finish tour by connecting last two nodes without 2 neighbors
Node* tourStart = nullptr;
Node* tourEnd = nullptr;
for (auto it : table){
if (it.second->neighbor_count == 1){
// std::cout << it.second->id << std::endl;
if(tourStart == nullptr){
tourStart = it.second;
// std::cout << "HELLO" << tourStart->id << std::endl;
}
else if (tourEnd == nullptr){
tourEnd = it.second;
break;
}
}
}
tourStart->addNeighbor(tourEnd);
tourEnd->addNeighbor(tourStart);
totalDistance += tourStart->distance(*tourEnd);
current_edges++;
std::cout << "Edge from " << tourStart->id << " to " << tourEnd->id << " of weight " << tourStart->distance(*tourEnd) << "\n";
// std::cout << current_edges << std::endl;
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
printResult(duration); // prints ids visited, distance and duration
}
};
// Function to trim whitespaces from the beginning and end of a string
// helper for nodesFromInput
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\n\r\f\v");
size_t last = str.find_last_not_of(" \t\n\r\f\v");
return (first != std::string::npos && last != std::string::npos) ? str.substr(first, last - first + 1) : "";
}
// creates a vector of Node objects from input text file
std::vector<Node> nodesFromInput(std::string filename){
std::ifstream input(filename);
if (!input.is_open()) {
std::cerr << "Error opening file.\n";
return {};
}
std::vector<Node> nodes;
std::string line;
bool readingNodes = false;
while (std::getline(input, line)) {
// Trim whitespaces from the line
line = trim(line);
if (line == "NODE_COORD_SECTION") {
// std::cout << "found ncs\n";
readingNodes = true;
continue;
}
if (readingNodes && line == "EOF") {
// std::cout << line << std::endl;
break; // Stop reading after reaching "EOF"
}
if (readingNodes && !line.empty()) {
// std::cout << "hello\n";
// Parse node data
int id;
double x, y;
std::istringstream iss(line);
iss >> id >> x >> y;
if (iss.fail()) {
std::cerr << "Error parsing line: " << line << "\n";
return {};
}
nodes.emplace_back(id, x, y);
}
}
return nodes;
}
void greedyTSP(std::string filename){
std::vector<Node> nodes = nodesFromInput(filename);
if (nodes.empty()){
std::runtime_error("Didn't read data correctly\n");
return;
}
GreedyTSP nn(nodes);
nn.run();
return;
}
#endif