Coverage Report

Created: 2026-12-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/igraph/src/community/walktrap/walktrap_communities.cpp
Line
Count
Source
1
/*
2
   igraph library.
3
   Copyright (C) 2007-2012  Gabor Csardi <csardi.gabor@gmail.com>
4
   334 Harvard street, Cambridge, MA 02139 USA
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
   02110-1301 USA
20
21
*/
22
23
/* The original version of this file was written by Pascal Pons
24
   The original copyright notice follows here. The FSF address was
25
   fixed by Tamas Nepusz */
26
27
// File: communities.cpp
28
//-----------------------------------------------------------------------------
29
// Walktrap v0.2 -- Finds community structure of networks using random walks
30
// Copyright (C) 2004-2005 Pascal Pons
31
//
32
// This program is free software; you can redistribute it and/or modify
33
// it under the terms of the GNU General Public License as published by
34
// the Free Software Foundation; either version 2 of the License, or
35
// (at your option) any later version.
36
//
37
// This program is distributed in the hope that it will be useful,
38
// but WITHOUT ANY WARRANTY; without even the implied warranty of
39
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
// GNU General Public License for more details.
41
//
42
// You should have received a copy of the GNU General Public License
43
// along with this program; if not, write to the Free Software
44
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
45
// 02110-1301 USA
46
//-----------------------------------------------------------------------------
47
// Author   : Pascal Pons
48
// Email    : pascal.pons@gmail.com
49
// Web page : http://www-rp.lip6.fr/~latapy/PP/walktrap.html
50
// Location : Paris, France
51
// Time     : June 2005
52
//-----------------------------------------------------------------------------
53
// see readme.txt for more details
54
55
#include "walktrap_communities.h"
56
#include "config.h" /* IGRAPH_THREAD_LOCAL */
57
#include <algorithm>
58
#include <cmath>
59
60
using namespace std;
61
62
namespace igraph {
63
64
namespace walktrap {
65
66
IGRAPH_THREAD_LOCAL int Probabilities::length = 0;
67
IGRAPH_THREAD_LOCAL Communities* Probabilities::C = nullptr;
68
IGRAPH_THREAD_LOCAL double* Probabilities::tmp_vector1 = nullptr;
69
IGRAPH_THREAD_LOCAL double* Probabilities::tmp_vector2 = nullptr;
70
IGRAPH_THREAD_LOCAL int* Probabilities::id = nullptr;
71
IGRAPH_THREAD_LOCAL int* Probabilities::vertices1 = nullptr;
72
IGRAPH_THREAD_LOCAL int* Probabilities::vertices2 = nullptr;
73
IGRAPH_THREAD_LOCAL int Probabilities::current_id = 0;
74
75
76
611k
Neighbor::Neighbor() {
77
611k
    next_community1 = nullptr;
78
611k
    previous_community1 = nullptr;
79
611k
    next_community2 = nullptr;
80
611k
    previous_community2 = nullptr;
81
611k
    heap_index = -1;
82
611k
}
83
84
188k
Probabilities::~Probabilities() {
85
188k
    delete[] P;
86
188k
    delete[] vertices;
87
188k
}
88
89
100k
Probabilities::Probabilities(int community) {
90
100k
    Graph* G = C->G;
91
100k
    int nb_vertices1 = 0;
92
100k
    int nb_vertices2 = 0;
93
94
100k
    double initial_proba = 1. / static_cast<double>(C->communities[community].size);
95
100k
    int last =  C->members[C->communities[community].last_member];
96
201k
    for (int m = C->communities[community].first_member; m != last; m = C->members[m]) {
97
100k
        tmp_vector1[m] = initial_proba;
98
100k
        vertices1[nb_vertices1++] = m;
99
100k
    }
100
101
403k
    for (int t = 0; t < length; t++) {
102
302k
        current_id++;
103
302k
        if (nb_vertices1 > (G->nb_vertices / 2)) {
104
19.5k
            nb_vertices2 = G->nb_vertices;
105
846k
            for (int i = 0; i < G->nb_vertices; i++) {
106
826k
                tmp_vector2[i] = 0.;
107
826k
            }
108
19.5k
            if (nb_vertices1 == G->nb_vertices) {
109
61.3k
                for (int i = 0; i < G->nb_vertices; i++) {
110
56.9k
                    double proba = tmp_vector1[i] / G->vertices[i].total_weight;
111
236k
                    for (int j = 0; j < G->vertices[i].degree; j++) {
112
179k
                        tmp_vector2[G->vertices[i].edges[j].neighbor] += proba * G->vertices[i].edges[j].weight;
113
179k
                    }
114
56.9k
                }
115
15.2k
            } else {
116
549k
                for (int i = 0; i < nb_vertices1; i++) {
117
534k
                    int v1 = vertices1[i];
118
534k
                    double proba = tmp_vector1[v1] / G->vertices[v1].total_weight;
119
2.98M
                    for (int j = 0; j < G->vertices[v1].degree; j++) {
120
2.45M
                        tmp_vector2[G->vertices[v1].edges[j].neighbor] += proba * G->vertices[v1].edges[j].weight;
121
2.45M
                    }
122
534k
                }
123
15.2k
            }
124
283k
        } else {
125
283k
            nb_vertices2 = 0;
126
1.53M
            for (int i = 0; i < nb_vertices1; i++) {
127
1.24M
                int v1 = vertices1[i];
128
1.24M
                double proba = tmp_vector1[v1] / G->vertices[v1].total_weight;
129
7.11M
                for (int j = 0; j < G->vertices[v1].degree; j++) {
130
5.86M
                    int v2 = G->vertices[v1].edges[j].neighbor;
131
5.86M
                    if (id[v2] == current_id) {
132
2.90M
                        tmp_vector2[v2] += proba * G->vertices[v1].edges[j].weight;
133
2.96M
                    } else {
134
2.96M
                        tmp_vector2[v2] = proba * G->vertices[v1].edges[j].weight;
135
2.96M
                        id[v2] = current_id;
136
2.96M
                        vertices2[nb_vertices2++] = v2;
137
2.96M
                    }
138
5.86M
                }
139
1.24M
            }
140
283k
        }
141
302k
        double* tmp = tmp_vector2;
142
302k
        tmp_vector2 = tmp_vector1;
143
302k
        tmp_vector1 = tmp;
144
145
302k
        int* tmp2 = vertices2;
146
302k
        vertices2 = vertices1;
147
302k
        vertices1 = tmp2;
148
149
302k
        nb_vertices1 = nb_vertices2;
150
302k
    }
151
152
100k
    if (nb_vertices1 > (G->nb_vertices / 2)) {
153
28.1k
        P = new double[G->nb_vertices];
154
28.1k
        size = G->nb_vertices;
155
28.1k
        vertices = nullptr;
156
28.1k
        if (nb_vertices1 == G->nb_vertices) {
157
820k
            for (int i = 0; i < G->nb_vertices; i++) {
158
803k
                P[i] = tmp_vector1[i] / sqrt(G->vertices[i].total_weight);
159
803k
            }
160
17.1k
        } else {
161
658k
            for (int i = 0; i < G->nb_vertices; i++) {
162
647k
                P[i] = 0.;
163
647k
            }
164
440k
            for (int i = 0; i < nb_vertices1; i++) {
165
429k
                P[vertices1[i]] = tmp_vector1[vertices1[i]] / sqrt(G->vertices[vertices1[i]].total_weight);
166
429k
            }
167
11.0k
        }
168
72.7k
    } else {
169
72.7k
        P = new double[nb_vertices1];
170
72.7k
        size = nb_vertices1;
171
72.7k
        vertices = new int[nb_vertices1];
172
72.7k
        int j = 0;
173
4.26M
        for (int i = 0; i < G->nb_vertices; i++) {
174
4.18M
            if (id[i] == current_id) {
175
823k
                P[j] = tmp_vector1[i] / sqrt(G->vertices[i].total_weight);
176
823k
                vertices[j] = i;
177
823k
                j++;
178
823k
            }
179
4.18M
        }
180
72.7k
    }
181
100k
}
182
183
87.5k
Probabilities::Probabilities(int community1, int community2) {
184
    // The two following probability vectors must exist.
185
    // Do not call this function if it is not the case.
186
87.5k
    Probabilities* P1 = C->communities[community1].P;
187
87.5k
    Probabilities* P2 = C->communities[community2].P;
188
189
87.5k
    double w1 = C->communities[community1].size / static_cast<double>(C->communities[community1].size + C->communities[community2].size);
190
87.5k
    double w2 = C->communities[community2].size / static_cast<double>(C->communities[community1].size + C->communities[community2].size);
191
192
193
87.5k
    if (P1->size == C->G->nb_vertices) {
194
28.1k
        P = new double[C->G->nb_vertices];
195
28.1k
        size = C->G->nb_vertices;
196
28.1k
        vertices = nullptr;
197
198
28.1k
        if (P2->size == C->G->nb_vertices) { // two full vectors
199
1.45M
            for (int i = 0; i < C->G->nb_vertices; i++) {
200
1.42M
                P[i] = P1->P[i] * w1 + P2->P[i] * w2;
201
1.42M
            }
202
26.7k
        } else { // P1 full vector, P2 partial vector
203
1.42k
            int j = 0;
204
29.1k
            for (int i = 0; i < P2->size; i++) {
205
70.0k
                for (; j < P2->vertices[i]; j++) {
206
42.4k
                    P[j] = P1->P[j] * w1;
207
42.4k
                }
208
27.6k
                P[j] = P1->P[j] * w1 + P2->P[i] * w2;
209
27.6k
                j++;
210
27.6k
            }
211
6.25k
            for (; j < C->G->nb_vertices; j++) {
212
4.82k
                P[j] = P1->P[j] * w1;
213
4.82k
            }
214
1.42k
        }
215
59.3k
    } else {
216
59.3k
        if (P2->size == C->G->nb_vertices) { // P1 partial vector, P2 full vector
217
5.67k
            P = new double[C->G->nb_vertices];
218
5.67k
            size = C->G->nb_vertices;
219
5.67k
            vertices = nullptr;
220
221
5.67k
            int j = 0;
222
125k
            for (int i = 0; i < P1->size; i++) {
223
309k
                for (; j < P1->vertices[i]; j++) {
224
190k
                    P[j] = P2->P[j] * w2;
225
190k
                }
226
119k
                P[j] = P1->P[i] * w1 + P2->P[j] * w2;
227
119k
                j++;
228
119k
            }
229
20.9k
            for (; j < C->G->nb_vertices; j++) {
230
15.2k
                P[j] = P2->P[j] * w2;
231
15.2k
            }
232
53.6k
        } else { // two partial vectors
233
53.6k
            int i = 0;
234
53.6k
            int j = 0;
235
53.6k
            int nb_vertices1 = 0;
236
751k
            while ((i < P1->size) && (j < P2->size)) {
237
697k
                if (P1->vertices[i] < P2->vertices[j]) {
238
31.2k
                    tmp_vector1[P1->vertices[i]] = P1->P[i] * w1;
239
31.2k
                    vertices1[nb_vertices1++] = P1->vertices[i];
240
31.2k
                    i++;
241
31.2k
                    continue;
242
31.2k
                }
243
666k
                if (P1->vertices[i] > P2->vertices[j]) {
244
65.0k
                    tmp_vector1[P2->vertices[j]] = P2->P[j] * w2;
245
65.0k
                    vertices1[nb_vertices1++] = P2->vertices[j];
246
65.0k
                    j++;
247
65.0k
                    continue;
248
65.0k
                }
249
601k
                tmp_vector1[P1->vertices[i]] = P1->P[i] * w1 + P2->P[j] * w2;
250
601k
                vertices1[nb_vertices1++] = P1->vertices[i];
251
601k
                i++;
252
601k
                j++;
253
601k
            }
254
53.6k
            if (i == P1->size) {
255
60.4k
                for (; j < P2->size; j++) {
256
9.16k
                    tmp_vector1[P2->vertices[j]] = P2->P[j] * w2;
257
9.16k
                    vertices1[nb_vertices1++] = P2->vertices[j];
258
9.16k
                }
259
51.3k
            } else {
260
6.68k
                for (; i < P1->size; i++) {
261
4.31k
                    tmp_vector1[P1->vertices[i]] = P1->P[i] * w1;
262
4.31k
                    vertices1[nb_vertices1++] = P1->vertices[i];
263
4.31k
                }
264
2.37k
            }
265
266
53.6k
            if (nb_vertices1 > (C->G->nb_vertices / 2)) {
267
508
                P = new double[C->G->nb_vertices];
268
508
                size = C->G->nb_vertices;
269
508
                vertices = nullptr;
270
31.0k
                for (int i = 0; i < C->G->nb_vertices; i++) {
271
30.4k
                    P[i] = 0.;
272
30.4k
                }
273
16.9k
                for (int i = 0; i < nb_vertices1; i++) {
274
16.4k
                    P[vertices1[i]] = tmp_vector1[vertices1[i]];
275
16.4k
                }
276
53.1k
            } else {
277
53.1k
                P = new double[nb_vertices1];
278
53.1k
                size = nb_vertices1;
279
53.1k
                vertices = new int[nb_vertices1];
280
747k
                for (int i = 0; i < nb_vertices1; i++) {
281
694k
                    vertices[i] = vertices1[i];
282
694k
                    P[i] = tmp_vector1[vertices1[i]];
283
694k
                }
284
53.1k
            }
285
53.6k
        }
286
59.3k
    }
287
87.5k
}
288
289
357k
double Probabilities::compute_distance(const Probabilities* P2) const {
290
357k
    double r = 0.0;
291
357k
    if (vertices) {
292
185k
        if (P2->vertices) { // two partial vectors
293
166k
            int i = 0;
294
166k
            int j = 0;
295
2.93M
            while ((i < size) && (j < P2->size)) {
296
2.76M
                if (vertices[i] < P2->vertices[j]) {
297
95.8k
                    r += P[i] * P[i];
298
95.8k
                    i++;
299
95.8k
                    continue;
300
95.8k
                }
301
2.67M
                if (vertices[i] > P2->vertices[j]) {
302
166k
                    r += P2->P[j] * P2->P[j];
303
166k
                    j++;
304
166k
                    continue;
305
166k
                }
306
2.50M
                r += (P[i] - P2->P[j]) * (P[i] - P2->P[j]);
307
2.50M
                i++;
308
2.50M
                j++;
309
2.50M
            }
310
166k
            if (i == size) {
311
182k
                for (; j < P2->size; j++) {
312
22.2k
                    r += P2->P[j] * P2->P[j];
313
22.2k
                }
314
160k
            } else {
315
17.9k
                for (; i < size; i++) {
316
11.1k
                    r += P[i] * P[i];
317
11.1k
                }
318
6.79k
            }
319
166k
        } else { // P1 partial vector, P2 full vector
320
321
19.1k
            int i = 0;
322
449k
            for (int j = 0; j < size; j++) {
323
1.07M
                for (; i < vertices[j]; i++) {
324
644k
                    r += P2->P[i] * P2->P[i];
325
644k
                }
326
430k
                r += (P[j] - P2->P[i]) * (P[j] - P2->P[i]);
327
430k
                i++;
328
430k
            }
329
62.2k
            for (; i < P2->size; i++) {
330
43.1k
                r += P2->P[i] * P2->P[i];
331
43.1k
            }
332
19.1k
        }
333
185k
    } else {
334
171k
        if (P2->vertices) { // P1 full vector, P2 partial vector
335
6.34k
            int i = 0;
336
153k
            for (int j = 0; j < P2->size; j++) {
337
346k
                for (; i < P2->vertices[j]; i++) {
338
199k
                    r += P[i] * P[i];
339
199k
                }
340
147k
                r += (P[i] - P2->P[j]) * (P[i] - P2->P[j]);
341
147k
                i++;
342
147k
            }
343
20.8k
            for (; i < size; i++) {
344
14.5k
                r += P[i] * P[i];
345
14.5k
            }
346
165k
        } else { // two full vectors
347
9.74M
            for (int i = 0; i < size; i++) {
348
9.57M
                r += (P[i] - P2->P[i]) * (P[i] - P2->P[i]);
349
9.57M
            }
350
165k
        }
351
171k
    }
352
357k
    return r;
353
357k
}
354
355
639k
Community::Community() {
356
639k
    P = nullptr;
357
639k
    first_neighbor = nullptr;
358
639k
    last_neighbor = nullptr;
359
639k
    sub_community_of = -1;
360
639k
    sub_communities[0] = -1;
361
639k
    sub_communities[1] = -1;
362
639k
    sigma = 0.;
363
639k
    internal_weight = 0.;
364
639k
    total_weight = 0.;
365
639k
}
366
367
639k
Community::~Community() {
368
639k
    delete P;
369
639k
}
370
371
372
Communities::Communities(Graph* graph, int random_walks_length,
373
                         igraph_matrix_int_t *pmerges,
374
7.09k
                         igraph_vector_t *pmodularity) {
375
7.09k
    G = graph;
376
7.09k
    merges = pmerges;
377
7.09k
    mergeidx = 0;
378
7.09k
    modularity = pmodularity;
379
380
7.09k
    Probabilities::C = this;
381
7.09k
    Probabilities::length = random_walks_length;
382
7.09k
    Probabilities::tmp_vector1 = new double[G->nb_vertices];
383
7.09k
    Probabilities::tmp_vector2 = new double[G->nb_vertices];
384
7.09k
    Probabilities::id = new int[G->nb_vertices];
385
326k
    for (int i = 0; i < G->nb_vertices; i++) {
386
319k
        Probabilities::id[i] = 0;
387
319k
    }
388
7.09k
    Probabilities::vertices1 = new int[G->nb_vertices];
389
7.09k
    Probabilities::vertices2 = new int[G->nb_vertices];
390
7.09k
    Probabilities::current_id = 0;
391
392
7.09k
    members = new int[G->nb_vertices];
393
326k
    for (int i = 0; i < G->nb_vertices; i++) {
394
319k
        members[i] = -1;
395
319k
    }
396
397
7.09k
    H = new Neighbor_heap(G->nb_edges);
398
7.09k
    IGRAPH_ASSUME(G->nb_vertices >= 0); // avoid false-positive GCC warnings
399
7.09k
    communities = new Community[2 * G->nb_vertices];
400
401
    // init the n single vertex communities
402
403
326k
    for (int i = 0; i < G->nb_vertices; i++) {
404
319k
        communities[i].this_community = i;
405
319k
        communities[i].first_member = i;
406
319k
        communities[i].last_member = i;
407
319k
        communities[i].size = 1;
408
319k
        communities[i].sub_community_of = 0;
409
        /* Input self-loops are internal to the community. Their weight is counted
410
         * once in total_weight (half the strength) to match G->total_weight. */
411
319k
        communities[i].internal_weight = G->vertices[i].loop_weight;
412
319k
        communities[i].total_weight = G->vertices[i].loop_weight;
413
319k
    }
414
415
7.09k
    nb_communities = G->nb_vertices;
416
7.09k
    nb_active_communities = G->nb_vertices;
417
418
326k
    for (int i = 0; i < G->nb_vertices; i++)
419
875k
        for (int j = 0; j < G->vertices[i].degree; j++)
420
555k
            if (i < G->vertices[i].edges[j].neighbor) {
421
118k
                communities[i].total_weight += G->vertices[i].edges[j].weight / 2.;
422
118k
                communities[G->vertices[i].edges[j].neighbor].total_weight += G->vertices[i].edges[j].weight / 2.;
423
118k
                Neighbor* N = new Neighbor;
424
118k
                N->community1 = i;
425
118k
                N->community2 = G->vertices[i].edges[j].neighbor;
426
118k
                N->delta_sigma = -1. / double(min(G->vertices[i].degree,  G->vertices[G->vertices[i].edges[j].neighbor].degree));
427
118k
                N->weight = G->vertices[i].edges[j].weight;
428
118k
                N->exact = false;
429
118k
                add_neighbor(N);
430
118k
            }
431
432
    /*   int c = 0; */
433
7.09k
    Neighbor* N = H->get_first();
434
    /* N is null when there are no edges other than self-loops. */
435
125k
    while (N != nullptr && !N->exact) {
436
118k
        update_neighbor(N, compute_delta_sigma(N->community1, N->community2));
437
118k
        N->exact = true;
438
118k
        N = H->get_first();
439
        /* TODO: this could use igraph_progress */
440
        /*     if(!silent) { */
441
        /*       c++; */
442
        /*       for(int k = (500*(c-1))/G->nb_edges + 1; k <= (500*c)/G->nb_edges; k++) { */
443
        /*  if(k % 50 == 1) {cerr.width(2); cerr << endl << k/ 5 << "% ";} */
444
        /*  cerr << "."; */
445
        /*       } */
446
        /*     } */
447
118k
    }
448
449
    /* The modularity of edgeless graphs is set to NaN by the caller. */
450
7.09k
    if (modularity && G->total_weight > 0) {
451
7.00k
        double Q = 0.0;
452
323k
        for (int i = 0; i < nb_communities; i++) {
453
316k
            if (communities[i].sub_community_of == 0) {
454
316k
                Q += (communities[i].internal_weight - communities[i].total_weight * communities[i].total_weight / G->total_weight);
455
316k
            }
456
316k
        }
457
7.00k
        Q /= G->total_weight;
458
7.00k
        VECTOR(*modularity)[mergeidx] = Q;
459
7.00k
    }
460
7.09k
}
461
462
7.09k
Communities::~Communities() {
463
7.09k
    delete[] members;
464
7.09k
    delete[] communities;
465
7.09k
    delete H;
466
467
7.09k
    delete[] Probabilities::tmp_vector1;
468
7.09k
    delete[] Probabilities::tmp_vector2;
469
7.09k
    delete[] Probabilities::id;
470
7.09k
    delete[] Probabilities::vertices1;
471
7.09k
    delete[] Probabilities::vertices2;
472
7.09k
}
473
474
1.22M
void Community::add_neighbor(Neighbor* N) { // add a new neighbor at the end of the list
475
1.22M
    if (last_neighbor) {
476
723k
        if (last_neighbor->community1 == this_community) {
477
186k
            last_neighbor->next_community1 = N;
478
537k
        } else {
479
537k
            last_neighbor->next_community2 = N;
480
537k
        }
481
482
723k
        if (N->community1 == this_community) {
483
259k
            N->previous_community1 = last_neighbor;
484
463k
        } else {
485
463k
            N->previous_community2 = last_neighbor;
486
463k
        }
487
723k
    } else {
488
499k
        first_neighbor = N;
489
499k
        if (N->community1 == this_community) {
490
351k
            N->previous_community1 = nullptr;
491
351k
        } else {
492
147k
            N->previous_community2 = nullptr;
493
147k
        }
494
499k
    }
495
1.22M
    last_neighbor = N;
496
1.22M
}
497
498
1.22M
void Community::remove_neighbor(Neighbor* N) {  // remove a neighbor from the list
499
1.22M
    if (N->community1 == this_community) {
500
611k
        if (N->next_community1) {
501
//      if (N->next_community1->community1 == this_community)
502
147k
            N->next_community1->previous_community1 = N->previous_community1;
503
//      else
504
//  N->next_community1->previous_community2 = N->previous_community1;
505
463k
        } else {
506
463k
            last_neighbor = N->previous_community1;
507
463k
        }
508
611k
        if (N->previous_community1) {
509
114k
            if (N->previous_community1->community1 == this_community) {
510
70.6k
                N->previous_community1->next_community1 = N->next_community1;
511
70.6k
            } else {
512
43.8k
                N->previous_community1->next_community2 = N->next_community1;
513
43.8k
            }
514
496k
        } else {
515
496k
            first_neighbor = N->next_community1;
516
496k
        }
517
611k
    } else {
518
611k
        if (N->next_community2) {
519
488k
            if (N->next_community2->community1 == this_community) {
520
61.0k
                N->next_community2->previous_community1 = N->previous_community2;
521
427k
            } else {
522
427k
                N->next_community2->previous_community2 = N->previous_community2;
523
427k
            }
524
488k
        } else {
525
122k
            last_neighbor = N->previous_community2;
526
122k
        }
527
611k
        if (N->previous_community2) {
528
//      if (N->previous_community2->community1 == this_community)
529
//  N->previous_community2->next_community1 = N->next_community2;
530
//      else
531
98.9k
            N->previous_community2->next_community2 = N->next_community2;
532
512k
        } else {
533
512k
            first_neighbor = N->next_community2;
534
512k
        }
535
611k
    }
536
1.22M
}
537
538
611k
void Communities::remove_neighbor(Neighbor* N) {
539
611k
    communities[N->community1].remove_neighbor(N);
540
611k
    communities[N->community2].remove_neighbor(N);
541
611k
    H->remove(N);
542
611k
}
543
544
611k
void Communities::add_neighbor(Neighbor* N) {
545
611k
    communities[N->community1].add_neighbor(N);
546
611k
    communities[N->community2].add_neighbor(N);
547
611k
    H->add(N);
548
611k
}
549
550
357k
void Communities::update_neighbor(Neighbor* N, double new_delta_sigma) {
551
357k
    N->delta_sigma = new_delta_sigma;
552
357k
    H->update(N);
553
357k
}
554
555
87.5k
void Communities::merge_communities(Neighbor* merge_N) {
556
87.5k
    int c1 = merge_N->community1;
557
87.5k
    int c2 = merge_N->community2;
558
559
87.5k
    communities[nb_communities].first_member = communities[c1].first_member;  // merge the
560
87.5k
    communities[nb_communities].last_member = communities[c2].last_member;    // two lists
561
87.5k
    members[communities[c1].last_member] = communities[c2].first_member;      // of members
562
563
87.5k
    communities[nb_communities].size = communities[c1].size + communities[c2].size;
564
87.5k
    communities[nb_communities].this_community = nb_communities;
565
87.5k
    communities[nb_communities].sub_community_of = 0;
566
87.5k
    communities[nb_communities].sub_communities[0] = c1;
567
87.5k
    communities[nb_communities].sub_communities[1] = c2;
568
87.5k
    communities[nb_communities].total_weight = communities[c1].total_weight + communities[c2].total_weight;
569
87.5k
    communities[nb_communities].internal_weight = communities[c1].internal_weight + communities[c2].internal_weight + merge_N->weight;
570
87.5k
    communities[nb_communities].sigma = communities[c1].sigma + communities[c2].sigma + merge_N->delta_sigma;
571
572
87.5k
    communities[c1].sub_community_of = nb_communities;
573
87.5k
    communities[c2].sub_community_of = nb_communities;
574
575
// update the new probability vector...
576
577
87.5k
    if (communities[c1].P && communities[c2].P) {
578
87.5k
        communities[nb_communities].P = new Probabilities(c1, c2);
579
87.5k
    }
580
581
87.5k
    if (communities[c1].P) {
582
87.5k
        delete communities[c1].P;
583
87.5k
        communities[c1].P = nullptr;
584
87.5k
    }
585
87.5k
    if (communities[c2].P) {
586
87.5k
        delete communities[c2].P;
587
87.5k
        communities[c2].P = nullptr;
588
87.5k
    }
589
590
// update the new neighbors
591
// by enumerating all the neighbors of c1 and c2
592
593
87.5k
    Neighbor* N1 = communities[c1].first_neighbor;
594
87.5k
    Neighbor* N2 = communities[c2].first_neighbor;
595
596
210k
    while (N1 && N2) {
597
122k
        int neighbor_community1;
598
122k
        int neighbor_community2;
599
600
122k
        if (N1->community1 == c1) {
601
89.0k
            neighbor_community1 = N1->community2;
602
89.0k
        } else {
603
33.7k
            neighbor_community1 = N1->community1;
604
33.7k
        }
605
122k
        if (N2->community1 == c2) {
606
43.2k
            neighbor_community2 = N2->community2;
607
79.4k
        } else {
608
79.4k
            neighbor_community2 = N2->community1;
609
79.4k
        }
610
611
122k
        if (neighbor_community1 < neighbor_community2) {
612
35.4k
            Neighbor* tmp = N1;
613
35.4k
            if (N1->community1 == c1) {
614
22.6k
                N1 = N1->next_community1;
615
22.6k
            } else {
616
12.7k
                N1 = N1->next_community2;
617
12.7k
            }
618
35.4k
            remove_neighbor(tmp);
619
35.4k
            Neighbor* N = new Neighbor;
620
35.4k
            N->weight = tmp->weight;
621
35.4k
            N->community1 = neighbor_community1;
622
35.4k
            N->community2 = nb_communities;
623
35.4k
            N->delta_sigma = (double(communities[c1].size + communities[neighbor_community1].size) * tmp->delta_sigma + double(communities[c2].size) * merge_N->delta_sigma) / (double(communities[c1].size + communities[c2].size + communities[neighbor_community1].size)); //compute_delta_sigma(neighbor_community1, nb_communities);
624
35.4k
            N->exact = false;
625
35.4k
            delete tmp;
626
35.4k
            add_neighbor(N);
627
35.4k
        }
628
629
122k
        if (neighbor_community2 < neighbor_community1) {
630
56.8k
            Neighbor* tmp = N2;
631
56.8k
            if (N2->community1 == c2) {
632
10.6k
                N2 = N2->next_community1;
633
46.1k
            } else {
634
46.1k
                N2 = N2->next_community2;
635
46.1k
            }
636
56.8k
            remove_neighbor(tmp);
637
56.8k
            Neighbor* N = new Neighbor;
638
56.8k
            N->weight = tmp->weight;
639
56.8k
            N->community1 = neighbor_community2;
640
56.8k
            N->community2 = nb_communities;
641
56.8k
            N->delta_sigma = (double(communities[c1].size) * merge_N->delta_sigma + double(communities[c2].size + communities[neighbor_community2].size) * tmp->delta_sigma) / (double(communities[c1].size + communities[c2].size + communities[neighbor_community2].size)); //compute_delta_sigma(neighbor_community2, nb_communities);
642
56.8k
            N->exact = false;
643
56.8k
            delete tmp;
644
56.8k
            add_neighbor(N);
645
56.8k
        }
646
647
122k
        if (neighbor_community1 == neighbor_community2) {
648
30.4k
            Neighbor* tmp1 = N1;
649
30.4k
            Neighbor* tmp2 = N2;
650
30.4k
            bool exact = N1->exact && N2->exact;
651
30.4k
            if (N1->community1 == c1) {
652
21.6k
                N1 = N1->next_community1;
653
21.6k
            } else {
654
8.85k
                N1 = N1->next_community2;
655
8.85k
            }
656
30.4k
            if (N2->community1 == c2) {
657
13.2k
                N2 = N2->next_community1;
658
17.2k
            } else {
659
17.2k
                N2 = N2->next_community2;
660
17.2k
            }
661
30.4k
            remove_neighbor(tmp1);
662
30.4k
            remove_neighbor(tmp2);
663
30.4k
            Neighbor* N = new Neighbor;
664
30.4k
            N->weight = tmp1->weight + tmp2->weight;
665
30.4k
            N->community1 = neighbor_community1;
666
30.4k
            N->community2 = nb_communities;
667
30.4k
            N->delta_sigma = (double(communities[c1].size + communities[neighbor_community1].size) * tmp1->delta_sigma + double(communities[c2].size + communities[neighbor_community1].size) * tmp2->delta_sigma - double(communities[neighbor_community1].size) * merge_N->delta_sigma) / (double(communities[c1].size + communities[c2].size + communities[neighbor_community1].size));
668
30.4k
            N->exact = exact;
669
30.4k
            delete tmp1;
670
30.4k
            delete tmp2;
671
30.4k
            add_neighbor(N);
672
30.4k
        }
673
122k
    }
674
675
676
87.5k
    if (!N1) {
677
398k
        while (N2) {
678
//      double delta_sigma2 = N2->delta_sigma;
679
326k
            int neighbor_community;
680
326k
            if (N2->community1 == c2) {
681
22.9k
                neighbor_community = N2->community2;
682
303k
            } else {
683
303k
                neighbor_community = N2->community1;
684
303k
            }
685
326k
            Neighbor* tmp = N2;
686
326k
            if (N2->community1 == c2) {
687
22.9k
                N2 = N2->next_community1;
688
303k
            } else {
689
303k
                N2 = N2->next_community2;
690
303k
            }
691
326k
            remove_neighbor(tmp);
692
326k
            Neighbor* N = new Neighbor;
693
326k
            N->weight = tmp->weight;
694
326k
            N->community1 = neighbor_community;
695
326k
            N->community2 = nb_communities;
696
326k
            N->delta_sigma = (double(communities[c1].size) * merge_N->delta_sigma + double(communities[c2].size + communities[neighbor_community].size) * tmp->delta_sigma) / (double(communities[c1].size + communities[c2].size + communities[neighbor_community].size)); //compute_delta_sigma(neighbor_community, nb_communities);
697
326k
            N->exact = false;
698
326k
            delete tmp;
699
326k
            add_neighbor(N);
700
326k
        }
701
71.8k
    }
702
87.5k
    if (!N2) {
703
131k
        while (N1) {
704
//      double delta_sigma1 = N1->delta_sigma;
705
43.5k
            int neighbor_community;
706
43.5k
            if (N1->community1 == c1) {
707
38.6k
                neighbor_community = N1->community2;
708
38.6k
            } else {
709
4.93k
                neighbor_community = N1->community1;
710
4.93k
            }
711
43.5k
            Neighbor* tmp = N1;
712
43.5k
            if (N1->community1 == c1) {
713
38.6k
                N1 = N1->next_community1;
714
38.6k
            } else {
715
4.93k
                N1 = N1->next_community2;
716
4.93k
            }
717
43.5k
            remove_neighbor(tmp);
718
43.5k
            Neighbor* N = new Neighbor;
719
43.5k
            N->weight = tmp->weight;
720
43.5k
            N->community1 = neighbor_community;
721
43.5k
            N->community2 = nb_communities;
722
43.5k
            N->delta_sigma = (double(communities[c1].size + communities[neighbor_community].size) * tmp->delta_sigma + double(communities[c2].size) * merge_N->delta_sigma) / (double(communities[c1].size + communities[c2].size + communities[neighbor_community].size)); //compute_delta_sigma(neighbor_community, nb_communities);
723
43.5k
            N->exact = false;
724
43.5k
            delete tmp;
725
43.5k
            add_neighbor(N);
726
43.5k
        }
727
87.5k
    }
728
729
87.5k
    nb_communities++;
730
87.5k
    nb_active_communities--;
731
87.5k
}
732
733
87.5k
double Communities::merge_nearest_communities() {
734
87.5k
    Neighbor* N = H->get_first();
735
326k
    while (!N->exact) {
736
239k
        update_neighbor(N, compute_delta_sigma(N->community1, N->community2));
737
239k
        N->exact = true;
738
239k
        N = H->get_first();
739
239k
    }
740
741
87.5k
    double d = N->delta_sigma;
742
87.5k
    remove_neighbor(N);
743
744
87.5k
    merge_communities(N);
745
746
87.5k
    if (merges) {
747
87.5k
        MATRIX(*merges, mergeidx, 0) = N->community1;
748
87.5k
        MATRIX(*merges, mergeidx, 1) = N->community2;
749
87.5k
    }
750
751
87.5k
    mergeidx++;
752
753
87.5k
    if (modularity) {
754
87.5k
        double Q = 0.0;
755
6.31M
        for (int i = 0; i < nb_communities; i++) {
756
6.22M
            if (communities[i].sub_community_of == 0) {
757
3.64M
                Q += (communities[i].internal_weight - communities[i].total_weight * communities[i].total_weight / G->total_weight);
758
3.64M
            }
759
6.22M
        }
760
87.5k
        Q /= G->total_weight;
761
87.5k
        VECTOR(*modularity)[mergeidx] = Q;
762
87.5k
    }
763
764
87.5k
    delete N;
765
766
    /* This could use igraph_progress */
767
    /*   if(!silent) { */
768
    /*     for(int k = (500*(G->nb_vertices - nb_active_communities - 1))/(G->nb_vertices-1) + 1; k <= (500*(G->nb_vertices - nb_active_communities))/(G->nb_vertices-1); k++) { */
769
    /*       if(k % 50 == 1) {cerr.width(2); cerr << endl << k/ 5 << "% ";} */
770
    /*       cerr << "."; */
771
    /*     } */
772
    /*   } */
773
87.5k
    return d;
774
87.5k
}
775
776
357k
double Communities::compute_delta_sigma(int community1, int community2) const {
777
357k
    if (!communities[community1].P) {
778
38.3k
        communities[community1].P = new Probabilities(community1);
779
38.3k
    }
780
357k
    if (!communities[community2].P) {
781
62.5k
        communities[community2].P = new Probabilities(community2);
782
62.5k
    }
783
784
357k
    return communities[community1].P->compute_distance(communities[community2].P) * double(communities[community1].size) * double(communities[community2].size) / double(communities[community1].size + communities[community2].size);
785
357k
}
786
787
}
788
}    /* end of namespaces */