-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity.cpp
executable file
·286 lines (229 loc) · 7.63 KB
/
community.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
// File: community.h
// -- community detection source file
//-----------------------------------------------------------------------------
// Community detection
// Based on the article "Fast unfolding of community hierarchies in large networks"
// Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre
//
// This program must not be distributed without agreement of the above mentionned authors.
//-----------------------------------------------------------------------------
// Author : E. Lefebvre, adapted by J.-L. Guillaume
// Email : [email protected]
// Location : Paris, France
// Time : February 2008
//-----------------------------------------------------------------------------
// see readme.txt for more details
#include "community.h"
using namespace std;
Community::Community(char * filename, int type, int nbp, double minm) {
g = Graph(filename, type);
size = g.nb_nodes;
n2c.resize(size);
in.resize(size);
tot.resize(size);
for (int i=0 ; i<size ; i++) {
n2c[i] = i;
in[i] = g.nb_selfloops(i);
tot[i] = g.weighted_degree(i);
}
nb_pass = nbp;
min_modularity = minm;
}
Community::Community(Graph gc, int nbp, double minm) {
g = gc;
size = g.nb_nodes;
n2c.resize(size);
in.resize(size);
tot.resize(size);
for (int i=0 ; i<size ; i++) {
n2c[i] = i;
in[i] = g.nb_selfloops(i);
tot[i] = g.weighted_degree(i);
}
nb_pass = nbp;
min_modularity = minm;
}
void
Community::display() {
cerr << endl << "<" ;
for (int i=0 ; i<size ; i++)
cerr << " " << i << "/" << n2c[i] << "/" << in[i] << "/" << tot[i] ;
cerr << ">" << endl;
}
double
Community::modularity() {
double q = 0.;
double m2 = (double)g.total_weight;
for (int i=0 ; i<size ; i++) {
if (tot[i]>0)
q += (double)in[i]/m2 - ((double)tot[i]/m2)*((double)tot[i]/m2);
}
return q;
}
map<int,int>
Community::neigh_comm(int node) {
map<int,int> res;
pair<int *,int *> p = g.neighbors(node);
int deg = g.nb_neighbors(node);
res.insert(make_pair(n2c[node],0));
for (int i=0 ; i<deg ; i++) {
int neigh = *(p.first+i);
int neigh_comm = n2c[neigh];
int neigh_weight = (g.weights==NULL)?1:*(p.second+i);
if (neigh!=node) {
map<int,int>::iterator it = res.find(neigh_comm);
if (it!=res.end())
it->second+=neigh_weight;
else
res.insert(make_pair(neigh_comm,neigh_weight));
}
}
return res;
}
void
Community::partition2graph() {
vector<int> renumber(size, -1);
for (int node=0 ; node<size ; node++) {
renumber[n2c[node]]++;
}
int final=0;
for (int i=0 ; i<size ; i++)
if (renumber[i]!=-1)
renumber[i]=final++;
for (int i=0 ; i<size ; i++) {
pair<int *,int *> p = g.neighbors(i);
int deg = g.nb_neighbors(i);
for (int j=0 ; j<deg ; j++) {
int neigh = *(p.first+j);
cout << renumber[n2c[i]] << " " << renumber[n2c[neigh]] << endl;
}
}
}
void
Community::display_partition() {
vector<int> renumber(size, -1);
for (int node=0 ; node<size ; node++) {
renumber[n2c[node]]++;
}
int final=0;
for (int i=0 ; i<size ; i++)
if (renumber[i]!=-1)
renumber[i]=final++;
for (int i=0 ; i<size ; i++)
cout << i << " " << renumber[n2c[i]] << endl;
}
// This function has to be revisited
// malloc is dirty
Graph
Community::partition2graph_binary() {
vector<int> renumber(size, -1);
for (int node=0 ; node<size ; node++) {
renumber[n2c[node]]++;
}
int final=0;
for (int i=0 ; i<size ; i++)
if (renumber[i]!=-1)
renumber[i]=final++;
// Compute communities
vector<vector<int> > comm_nodes(final);
for (int node=0 ; node<size ; node++) {
comm_nodes[renumber[n2c[node]]].push_back(node);
}
// unweigthed to weighted
Graph g2;
g2.nb_nodes = comm_nodes.size();
g2.degrees = (int *)malloc(comm_nodes.size()*4);
g2.links = (int *)malloc((long)10000000*8);
g2.weights = (int *)malloc((long)10000000*8);
long where = 0;
int comm_deg = comm_nodes.size();
for (int comm=0 ; comm<comm_deg ; comm++) {
map<int,int> m;
map<int,int>::iterator it;
int comm_size = comm_nodes[comm].size();
for (int node=0 ; node<comm_size ; node++) {
pair<int *,int *> p = g.neighbors(comm_nodes[comm][node]);
int deg = g.nb_neighbors(comm_nodes[comm][node]);
for (int i=0 ; i<deg ; i++) {
int neigh = *(p.first+i);
int neigh_comm = renumber[n2c[neigh]];
int neigh_weight = (g.weights==NULL)?1:*(p.second+i);
it = m.find(neigh_comm);
if (it==m.end())
m.insert(make_pair(neigh_comm, neigh_weight));
else
it->second+=neigh_weight;
}
}
g2.degrees[comm]=(comm==0)?m.size():g2.degrees[comm-1]+m.size();
g2.nb_links+=m.size();
for (it = m.begin() ; it!=m.end() ; it++) {
g2.total_weight += it->second;
g2.links[where] = it->first;
g2.weights[where] = it->second;
where++;
}
}
realloc(g.links, (long)g2.nb_links*4);
realloc(g.weights, (long)g2.nb_links*4);
return g2;
}
double
Community::one_level() {
bool improvement = false;
int nb_pass_done = 0;
double new_mod = modularity();
double cur_mod = new_mod;
// repeat while
// there is an improvement of modularity
// or there is an improvement of modularity greater than a given epsilon
// or a predefined number of pass have been done
vector<int> random_order(size);
for (int i=0 ; i<size ; i++) {
random_order[i]=i;
}
for (int i=0 ; i<size-1 ; i++) {
int rand_pos = rand()%(size-i)+i;
int tmp = random_order[i];
random_order[i] = random_order[rand_pos];
random_order[rand_pos] = tmp;
}
do {
cur_mod = new_mod;
improvement = false;
nb_pass_done++;
// for each node: remove the node from its community and insert it in the best community
for (int node_tmp=0 ; node_tmp<size ; node_tmp++) {
int node = node_tmp;
// int node = random_order[node_tmp];
// if (node%1000000==0) {fprintf(stderr,"%d ",node); fflush(stderr);}
int node_comm = n2c[node];
// computation of all neighboring communities of current node
map<int,int> ncomm = neigh_comm(node);
// remove node from its current community
remove(node, node_comm, ncomm.find(node_comm)->second);
// compute the nearest community for node
// default choice for future insertion is the former community
int best_comm = node_comm;
int best_nblinks = 0;//ncomm.find(node_comm)->second;
double best_increase = 0.;//modularity_gain(node, best_comm, best_nblinks);
for (map<int,int>::iterator it=ncomm.begin() ; it!=ncomm.end() ; it++) {
double increase = modularity_gain(node, it->first, it->second);
if (increase>best_increase) {
best_comm = it->first;
best_nblinks = it->second;
best_increase = increase;
}
}
// insert node in the nearest community
// cerr << "insert " << node << " in " << best_comm << " " << best_increase << endl;
insert(node, best_comm, best_nblinks);
if (best_comm!=node_comm)
improvement=true;
}
new_mod = modularity();
cerr << "pass number " << nb_pass_done << " of " << nb_pass
<< " : " << new_mod << " " << cur_mod << endl;
} while (improvement && new_mod-cur_mod>min_modularity && nb_pass_done!=nb_pass);
return new_mod;
}