-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathcommunities.cpp
More file actions
465 lines (357 loc) · 12.1 KB
/
communities.cpp
File metadata and controls
465 lines (357 loc) · 12.1 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include "communities.h"
using namespace std;
Community::~Community() {
if (set == NULL) delete set;
}
Communities::Communities(igraph_t *graph) {
// salviamo il puntatore al grafo
this->graph = graph;
// Vogliamo copiare i gradi in un array
unsigned int vc = igraph_vcount(graph);
igraph_vector_t degree;
igraph_vector_init(°ree, vc);
// per quanto riguarda igraph_vss_1, la guida di igraph
// dice che è immediato, e può essere usato senza preoccuparsi di distruggerlo
igraph_degree(graph, °ree, igraph_vss_all(), IGRAPH_ALL, false);
// creiamo l'array e lo popoliamo
degrees = new unsigned int[vc];
for (unsigned int i = 0; i < vc; i++) degrees[i] = VECTOR(degree)[i];
igraph_vector_destroy(°ree);
// questa è una costante ricorrente
factor = 0.5 / ((double) igraph_ecount(graph));
}
Communities::~Communities() {
delete[] degrees;
delete[] membership;
// non possiamo fare il delete di altre cose, perché parte sono condivise.
// si usa sync quando si vuole fare pulizia tra una copia e l'altra,
// si usa free quando si vuole fare pulizia prima di cancellare TUTTE le
// istanze
}
Communities::Communities(const Communities &c) {
this->graph = c.graph;
this->communities = c.communities;
this->counter = c.counter;
this->keys = c.keys;
this->factor = c.factor;
#ifdef EQ_HARD
this->mmap = c.mmap;
#endif
unsigned int vs = igraph_vcount(graph);
this->degrees = new unsigned int[vs];
this->membership = new unsigned int[vs];
for (unsigned int i = 0; i < vs; i++) {
degrees[i] = c.degrees[i];
membership[i] = c.membership[i];
}
}
void Communities::free() {
BOOST_FOREACH(CID c, keys) {
delete communities[c];
}
}
void Communities::sync(Communities &b) {
set<CID> toDelete;
std::set_difference(keys.begin(), keys.end(), b.keys.begin(), b.keys.end(),
std::inserter(toDelete, toDelete.begin()));
BOOST_FOREACH(CID c, toDelete) {
delete communities[c];
}
}
bool Communities::has(CID i) {
return keys.find(i) != keys.end();
}
////////////////////////////////////////////////////////////////////////////////
// utilità
////////////////////////////////////////////////////////////////////////////////
// FIXME: inserire il controllo degli errori
unsigned int Communities::getCommunityInternalEdges(unsigned int s) {
int err;
igraph_vector_t *v = communities[s]->toVector();
igraph_vs_t *vs = communities[s]->toVertexSelector(v);
//creazione sottografo dal vertex_selector;
igraph_t subgraph;
err = igraph_subgraph(graph, &subgraph, *vs);
// quello che ci interessa realmente
unsigned int e = igraph_ecount(&subgraph);
// queste funzioni non restituiscono niente
igraph_vs_destroy(vs);
igraph_vector_destroy(v);
delete vs;
delete v;
igraph_destroy(&subgraph);
return e;
}
// FIXME: inserire il controllo degli errori
unsigned int Communities::getCommunityBoundaryEdges(unsigned int s) {
int err;
unsigned int o = 0;
igraph_vector_t *v = communities[s]->toVector();
igraph_vs_t *vs = communities[s]->toVertexSelector(v);
igraph_vit_t vit;
igraph_vit_create(graph, *vs, &vit);
unsigned int cSize = communities[s]->set->size();
igraph_vector_ptr_t p;
err = igraph_vector_ptr_init(&p, cSize);
err = igraph_neighborhood(graph, &p, *vs, 1, IGRAPH_ALL, 1);
// la componente i-esima di p contiene l'elenco di vicini di ordine 1
// dell'i-esimo nodo della community (quindi anche sé stesso)
//conteggio edge che escono dalla community
for (unsigned int i = 0; i < cSize; i++) //per ogni vertice della community
{
igraph_vector_t *vv = (igraph_vector_t *) igraph_vector_ptr_e(&p, i);
// per ogni vertice vicino di i
for (int l = 0; l < igraph_vector_size(vv); l++) {
//se non appartiene alla community, incrementa o
if (!ownership(VECTOR(*vv)[l], s))
o++;
}
}
igraph_vit_destroy(&vit);
igraph_vs_destroy(vs);
igraph_vector_destroy(v);
delete vs;
delete v;
igraph_vector_ptr_destroy(&p);
return o;
}
void Communities::load(std::istream &s) {
// TODO: pulire la struttura dati prima di fare il load
unsigned int cSize;
s >> cSize;
for (unsigned int i = 0; i < cSize; i++) {
unsigned int iSize;
s >> iSize;
NodeSet *nsp = new NodeSet;
Node aux;
for (unsigned int j = 0; j < iSize; j++) {
s >> aux;
// calcola membership automaticamente
nsp->insert(aux);
}
addCommunity(nsp);
}
}
void Communities::dump(std::ostream &s) {
s << size() << endl;
BOOST_FOREACH(unsigned int i, keys) {
// K n1 n2 .. nK\n
unsigned int size = getCommunitySize(i);
// commentato perché voglio che usi gli id anziché label..
/*
igraph_vector_t* v = communities[i]->toVector();
igraph_vs_t* vs = communities[i]->toVertexSelector(v);
igraph_strvector_t result;
igraph_strvector_init(&result, size);
igraph_cattribute_VASV(graph, "label", *vs, &result);
*/
s << size;
BOOST_FOREACH(Node n, *communities[i]->set) s << " " << n;
s << endl;
/*
igraph_strvector_destroy(&result);
igraph_vs_destroy(vs);
igraph_vector_destroy(v);
delete vs;
delete v;*/
}
}
////////////////////////////////////////////////////////////////////////////////
// Metodi di supporto a GCE e EAGLE
////////////////////////////////////////////////////////////////////////////////
// ho rimosso il parametro graph, tanto gli viene passato al costruttore
int Communities::setCommunities(string fileName) {
counter = 0;
ifstream f1(fileName.c_str());
if (!f1) {
return -1;
}
communities.clear();
string s;
string sub;
while (f1.good()) {
//legge tutta la riga dal file e la mette nella variabile s
getline(f1, s);
if (s.length() == 0)
continue;
NodeSet *ns = new NodeSet;
//unsigned int vs = igraph_vcount(graph);
//Estrapolazione interi dalla stringa s
istringstream iss(s);
while (iss.good()) {
unsigned int node;
iss >> node;
//node--;
//cout << node << " < " << vs << " == " << (node < vs) << endl;
//assert(node < vs);
ns->insert(node);
}
//cout << "Inserisco tutto e speriamo" << endl;
addCommunity(ns);
}
// ricordarsi di chiamare
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Metodi di supporto ad EAGLE
////////////////////////////////////////////////////////////////////////////////
CID Communities::registerCommunity(CommunityPtr c) {
CID r = counter++;
communities[r] = c;
keys.insert(r);
return r;
}
CID Communities::addCommunity(NodeSet *s) {
CommunityPtr c = Community::spawn();
c->set = s;
CID r = registerCommunity(c);
BOOST_FOREACH(Node n, *c->set) {
membership[n]++;
#ifdef EQ_HARD
mmap[n].insert(r);
#endif
}
c->communitySize = getCommunitySize(r);
c->communityInternalEdges = getCommunityInternalEdges(r);
c->communityBoundaryEdges = getCommunityBoundaryEdges(r);
return r;
}
CID Communities::addCommunity(igraph_vector_t *vector) {
// Creiamo una nuova community
CommunityPtr c = Community::spawn();
c->set = new NodeSet;
CID r = registerCommunity(c);
// popoliamola
int s = igraph_vector_size(vector);
for (int j = 0; j < s; j++) {
int node = VECTOR(*vector)[j];
membership[node]++;
c->set->insert(node);
#ifdef EQ_HARD
mmap[node].insert(r);
#endif
}
return r;
}
CID Communities::addCommunity(unsigned int i) {
// Creiamo una nuova community
CommunityPtr c = Community::spawn();
c->set = new NodeSet;
// inseriamo la community nella lista
CID r = registerCommunity(c);
// Popoliamola
membership[i]++;
c->set->insert(i);
#ifdef EQ_HARD
mmap[i].insert(r);
#endif
return r;
}
void Communities::prepareForEAGLE() {
unsigned int s = igraph_vcount(graph);
communities.clear();
keys.clear();
#ifdef EQ_HARD
mmap.clear();
#endif
membership = new unsigned int[s];
for (unsigned int i = 0; i < s; i++) membership[i] = 0;
counter = 0;
}
void Communities::precalculateData() {
BOOST_FOREACH(CommunitiesMap::value_type
cp, communities)cp.second->update(graph, degrees, membership);
}
unsigned int Communities::merge(unsigned int a, unsigned int b, bool hasComms) {
// m è la community unione
CommunityPtr m = get(a)->unite(get(b));
CID r = registerCommunity(m);
// abbiamo bisogno della community intersezione per poter aggiustare il
// vettore membership
CommunityPtr is = get(a)->intersect(get(b));
// questo fa cambiare membership, ed è necessario fare l'update di tutte le
// communities che hanno questi nodi
BOOST_FOREACH(Node n, *is->set)membership[n]--;
delete is;
#ifdef EQ_HARD
BOOST_FOREACH(Node n, *m->set) {
mmap[n].erase(a);
mmap[n].erase(b);
mmap[n].insert(r);
}
#endif
/*
if (!hasComms)
{
delete get(a);
delete get(b);
}
*/
communities.erase(a);
keys.erase(a);
communities.erase(b);
keys.erase(b);
#ifdef EQ_HARD
// qui mettiamo i CID delle communities da aggiornare
set<CID> toUpdate;
// popliamo update, prendendo tutte le communities che hanno nodi la cui
// membership è cambiata (e questi per forza sono quelli dell'intersezione)
BOOST_FOREACH(Node n, *m->set)
BOOST_FOREACH(CID cid, mmap[n])toUpdate.insert(cid);
int upSize = toUpdate.size();
CommunityPtr *temp = new CommunityPtr[upSize];
int upc = 0;
// a questo punto chiamiamo l'update solo su queste communities
BOOST_FOREACH(CID cid, toUpdate)temp[upc++] = get(cid);
#pragma omp parallel for if (upSize > 10) default(shared)
for (int iterator = 0; iterator < upSize; iterator++)
temp[iterator]->update(graph, degrees, membership);
#else
m->update(graph, degrees, membership);
#endif
return r;
}
////////////////////////////////////////////////////////////////////////////////
// grim, forgotten and frostbitten
////////////////////////////////////////////////////////////////////////////////
std::string Communities::summary(bool verbose, bool debug) {
ostringstream s;
s << "Dimensione: " << size() << endl;
if (verbose) {
s << "Elenco communities:" << endl;
BOOST_FOREACH(CID c, keys) s << summary(c, debug) << endl;
}
return s.str();
}
std::string Communities::summary(CID i, bool debug) {
ostringstream s;
if (debug) s << i << " (" << communities[i] << "):";
else s << i << ":";
// procuriamoci il vertex selector
igraph_vector_t *v = communities[i]->toVector();
igraph_vs_t *vs = communities[i]->toVertexSelector(v);
igraph_strvector_t result;
unsigned int size = getCommunitySize(i);
igraph_strvector_init(&result, size);
igraph_cattribute_VASV(graph, "label", *vs, &result);
for (unsigned int j = 0; j < size; j++)
s << " " << STR(result, j);
if (debug) {
s << " - " << communities[i]->comdegree;
#ifdef EQ_HARD
s << ", " << communities[i]->cf << ", " << communities[i]->dm;
#endif
}
igraph_strvector_destroy(&result);
igraph_vs_destroy(vs);
igraph_vector_destroy(v);
delete vs;
delete v;
return s.str();
}