-
Notifications
You must be signed in to change notification settings - Fork 122
/
clustering.cc
49 lines (42 loc) · 1.32 KB
/
clustering.cc
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
#include "clustering.h"
namespace L3DPP
{
//------------------------------------------------------------------------------
CLUniverse* performClustering(std::list<CLEdge>& edges, int numNodes,
float c)
{
if(edges.size() == 0)
return NULL;
// sort edges by weight (increasing)
edges.sort(L3DPP::sortCLEdgesByWeight);
// init universe
CLUniverse *u = new CLUniverse(numNodes);
// init thresholds
float* threshold = new float[numNodes];
for(int i=0; i < numNodes; ++i)
threshold[i] = c;
// perform clustering
std::list<CLEdge>::const_iterator it = edges.begin();
for(; it!=edges.end(); ++it)
{
CLEdge e = *it;
// components conected by this edge
int a = u->find(e.i_);
int b = u->find(e.j_);
if (a != b)
{
// not in the same segment yet
if((e.w_ <= threshold[a]) && (e.w_ <= threshold[b]))
{
// join nodes
u->join(a,b);
a = u->find(a);
threshold[a] = e.w_ + c/u->size(a);
}
}
}
// cleanup
delete threshold;
return u;
}
}