/** * Community Detection over the File-Import Graph (P4-T4 / D4) * * Multi-level **Louvain** modularity optimization with a connectivity * refinement pass. Surfaces de-facto modules/clusters: tightly-coupled groups * of files that form implicit packages/layers in the codebase. * * Why Louvain (+ refinement) rather than full Leiden: * - Louvain is simpler and well-understood; its main known defect is that a * local-moving phase can leave a community internally disconnected. We * close that gap with an explicit connectivity refinement pass * (split-and-reattach by connected components within each community), which * is the property Leiden guarantees. This gives Leiden-quality output at * Louvain implementation cost, with no heavy dependency. * * Algorithm sketch (porting the *approach* from codebase-memory-mcp's Leiden, * rewritten fresh in TypeScript): * * 1. Build an undirected weighted graph (CSR: offsets + neighbors + weights). * Directed import edges {from→to} are symmetrized and deduped (multi-edges * become weights); self-loops are folded into each node's weighted degree. * 2. Local-moving phase: greedily move each node to the neighboring community * that maximizes modularity gain, until stable. Re-queue only neighbors of * moved nodes (near-linear per level). * 3. Refinement: ensure every community is internally connected by splitting * disconnected communities along their connected components. * 4. Aggregate: collapse communities into super-nodes; repeat on the coarse * graph until a level stops shrinking. * 5. Back-project the final community onto the original node ids via an * `orig[]` indirection array. * * Bounds & fallback: * - Node cap `COMMUNITY_NODE_CAP` (default 8000). Graphs above this run a * cheaper connected-components + weighted label-propagation hybrid. * - Edge cap `COMMUNITY_EDGE_CAP` (default 50000) — same fallback. * - Max levels `MAX_LEVELS` (default 64) and per-pass iteration cap guard * against pathological oscillation. * - Empty / single-node / no-edge graphs short-circuit: every node its own * community (no crash). * * Resolution `gamma` (default 1.0): higher → more, smaller communities. */ /** Above this many nodes, fall back to the cheaper label-propagation hybrid. */ export declare const COMMUNITY_NODE_CAP = 8000; /** Above this many undirected edges, fall back. */ export declare const COMMUNITY_EDGE_CAP = 50000; export interface WeightedEdge { /** Index into the `nodes` array of runLouvain. */ a: number; b: number; /** Edge weight (>= 0). */ w: number; } export interface CommunityOptions { /** Resolution parameter; higher → more, smaller communities. Default 1.0. */ gamma?: number; /** Node cap above which the cheaper fallback runs. */ nodeCap?: number; /** Edge cap above which the cheaper fallback runs. */ edgeCap?: number; } export interface Community { /** Stable community id (0-based, contiguous in the result array). */ id: number; /** Original node indices belonging to this community. */ members: number[]; } export interface CommunityResult { communities: Community[]; /** communityId per original node (length === nodes.length). */ assignment: number[]; /** Modularity of the final partition (-1..1, higher is better). */ modularity: number; /** Which algorithm produced this result. */ algorithm: "louvain" | "fallback" | "trivial"; } /** * Run multi-level Louvain community detection. * * @param nodeCount number of nodes (files). * @param edges weighted undirected edges (will be symmetrized internally). * @param opts gamma + caps. * @returns communities, per-node assignment, modularity, and algorithm used. */ export declare function runLouvain(nodeCount: number, edges: WeightedEdge[], opts?: CommunityOptions): CommunityResult; //# sourceMappingURL=communities.d.ts.map