-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroupingMethod.cpp
297 lines (255 loc) · 8.08 KB
/
GroupingMethod.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
//#define TRACE_GROUPING
#ifdef TRACE_GROUPING
#include <iostream>
#include <iomanip>
#endif
#include <vector>
#include <cmath>
#include <cfloat>
#include <cstring>
#include "GroupingMethod.h"
using namespace cfp_internal;
void PseudoSNNGrouping::computeConnectionMatrix(size_t aNumStructures, const DistanceMatrix& aDistances, unsigned int* aConnection) const
{
// Build the connection matrix
for(unsigned int r=0; r < aNumStructures; ++r)
{
for(unsigned int c=r+1; c < aNumStructures; ++c)
{
float dist = aDistances(r, c);
aConnection[c*aNumStructures+r] = aConnection[r*aNumStructures+c] = (dist < mMaxDistanceForGrouping) ? 1 : 0;
}
}
for(unsigned int k=0; k < aNumStructures; ++k) aConnection[k*(aNumStructures+1)] = 0;
}
void PseudoSNNGrouping::doDepthFirstVisit(unsigned int aIdx, bool *aAssigned, std::set<unsigned int>& aGroups, unsigned int* aConnection, size_t aNumStructures) const
{
// Assign the node to the connected component
aAssigned[aIdx] = true;
aGroups.insert(aIdx);
// For each node to which it is connected
for(unsigned int j=0; j < aNumStructures; ++j)
{
if(aAssigned[j]) continue;
if(aConnection[aIdx*aNumStructures+j] != 0) doDepthFirstVisit(j, aAssigned, aGroups, aConnection, aNumStructures);
}
}
void PseudoSNNGrouping::doGrouping(size_t aNumStructures, const DistanceMatrix& aDistances, std::vector< std::set<unsigned int> >& aResult)
{
// Compute the connection matrix
unsigned int* connection = new unsigned int[aNumStructures*aNumStructures];
computeConnectionMatrix(aNumStructures, aDistances, connection);
// Compute the number of shared NN
unsigned int idx1, idx2, j;
for(idx1=0; idx1 < aNumStructures-1; ++idx1)
{
for(idx2=idx1+1; idx2 < aNumStructures; ++idx2)
{
// Do nothing if nodes not connected
if(connection[idx1*aNumStructures+idx2] == 0) continue;
// For all the connections to node1
for(j=0; j < aNumStructures; ++j)
{
// if j is shared between node-1 and node-2
if(j != idx1 && j != idx2 && connection[idx1*aNumStructures+j] != 0 && connection[idx2*aNumStructures+j] != 0)
{
++connection[idx1*aNumStructures+idx2];
++connection[idx2*aNumStructures+idx1];
}
}
}
}
// Now remove connections with only one connection except for standalone pairs
for(idx1=0; idx1 < aNumStructures-1; ++idx1)
{
for(idx2=idx1+1; idx2 < aNumStructures; ++idx2)
{
// Do nothing if nodes have none or more than one SNN
if(connection[idx1*aNumStructures+idx2] != 1) continue;
// Count the number of connections from node1
unsigned int nconn1 = 0;
for(j=0; j < aNumStructures; ++j) if(connection[idx1*aNumStructures+j] != 0) ++nconn1;
// Count the number of connections from node2
unsigned int nconn2 = 0;
for(j=0; j < aNumStructures; ++j) if(connection[idx2*aNumStructures+j] != 0) ++nconn2;
// If it is a bridge, break it
if(nconn1 > 1 && nconn2 > 1) connection[idx1*aNumStructures+idx2] = 0;
}
}
// If requested remove nodes with less than K shared nearest neighbors
if(mK > 0)
{
for(idx1=0; idx1 < aNumStructures-1; ++idx1)
{
for(idx2=idx1+1; idx2 < aNumStructures; ++idx2)
{
// Do nothing if nodes have none or more than one SNN
if(connection[idx1*aNumStructures+idx2] == 0) continue;
if(connection[idx1*aNumStructures+idx2] < (1+mK)) connection[idx1*aNumStructures+idx2] = 0;
}
}
}
// Make the matrix symmetrical again
for(idx1=0; idx1 < aNumStructures-1; ++idx1)
{
for(idx2=idx1+1; idx2 < aNumStructures; ++idx2)
{
connection[idx2*aNumStructures+idx1] = connection[idx1*aNumStructures+idx2];
}
}
// Array to keep track of nodes already assigned to a group
bool *assigned = new bool[aNumStructures];
for(j=0; j < aNumStructures; ++j) assigned[j] = false;
// For each node do a DFS to extract the nodes
for(idx1=0; idx1 < aNumStructures; ++idx1)
{
// Skip if already assigned
if(assigned[idx1]) continue;
// Start a new group
std::set<unsigned int> s;
// Find the connected component
doDepthFirstVisit(idx1, assigned, s, connection, aNumStructures);
// Insert the new group
aResult.push_back(s);
}
// Release and return
delete [] assigned;
delete [] connection;
}
float HierarchicalGrouping::ClusterDistanceSingleLinkage(std::list<HierarchicalGrouping::Node>::iterator& aNi,
std::list<HierarchicalGrouping::Node>::iterator& aNj,
const DistanceMatrix& aDistances) const
{
size_t leni = aNi->idx.size();
size_t lenj = aNj->idx.size();
if(leni == 1 && lenj == 1)
{
return aDistances(aNi->idx[0], aNj->idx[0]);
}
else
{
float dist = FLT_MAX;
for(size_t i=0; i < leni; ++i)
{
for(size_t j=0; j < lenj; ++j)
{
float d = aDistances(aNi->idx[i], aNj->idx[j]);
if(d < dist) dist = d;
}
}
return dist;
}
}
float HierarchicalGrouping::ClusterDistanceCompleteLinkage(std::list<HierarchicalGrouping::Node>::iterator& aNi,
std::list<HierarchicalGrouping::Node>::iterator& aNj,
const DistanceMatrix& aDistances) const
{
size_t leni = aNi->idx.size();
size_t lenj = aNj->idx.size();
if(leni == 1 && lenj == 1)
{
return aDistances(aNi->idx[0], aNj->idx[0]);
}
else
{
float dist = -FLT_MAX;
for(size_t i=0; i < leni; ++i)
{
for(size_t j=0; j < lenj; ++j)
{
float d = aDistances(aNi->idx[i], aNj->idx[j]);
if(d > dist) dist = d;
}
}
return dist;
}
}
void HierarchicalGrouping::doGrouping(size_t aNumStructures, const DistanceMatrix& aDistances, std::vector< std::set<unsigned int> >& aResult)
{
std::list<Node>::const_iterator n;
// Initialize root (to point to all)
std::list<Node> root;
for(unsigned int i=0; i < aNumStructures; ++i) root.push_back(Node(i));
#ifdef TRACE_GROUPING
float curr_min = 0.0F;
#endif
// Iterate till the distance becomes greather than the given threshold
for(; root.size() > 1;)
{
#ifdef TRACE_GROUPING
// Corresponding threshold value
std::cerr << std::fixed << std::setprecision(4) << curr_min << " ";
// Debug: output initial grouping
for(n=root.begin(); n != root.end(); ++n)
{
std::cerr << "(" ;
std::vector<unsigned int>::const_iterator ii;
for(ii=n->idx.begin(); ii != n->idx.end(); ++ii)
{
if(ii != n->idx.begin()) std::cerr << " " ;
std::cerr << *ii;
}
std::cerr << ")" ;
}
std::cerr << std::endl;
#endif
// Find minimum dist and minimum pairs
std::list<Node>::iterator ni;
std::list<Node>::iterator nj;
float min_dist = FLT_MAX;
std::list<Node>::iterator min_i;
std::list<Node>::iterator min_j;
for(ni=root.begin(); ni != root.end(); ++ni)
{
for(nj=ni, ++nj; nj != root.end(); ++nj)
{
float dist = (this->*ClusterDistance)(ni, nj, aDistances);
if(dist < min_dist)
{
min_dist = dist;
min_i = ni;
min_j = nj;
}
}
}
// Exit if the threshold has been reached
if(min_dist > mMaxDistanceForGrouping) break;
#ifdef TRACE_GROUPING
// Update min distance
curr_min = min_dist;
#endif
// Update the group list. Merge node j at the end of node i
min_i->Merge(min_j);
// Remove merged node
root.erase(min_j);
}
#ifdef TRACE_GROUPING
// If the last step is a single cluster, display it
if(root.size() == 1)
{
// Corresponding threshold value
std::cerr << std::fixed << std::setprecision(4) << curr_min << " ";
// Debug: output initial grouping
for(n=root.begin(); n != root.end(); ++n)
{
std::cerr << "(" ;
std::vector<unsigned int>::const_iterator ii;
for(ii=n->idx.begin(); ii != n->idx.end(); ++ii)
{
if(ii != n->idx.begin()) std::cerr << " " ;
std::cerr << *ii;
}
std::cerr << ")" ;
}
std::cerr << std::endl;
}
#endif
// Load the configuration in the final structure
for(n=root.begin(); n != root.end(); ++n)
{
std::vector<unsigned int>::const_iterator ii;
std::set<unsigned int> s;
for(ii=n->idx.begin(); ii != n->idx.end(); ++ii) s.insert(*ii) ;
aResult.push_back(s);
}
}