MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / leiden_refine

Function leiden_refine

src/store/store.c:10194–10266  ·  view source on GitHub ↗

Refinement phase: within each move-phase community, merge singleton nodes * into the best connected sub-community (positive modularity gain, edge must * exist). This re-derives communities bottom-up so each one is guaranteed * internally connected — the defect single-level Louvain suffers from, which * fragments the graph into hundreds of tiny noisy clusters. Writes * sub-community labels int

Source from the content-addressed store, hash-verified

10192 * fragments the graph into hundreds of tiny noisy clusters. Writes
10193 * sub-community labels into refined[] and returns their count. */
10194static int leiden_refine(const cbm_lg_t *g, const int *comm, double gamma, double twom,
10195 int *refined) {
10196 int n = g->n;
10197 double *stot = calloc((size_t)n, sizeof(double));
10198 double *acc = calloc((size_t)n, sizeof(double));
10199 int *rsize = malloc((size_t)n * sizeof(int));
10200 int *dirty = malloc((size_t)n * sizeof(int));
10201 if (!stot || !acc || !rsize || !dirty) {
10202 free(stot);
10203 free(acc);
10204 free(rsize);
10205 free(dirty);
10206 for (int i = 0; i < n; i++) {
10207 refined[i] = i;
10208 }
10209 return leiden_relabel(refined, n);
10210 }
10211 for (int i = 0; i < n; i++) {
10212 refined[i] = i;
10213 stot[i] = g->k[i];
10214 rsize[i] = 1;
10215 }
10216 for (int v = 0; v < n; v++) {
10217 if (rsize[refined[v]] != 1) {
10218 continue; /* only singletons merge, per the refinement rule */
10219 }
10220 int cv = comm[v];
10221 int ndirty = 0;
10222 for (int e = g->off[v]; e < g->off[v + 1]; e++) {
10223 int u = g->nbr[e];
10224 if (u == v || comm[u] != cv) {
10225 continue; /* stay within the move-phase community */
10226 }
10227 int ru = refined[u];
10228 if (acc[ru] == 0.0) {
10229 dirty[ndirty++] = ru;
10230 }
10231 acc[ru] += g->w[e];
10232 }
10233 int rv = refined[v];
10234 double kv = g->k[v];
10235 stot[rv] -= kv;
10236 int best_r = rv;
10237 double best_gain = 0.0;
10238 for (int d = 0; d < ndirty; d++) {
10239 int r = dirty[d];
10240 if (r == rv) {
10241 continue;
10242 }
10243 double gain = acc[r] - gamma * kv * stot[r] / twom;
10244 if (gain > best_gain) {
10245 best_gain = gain;
10246 best_r = r;
10247 }
10248 }
10249 if (best_r != rv) {
10250 refined[v] = best_r;
10251 stot[best_r] += kv;

Callers 1

cbm_leidenFunction · 0.85

Calls 1

leiden_relabelFunction · 0.85

Tested by

no test coverage detected