-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
404 lines (365 loc) · 11 KB
/
Test.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "Test.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <tuple>
#include <cmath>
#include <set>
struct Table {
public:
VI tags;
int round;
Table(int N) {
tags = VI(N, -1);
round = 0;
}
void nextRound() {
round++;
}
void set(int x) {
if (x > tags.size()) {
throw out_of_range("When setting table");
return;
}
tags[x] = round;
}
bool get(int x) {
if (x > tags.size()) {
throw out_of_range("When getting table");
return false;
}
return tags[x] == round;
}
private:
Table() {}
};
struct IntTable {
public:
VI tags, values;
int round;
IntTable(int N) {
tags = VI(N, -1);
values = VI(N, 0);
round = 0;
}
void nextRound() {
round++;
}
void add(int x) {
if (x > tags.size()) {
throw out_of_range("When setting table");
return;
}
if (tags[x] != round) {
tags[x] = round;
values[x] = 0;
}
values[x]++;
}
int get(int x) {
if (x > tags.size()) {
throw out_of_range("When getting table");
return false;
}
return (tags[x] != round) ? 0 : values[x];
}
private:
IntTable() {}
};
void Test::toOldId(CommunitySet& commSet, const Graph* g) {
for (auto& comm : commSet) {
for (auto& id : comm) {
id = g->id_new_to_old_[id];
}
}
}
VI Test::getOldToNew(const Graph* g) {
int oldSize = *std::max_element(std::begin(g->id_new_to_old_), std::end(g->id_new_to_old_));
VI oldToNew(oldSize + 1, -1);
for (int i = 0; i < g->id_new_to_old_.size(); i++) {
oldToNew[g->id_new_to_old_[i]] = i;
}
return oldToNew;
}
void Test::sortCommunitySet(CommunitySet& commSet) {
for (Community& comm : commSet) {
std::sort(std::begin(comm), std::end(comm));
}
}
CommunitySet Test::readCommunity(const std::string commFile) {
std::ifstream fin;
fin.open(commFile);
if (!fin) {
return {};
}
CommunitySet commSet;
while (fin) {
std::string buff;
std::getline(fin, buff);
std::istringstream sin(buff);
Community comm;
int id;
while (sin >> id) {
comm.push_back(id);
}
if (!comm.empty()) {
commSet.push_back(comm);
}
}
return commSet;
}
void Test::writeCommunity(const std::string commFile,
const CommunitySet& commSet) {
std::ofstream fout;
fout.open(commFile);
for (auto& comm : commSet) {
if (comm.empty()) continue;
fout << comm[0];
for (int i = 1; i < comm.size(); i++) {
fout << '\t' << comm[i];
}
fout << std::endl;
}
fout.close();
}
int getMax(const CommunitySet& commSet, const CommunitySet &groundTruth) {
auto getMaxSub = [](const CommunitySet &commSet) {
int ans = 0;
for (auto &comm : commSet)
for (auto &id : comm)
ans = std::max(ans, id);
return ans;
};
return std::max(getMaxSub(commSet), getMaxSub(groundTruth)) + 1;
}
double Test::computeFsame(const CommunitySet& commSet,
const CommunitySet& groundTruth) {
auto process = [](const CommunitySet& commSet, const CommunitySet& groundTruth) {
int graphSize = getMax(commSet, groundTruth);
LL ans = 0;
LL sum = 0;
VVI bk(graphSize, VI());
IntTable cnt(graphSize);
for (int i = 0; i < groundTruth.size(); i++)
for (auto &id: groundTruth[i])
bk[id].push_back(i);
for (int i = 0; i < commSet.size(); i++) {
sum += commSet[i].size();
int tmp = 0;
for (auto &id: commSet[i]) {
for (auto &opid: bk[id]) {
cnt.add(opid);
tmp = std::max(tmp, cnt.get(opid));
}
}
ans += tmp;
cnt.nextRound();
}
return double(ans) / sum;
};
return (process(commSet, groundTruth) + process(groundTruth, commSet)) / 2.;
}
double Test::computeJaccard(const CommunitySet& commSet,
const CommunitySet& groundTruth) {
int graphSize = getMax(commSet, groundTruth);
VVI bkComm(graphSize, VI()), bkGrou(graphSize, VI());
auto buildBk = [](const CommunitySet &commSet, VVI &bk) {
for (int i = 0; i < commSet.size(); i++) {
auto &comm = commSet[i];
for (auto &id : comm)
bk[id].push_back(i);
}
};
buildBk(commSet, bkComm);
buildBk(groundTruth, bkGrou);
Table visComm(graphSize), visGrou(graphSize);
LL PS = 0, PS1 = 0, PS2 = 0;
for (int i = 0; i < graphSize; i++) {
if (bkComm[i].empty() || bkGrou[i].empty()) continue;
int lcnt = 0, rcnt = 0, overlap = 0;
for (auto &commId : bkComm[i]) {
for (auto &id : commSet[commId]) {
if (id == i) continue;
if (visComm.get(id)) continue;
visComm.set(id);
lcnt++;
}
}
for (auto &commId : bkGrou[i]) {
for (auto &id : groundTruth[commId]) {
if (id == i) continue;
if (visGrou.get(id)) continue;
visGrou.set(id);
rcnt++;
if (visComm.get(id))
overlap++;
}
}
PS += overlap;
PS1 += lcnt - overlap;
PS2 += rcnt - overlap;
visComm.nextRound(), visGrou.nextRound();
}
return double(PS) / (PS + PS1 + PS2);
}
double Test::computeNMI(const CommunitySet& commSet,
const CommunitySet& groundTruth) {
int graphSize = getMax(commSet, groundTruth);
std::vector<LL> rowSum(commSet.size(), 0), colSum(groundTruth.size(), 0);
LL sum = 0;
VVI bk(graphSize, VI());
IntTable cnt(graphSize);
for (int i = 0; i < groundTruth.size(); i++)
for (auto &id: groundTruth[i])
bk[id].push_back(i);
for (int i = 0; i < commSet.size(); i++) {
for (auto &id: commSet[i]) {
for (auto &opid: bk[id]) {
rowSum[i]++;
colSum[opid]++;
}
}
sum += rowSum[i];
}
double NT = std::log(sum);
std::vector<double> NI, NJ;
for (auto &v : rowSum) NI.push_back(v ? std::log(v) : 0);
for (auto &v : colSum) NJ.push_back(v ? std::log(v) : 0);
double up = 0.0;
for (int i = 0; i < commSet.size(); i++) {
std::set<int> opidSet;
for (auto &id: commSet[i]) {
for (auto &opid: bk[id]) {
cnt.add(opid);
opidSet.insert(opid);
}
}
for (auto &opid : opidSet) {
double NIJ = std::log(cnt.get(opid));
up += cnt.get(opid) * (NIJ + NT - NI[i] - NJ[opid]);
}
cnt.nextRound();
}
double down = 0;
for (int i = 0; i < NI.size(); i++) {
if (rowSum[i] == 0) continue;
down += rowSum[i] * (NI[i] - NT);
}
for (int i = 0; i < NJ.size(); i++) {
if (colSum[i] == 0) continue;
down += colSum[i] * (NJ[i] - NT);
}
return -2 * up / down;
}
double Test::computeClusterCoef(const CommunitySet& commSet, const Graph* g) {
VI oldToNew = Test::getOldToNew(g);
Table vis(g->vertices_num_);
Table edge(g->vertices_num_);
double ans = 0.0;
for (auto &comm : commSet) {
VI ids;
for (auto &id : comm) {
ids.push_back(oldToNew[id]);
vis.set(ids.back());
}
double sum = 0.;
for (auto &id : ids) {
int deg = g->vertices_[id]->degree;
int cnt = 0;
if (deg < 2) continue;
for (auto &nbr : g->vertices_[id]->nbr) {
if (!vis.get(nbr)) continue;
edge.set(nbr);
for (auto &cand : g->vertices_[nbr]->nbr) {
if (!vis.get(cand) || !edge.get(cand)) continue;
cnt++;
}
}
edge.nextRound();
sum += 2. * cnt / deg / (deg - 1);
}
ans += sum / ids.size();
vis.nextRound();
}
return ans / commSet.size();
}
std::tuple<VVI, VVI> computeInOutCommunity(const CommunitySet& commSet, const Graph* g) {
VI oldToNew = Test::getOldToNew(g);
Table vis(g->vertices_num_);
VVI in, out;
for (int i = 0; i < commSet.size(); i++) {
auto &comm = commSet[i];
for (auto& v: comm) {
vis.set(oldToNew[v]);
}
VI inSub, outSub;
for (auto& v: comm) {
int newV = oldToNew[v];
int vIn = 0, vOut = 0;
auto& allNbr = g->vertices_[newV]->nbr;
for (auto& nbr: allNbr) {
if (vis.get(nbr)) {
vIn++;
} else {
vOut++;
}
}
inSub.push_back(vIn);
outSub.push_back(vOut);
}
in.push_back(inSub);
out.push_back(outSub);
vis.nextRound();
}
return std::make_tuple(in, out);
}
double Test::computeStrength(const CommunitySet& commSet, const Graph* g) {
VVI in, out;
std::tie(in, out) = computeInOutCommunity(commSet, g);
int score = 0;
for (int i = 0; i < commSet.size(); i++) {
auto &comm = commSet[i];
int sumOut = 0, sumIn = 0;
bool strong = true;
for (int j = 0; j < comm.size(); j++) {
int vIn = in[i][j], vOut = out[i][j];
strong &= vIn > vOut;
sumOut += vOut, sumIn += vIn;
}
if (strong) {
score += 2;
} else if (sumIn > sumOut) {
score++;
}
}
return score / 2. / commSet.size();
}
double Test::computeModularity(const CommunitySet& commSet, const Graph* g) {
VVI in, out;
std::tie(in, out) = computeInOutCommunity(commSet, g);
double m = g->edges_num_;
double ans = 0.0;
for (int i = 0; i < commSet.size(); i++) {
auto &comm = commSet[i];
int sumOut = 0, sumIn = 0;
for (int j = 0; j < comm.size(); j++) {
sumOut += out[i][j], sumIn += in[i][j];
}
sumIn /= 2;
double tmp = (2. * sumIn + sumOut) / 2. / m;
ans += (sumIn / m - tmp * tmp);
}
return ans;
}
Test::TestReport Test::getTestReport(const CommunitySet& commSet,
const CommunitySet& groundTruth,
const Graph* g) {
Test::TestReport report;
report.Fsame = Test::computeFsame(commSet, groundTruth);
report.Jaccard = Test::computeJaccard(commSet, groundTruth);
report.NMI = Test::computeNMI(commSet, groundTruth);
report.ClusterCoef = Test::computeClusterCoef(commSet, g);
report.Strength = Test::computeStrength(commSet, g);
report.Modularity = Test::computeModularity(commSet, g);
return report;
}