-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunity_kullback_liebler.c
More file actions
293 lines (255 loc) · 7.41 KB
/
community_kullback_liebler.c
File metadata and controls
293 lines (255 loc) · 7.41 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "defs.h"
#include "globals.h"
#ifdef __MTA__
#include <sys/mta_task.h>
#include <machine/runtime.h>
#else
#include "compat/xmt-ops.h"
#endif
#include <math.h>
#include <sys/mman.h>
#if !defined(NAN)
#define NAN (0.0/0.0)
#endif
#ifdef __MTA__
double Log2(double n)
{
return log(n) / log(2);
}
#endif
double
get_community_kl_dir(const graph *G, const int64_t *membership,
int64_t num_components)
{
int64_t u;
int64_t n, m;
double m_inv;
double kldiv;
int64_t *Lss, *Lsplus, *Lpluss;
n = G->numVertices;
m = G->numEdges;
m_inv = 1.0/(G->numEdges);
Lss = xcalloc (3*num_components, sizeof (*Lsplus));
Lsplus = &Lss[num_components];
Lpluss = &Lsplus[num_components];
OMP("omp parallel for")
for(u=0; u<n; u++) {
const int64_t cid = membership[u];
const int64_t u_edge_start = G->edgeStart[u];
const int64_t u_edge_end = G->edgeStart[u+1];
int64_t lss = 0, lsplus = 0;
if (cid >= 0) {
int64_t j;
for (j=u_edge_start; j<u_edge_end; j++) {
const int64_t v = G->endVertex[j];
const int64_t vcid = membership[v];
if (vcid == cid)
++lss;
else {
++lsplus;
if (vcid >= 0)
OMP("omp atomic") ++Lpluss[vcid];
}
}
OMP("omp atomic") Lss[cid] += lss;
OMP("omp atomic") Lsplus[cid] += lsplus;
}
}
kldiv = 0.0;
{
int64_t j;
for (j = 0; j < num_components; j++) {
double p, q;
p = Lss[j] * m_inv;
q = Lsplus[j] * m_inv;
q *= q;
if (p != q)
#ifdef __MTA__
kldiv += p * Log2 (p / q);
#else
kldiv += p * log2 (p / q);
#endif
}
}
free(Lss);
return kldiv;
}
double
get_community_kl_undir(const graph *G, const int64_t *membership,
int64_t num_components)
{
int64_t u;
int64_t n, m;
double m_inv;
double kldiv;
int64_t *Lss, *Lsplus;
n = G->numVertices;
m = G->numEdges;
m_inv = 1.0/(G->numEdges);
Lss = xcalloc (2*num_components, sizeof (*Lsplus));
Lsplus = &Lss[num_components];
OMP("omp parallel for")
for(u=0; u<n; u++) {
const int64_t cid = membership[u];
const int64_t u_edge_start = G->edgeStart[u];
const int64_t u_edge_end = G->edgeStart[u+1];
int64_t lss = 0, lsplus = 0;
if (cid >= 0) {
int64_t j;
for (j=u_edge_start; j<u_edge_end; j++) {
const int64_t v = G->endVertex[j];
const int64_t vcid = membership[v];
/* Examine only the upper triangle and diagonal. */
if (v < u) continue;
if (vcid == cid)
++lss;
else
++lsplus;
}
OMP("omp atomic") Lss[cid] += lss;
OMP("omp atomic") Lsplus[cid] += lsplus;
}
}
kldiv = 0.0;
{
int64_t j;
for (j = 0; j < num_components; j++) {
double p, q;
p = Lss[j] * m_inv;
q = 0.5 * Lsplus[j] * m_inv;
q *= q;
if (p != q)
#ifdef __MTA__
kldiv += p * Log2 (p / q);
#else
kldiv += p * log2 (p / q);
#endif
}
}
free(Lss);
return kldiv;
}
/** Compute the Kullback-Liebler divergence of the components induced
by the membership array relative to a uniform base model. The
component is induced by the decomposition in the membership array.
The mechanics of the modularity computations upon which these models
are based are described in McCloskey and Bader, "Modularity and
Graph Algorithms", presented at UMBC Sept. 2009.
@param g A graph_t, either directed or undirected. The computations
differ for directed v. undirected graphs.
@param membership An array mapping each vertex in g to its component.
@param num_components The number of components, at least as large as
the largest number in membership.
@return The Kullback-Liebler divergence from a uniform base model,
or NaN if the arguments are invalid.
*/
double
get_community_kl(const graph *G, const int64_t *membership,
int64_t num_components)
{
if (!G || !membership || num_components <= 0) return NAN;
if (!G->numVertices || !G->numEdges) return 0.0;
if (G->undirected)
return get_community_kl_undir (G, membership, num_components);
else
return get_community_kl_dir (G, membership, num_components);
}
double
get_single_community_kl_undir(const graph *G, const int64_t *membership,
int64_t which_component)
{
int64_t n, m;
double kldiv, p, q;
int64_t Ls = 0, Vols = 0, Xs, u;
n = G->numVertices;
m = G->numEdges/2;
OMP("omp parallel for reduction(+:Ls) reduction(+:Vols)")
for(u=0; u<n; u++) {
const int64_t u_edge_start = G->edgeStart[u];
const int64_t u_edge_end = G->edgeStart[u+1];
const int64_t u_degree = u_edge_end - u_edge_start;
int64_t j;
if (membership[u] == which_component) {
for(j=u_edge_start; j<u_edge_end; j++) {
const int64_t v = G->endVertex[j];
if (membership[v] == which_component)
++Ls; /* double-counted */
}
Vols += u_degree;
}
}
assert (!(Ls % 2));
Ls /= 2;
Xs = Vols - Ls;
p = Ls/(double)m;
q = 0.5 * Xs / (double)m;
q *= q;
#ifdef __MTA__
kldiv = p * Log2 (p / q);
#else
kldiv = p * log2 (p / q);
#endif
return kldiv;
}
double
get_single_community_kl_dir(const graph *G, const int64_t *membership,
int64_t which_component)
{
int64_t n, m;
double kldiv, p, q;
int64_t Ls = 0, Vols = 0, Xs, u;
n = G->numVertices;
m = G->numEdges;
OMP("omp parallel for reduction(+:Ls) reduction(+:Vols)")
for(u=0; u<n; u++) {
const int64_t u_edge_start = G->edgeStart[u];
const int64_t u_edge_end = G->edgeStart[u+1];
const int64_t u_degree = u_edge_end - u_edge_start;
int64_t j;
if (membership[u] == which_component) {
for(j=u_edge_start; j<u_edge_end; j++) {
const int64_t v = G->endVertex[j];
if (membership[v] == which_component)
++Ls; /* double-counted */
}
Vols += u_degree;
}
}
assert (!(Ls % 2));
Ls /= 2;
Xs = Vols - Ls;
p = Ls / (double)m;
q = Xs / (double)m;
q *= q;
kldiv = p * log (p / q);
return kldiv;
}
/** Compute the Kullback-Liebler divergence of a single induced
component relative to a uniform base model. The component is
induced by the decomposition in the membership array. The
get_community_kl() routine is more efficient than computing each KL
contribution separately.
The mechanics of the modularity computations upon which these models
are based are described in McCloskey and Bader, "Modularity and
Graph Algorithms", presented at UMBC Sept. 2009.
@param g A graph_t, either directed or undirected. The computations
differ for directed v. undirected graphs.
@param membership An array mapping each vertex in g to its component.
@param which_component The component of interest.
@return The Kullback-Liebler divergence from a uniform base model,
or NaN if the arguments are invalid.
*/
double
get_single_community_kl(const graph *G, const int64_t *membership,
int64_t which_component)
{
if (!G || !membership) return NAN;
if (G->undirected)
return get_single_community_kl_undir (G, membership, which_component);
else
return get_single_community_kl_dir (G, membership, which_component);
}