-
Notifications
You must be signed in to change notification settings - Fork 1
/
NN.hpp
45 lines (43 loc) · 1.25 KB
/
NN.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
#ifndef NN_HPP
#define NN_HPP
// #include "Bitmap.hpp"
#include <limits>
#include <vector>
#include "Bitmap.hpp"
/**
* @brief Executes Nearest Neighbour algorithm starting at the given city
*
* @param mat the distance matrix
* @param start_city the initial city
* @return int the length found.
*/
int NN(std::vector<std::vector<double>> *mat, int start_city) {
int curr_city = start_city;
// random starting city:
int n = (*mat).size();
Bitmap visited(n);
visited.set(start_city, 1);
int total_dist = 0;
for (int i=1; i<n;++i) { // for each city
int best_dist = std::numeric_limits<int>::max();
int best_city = -1;
for(int j=0; j<n; ++j) { // find next city
if (curr_city != j && !visited.get(j)) {
int dist = (*mat)[curr_city][j];
if(dist < best_dist) {
// std::cout << 's' <<std::endl;
best_dist = dist;
best_city = j;
}
}
}
// std::cout << best_dist << std::endl;
total_dist += best_dist;
visited.set(best_city, 1);
curr_city = best_city;
}
// add last node:
total_dist += (*mat)[start_city][curr_city];
return total_dist;
}
#endif