Aggregation phase: collapse each refined sub-community into a single node. * Builds the coarser graph in *out and records, for each aggregate node, the * move-phase community it belonged to in seed[] so the next level starts from * the coarse structure rather than from singletons. */
| 10270 | * move-phase community it belonged to in seed[] so the next level starts from |
| 10271 | * the coarse structure rather than from singletons. */ |
| 10272 | static int leiden_aggregate(const cbm_lg_t *g, const int *refined, int r_count, const int *comm, |
| 10273 | cbm_lg_t *out, int *seed) { |
| 10274 | int n = g->n; |
| 10275 | double *k2 = calloc((size_t)r_count, sizeof(double)); |
| 10276 | int *gcount = calloc((size_t)r_count, sizeof(int)); |
| 10277 | /* calloc: the fill-cursor pattern writes every slot (same degree-sum |
| 10278 | * invariant as the CSR builds), invisible to path-sensitive analysis. */ |
| 10279 | int *gstart = calloc((size_t)r_count + 1, sizeof(int)); |
| 10280 | int *members = calloc((size_t)n, sizeof(int)); |
| 10281 | int *fill = calloc((size_t)r_count, sizeof(int)); |
| 10282 | double *acc = calloc((size_t)r_count, sizeof(double)); |
| 10283 | int *dirty = malloc((size_t)r_count * sizeof(int)); |
| 10284 | int *off2 = malloc(((size_t)r_count + 1) * sizeof(int)); |
| 10285 | if (!k2 || !gcount || !gstart || !members || !fill || !acc || !dirty || !off2) { |
| 10286 | free(k2); |
| 10287 | free(gcount); |
| 10288 | free(gstart); |
| 10289 | free(members); |
| 10290 | free(fill); |
| 10291 | free(acc); |
| 10292 | free(dirty); |
| 10293 | free(off2); |
| 10294 | return CBM_NOT_FOUND; |
| 10295 | } |
| 10296 | for (int r = 0; r < r_count; r++) { |
| 10297 | seed[r] = CBM_NOT_FOUND; |
| 10298 | } |
| 10299 | for (int i = 0; i < n; i++) { |
| 10300 | int r = refined[i]; |
| 10301 | k2[r] += g->k[i]; |
| 10302 | gcount[r]++; |
| 10303 | if (seed[r] == CBM_NOT_FOUND) { |
| 10304 | seed[r] = comm[i]; |
| 10305 | } |
| 10306 | } |
| 10307 | gstart[0] = 0; |
| 10308 | for (int r = 0; r < r_count; r++) { |
| 10309 | gstart[r + 1] = gstart[r] + gcount[r]; |
| 10310 | fill[r] = gstart[r]; |
| 10311 | } |
| 10312 | for (int i = 0; i < n; i++) { |
| 10313 | members[fill[refined[i]]++] = i; |
| 10314 | } |
| 10315 | /* Pass 1: count distinct inter-community neighbours per aggregate node. */ |
| 10316 | off2[0] = 0; |
| 10317 | for (int r = 0; r < r_count; r++) { |
| 10318 | int nd = 0; |
| 10319 | for (int m = gstart[r]; m < gstart[r + 1]; m++) { |
| 10320 | int i = members[m]; |
| 10321 | for (int e = g->off[i]; e < g->off[i + 1]; e++) { |
| 10322 | int rb = refined[g->nbr[e]]; |
| 10323 | if (rb == r || acc[rb] != 0.0) { |
| 10324 | continue; |
| 10325 | } |
| 10326 | acc[rb] = 1.0; |
| 10327 | dirty[nd++] = rb; |
| 10328 | } |
| 10329 | } |