-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbscan.cpp
364 lines (331 loc) · 11.6 KB
/
dbscan.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
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
#include <iostream>
#include <unordered_map>
#include <set>
#include <vector>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <cmath>
#include "nanoflann.hpp"
#include <ctime>
#include <cstdlib>
#include "KDTreeVectorOfVectorsAdaptor.h"
using namespace std;
using namespace nanoflann;
void run_dbscan(int min_points, double epsilon, const string input_file_name, const string output_file_name);
double get_distance(int one, int two);
void initialise_neighbourhood(double epsilon);
bool expand_cluster(int p, int cluster_label, double epsilon, int min_points);
void delete_vector(vector<int> &v, int value);
void print_vector(vector<int> vec);
void kdtree_demo(const size_t nSamples, const size_t dim, double epsilon);
void print_neighbourhood();
#define UNCLASSIFIED -2
#define NOISE -1
typedef std::vector<std::vector<double>> my_vector_of_vectors_t;
my_vector_of_vectors_t samples;
struct point
{
int id; //unique id of each point
int label_number; // -2 -> unclassified, -1 -> noise, >= 0 -> cluster ID
int neighbour_number; // number of core points from the neightbourhood of this point earlier deleted from D
vector<int> neighbourhood; //int ids of all the points which are in epsilon-neighbourhood of this point
};
int main(int argc, char *argv[])
{
int min_points = atoi(argv[1]);
double epsilon = atof(argv[2]);
string infile = argv[3];
string outfile = "dbscan.txt";
run_dbscan(min_points, epsilon, infile, outfile);
return 0;
}
unordered_map<int, point *> point_map;
unordered_map<int, int> retrieve_map;
int number_of_points;
int number_of_dims;
void run_dbscan(int min_points, double epsilon, const string input_file_name, const string output_file_name)
{
//read the input file here, and generate a map fr
ifstream inputFile(input_file_name);
if (!inputFile)
{
cout << "Unable to open file";
exit(1); // terminate with error
}
double dimension;
long absolute_count = 1;
string str;
while (getline(inputFile, str))
{
point *p = (point *)calloc(1, sizeof(point));
p->id = absolute_count;
p->label_number = UNCLASSIFIED; //UNCLASSIFIED
p->neighbour_number = 0; // NO NEIGHBOURS DELETED INTITALLY
//uncomment the line below when using the brute force implementation for finding the neighbourhood
// p->neighbourhood.emplace_back(absolute_count);
istringstream inf(str);
vector<double> point_dimension;
while (inf >> dimension)
{
point_dimension.emplace_back(dimension);
}
samples.emplace_back(point_dimension);
point_map[absolute_count++] = p;
}
cout << "DATASET READ." << endl;
//assigning some globals
number_of_points = point_map.size();
number_of_dims = samples[0].size();
//initialising the neighbourhood value of each point
initialise_neighbourhood(epsilon);
cout << "INITIALISATION DONE." << endl;
//Data stricture is done -> run the dbscan algorithm
int cluster_label = 0;
for (int i = 1; i <= number_of_points; i++)
{
if (point_map.find(i) != point_map.end() && point_map[i]->label_number == UNCLASSIFIED)
{
cout << "CURRENT POINT : " << i << endl;
if (expand_cluster(i, cluster_label, epsilon, min_points))
{
cluster_label ++;
}
}
}
cout << "TOTAL NUMEBR OF CLUSTERS : " << cluster_label << endl;
//output the results
unordered_map<int, vector<int>> clusters;
for(int i = 1; i <= number_of_points; i ++)
{
int current_label = -10000;
if(point_map.find(i) != point_map.end())
{
current_label = point_map[i]->label_number;
}
else if(retrieve_map.find(i) != retrieve_map.end())
{
current_label = retrieve_map[i];
}
clusters[current_label].emplace_back(i);
}
ofstream outfile;
outfile.open(output_file_name);
for(int i = 0; i < cluster_label; i ++)
{
outfile << "#" << i << "\n";
for(int point : clusters[i])
{
outfile << point - 1 << "\n";
}
}
if(clusters[NOISE].size() > 0)
{
outfile << "#outlier\n";
for(int point : clusters[NOISE])
{
outfile << point - 1 << "\n";
}
}
outfile.close();
cout << "FILE CREATED." << endl;
//this is only for checking purposes
outfile.open("dbscan_check.txt");
for(int i = 1; i <= number_of_points; i ++)
{
int current_label = -10000;
if (point_map.find(i) != point_map.end())
{
current_label = point_map[i]->label_number;
}
else if (retrieve_map.find(i) != retrieve_map.end())
{
current_label = retrieve_map[i];
}
outfile << current_label << endl;
}
outfile.close();
return;
}
//returns the euclidean distance between two points
double get_distance(vector<double> &vec1, vector<double> &vec2)
{
double distance = 0.0;
for (int i = 0; i < number_of_dims; i++)
{
double diff = vec1[i] - vec2[i];
distance += (diff * diff);
}
return sqrt(distance);
}
//this method is used for intialising the neighbourhood of all the points as per the epsilon value, and updates the point_map as well
void initialise_neighbourhood(double epsilon)
{
//below is the kd tree used for finding the points within epsilon
srand(static_cast<unsigned int>(time(nullptr)));
kdtree_demo(number_of_points, number_of_dims, epsilon);
// print_neighbourhood();
// exit(0);
//below is the bad algorithm used to find the neighbourhood
// for (int i = 1; i <= number_of_points - 1; i++)
// {
// // cout << i << endl;
// for (int j = i + 1; j <= number_of_points; j++)
// {
// if (get_distance(samples[i - 1], samples[j - 1]) <= epsilon)
// {
// point_map[i]->neighbourhood.emplace_back(j);
// point_map[j]->neighbourhood.emplace_back(i);
// }
// }
// }
// print_neighbourhood();
// exit(0);
}
bool expand_cluster(int p, int cluster_label, double epsilon, int min_points)
{
vector<int> seeds = point_map[p]->neighbourhood;
point_map[p]->neighbour_number += seeds.size();
if (point_map[p]->neighbour_number < min_points)
{
// cout << "1.9" << endl;
point_map[p]->label_number = NOISE;
return false;
}
else
{
point_map[p]->label_number = cluster_label;
// cout << "2.0" << endl;
// cout << "p : " << p << endl;
vector<int> p_neighbours = point_map[p]->neighbourhood;
// print_vector(p_neighbours);
for (int neighbour : p_neighbours)
{
// cout << "2.01" << endl;
// cout << "neighbour : " << neighbour << endl;
// print_vector(point_map[neighbour]->neighbourhood);
delete_vector(point_map[neighbour]->neighbourhood, p);
// print_vector(point_map[neighbour]->neighbourhood);
}
retrieve_map[p] = point_map[p]->label_number; //this will help to retrieve it back later
point_map.erase(p);
// cout << "point deleted from D : " << p << endl;
// cout << "2.1" << endl;
delete_vector(seeds, p);
// cout << "point deleted from seeds : " << p << endl;
// cout << "2.2" << endl;
for (int q : seeds)
{
if (point_map[q]->label_number == NOISE)
{
point_map[q]->label_number = cluster_label;
delete_vector(seeds, q);
}
else
{
point_map[q]->label_number = cluster_label;
point_map[q]->neighbour_number++;
}
}
// cout << "2.3" << endl;
while (seeds.size() > 0)
{
// cout << "seeds.size() : " << seeds.size() << endl;
// print_vector(seeds);
int curPoint = seeds[0];
// cout << "curPoint : " << curPoint << endl;
vector<int> curSeeds = point_map[curPoint]->neighbourhood;
// cout << "curSeeds.size() : " << curSeeds.size() << endl;
point_map[curPoint]->neighbour_number += curSeeds.size();
// cout << "2.4" << endl;
if (point_map[curPoint]->neighbour_number >= min_points)
{
// cout << "2.44" << endl;
for (int q : curSeeds)
{
// cout << "q : " << q << endl;
// cout << "2.45" << endl;
if (point_map[q]->label_number == UNCLASSIFIED)
{
point_map[q]->label_number = cluster_label;
point_map[q]->neighbour_number++;
seeds.emplace_back(q);
}
else if (point_map[q]->label_number == NOISE)
{
point_map[q]->label_number = cluster_label;
}
}
// cout << "2.5" << endl;
// cout << "curPoint : " << curPoint << endl;
vector<int> curPoint_neighbours = point_map[curPoint]->neighbourhood;
for (int neighbour : curPoint_neighbours)
{
// cout << "neighbour : " << neighbour << endl;
// print_vector(point_map[neighbour]->neighbourhood);
delete_vector(point_map[neighbour]->neighbourhood, curPoint);
// print_vector(point_map[neighbour]->neighbourhood);
}
retrieve_map[curPoint] = point_map[curPoint]->label_number;
point_map.erase(curPoint);
// cout << "point deleted from point_map : " << curPoint << endl;
// cout << "2.6" << endl;
}
delete_vector(seeds, curPoint);
// cout << "point deleted from seeds : " << curPoint << endl;
// cout << "2.7" << endl;
}
// cout << "2.8" << endl;
return true;
}
}
void delete_vector(vector<int> &vec, int value)
{
vec.erase(std::remove(vec.begin(), vec.end(), value), vec.end());
}
void print_vector(vector<int> vec)
{
for(int something : vec)
{
cout << something << " ";
}
cout << endl;
return;
}
void kdtree_demo(const size_t nSamples, const size_t dim, double epsilon)
{
// Generate points:
// generateRandomPointCloud(samples, nSamples, dim);
// construct a kd-tree index:
// Dimensionality set at run-time (default: L2)
// ------------------------------------------------------------
typedef KDTreeVectorOfVectorsAdaptor<my_vector_of_vectors_t, double> my_kd_tree_t;
my_kd_tree_t mat_index(dim /*dim*/, samples, 10 /* max leaf */);
mat_index.index->buildIndex();
const double search_radius = static_cast<double>(epsilon * epsilon);
std::vector<std::pair<size_t, double>> ret_matches;
nanoflann::SearchParams params;
//params.sorted = false;
for(int i = 1; i <= number_of_points; i ++)
{
const size_t nMatches = mat_index.index->radiusSearch(&samples[i - 1][0], search_radius, ret_matches, params);
for(size_t j = 0; j < nMatches; j ++)
{
point_map[i]->neighbourhood.emplace_back(ret_matches[j].first + 1);
}
}
}
void print_neighbourhood()
{
for(int i = 1; i <= number_of_points; i ++)
{
cout << i << " : ";
vector<int> vec = point_map[i]->neighbourhood;
sort(vec.begin(), vec.end());
for(int j : vec)
{
cout << j << " ";
}
cout << endl;
}
}