forked from elbamos/largeVis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbscan.cpp
100 lines (90 loc) · 2.8 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
// [[Rcpp::plugins(openmp)]]
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppProgress)]]
#include <RcppArmadillo.h>
#include <Rmath.h>
#include <progress.hpp>
//#define DEBUG
using namespace Rcpp;
using namespace std;
using namespace arma;
class DBSCAN {
protected:
const sp_mat* edges;
const imat* neighbors;
const long double eps;
const unsigned int minPts;
const long long N;
vector< bool > visited;
vector< int > clusterAssignments;
Progress progress;
list< long long > regionQuery(long long& p) const {
set< long long > holder = set< long long >();
bool exceeded = false;
for (auto it = neighbors -> begin_col(p);
it != neighbors -> end_col(p);
it ++) {
if (*it == -1 || (*edges)(p, *it) > eps) {
exceeded = true;
break;
}
holder.insert(*it);
}
if (! exceeded) {
for (auto it = edges -> begin_col(p);
it != edges -> end_col(p);
it++) {
if (*it < eps) holder.insert(it.row());
}
}
list< long long > ret = list< long long >(holder.begin(), holder.end());
return ret;
}
void expandCluster(long long& P, list< long long >& pNeighbors, int& C) {
clusterAssignments[P] = C;
for (auto pprime = pNeighbors.begin(); pprime != pNeighbors.end(); pprime++) {
if (! visited[*pprime]) {
visited[*pprime] = true;
list< long long > pprimeNeighbors = regionQuery(*pprime);
if (pprimeNeighbors.size() >= minPts - 1) {
pNeighbors.insert(pNeighbors.end(), pprimeNeighbors.begin(), pprimeNeighbors.end());
}
}
if (clusterAssignments[*pprime] == -1) clusterAssignments[*pprime] = C;
}
}
public:
DBSCAN(const sp_mat& edges,
const imat& neighbors,
const double& eps,
const unsigned int& minPts,
bool verbose) : edges{&edges}, neighbors{&neighbors},
eps{eps}, minPts{minPts}, N(neighbors.n_cols),
visited(vector< bool >(N, false)),
clusterAssignments(vector<int>(N, -1)),
progress(Progress(N, verbose)) {
if (neighbors.n_rows < minPts) throw Rcpp::exception("Insufficient Neighbors.");
}
IntegerVector run() {
int C = -1;
for (long long p = 0; p < N; p++) if (progress.increment() && ! visited[p]) {
visited[p] = true;
list< long long > pNeighbors = regionQuery(p);
if (pNeighbors.size() >= minPts - 1) {
++C;
expandCluster(p, pNeighbors, C);
}
}
return IntegerVector(clusterAssignments.begin(), clusterAssignments.end()) + 1;
}
};
// [[Rcpp::export]]
IntegerVector dbscan_cpp(const arma::sp_mat& edges,
const arma::imat& neighbors,
double eps,
int minPts,
bool verbose) {
DBSCAN db = DBSCAN(edges, neighbors, eps, minPts, verbose);
return db.run();
}