Skip to content

Leiden clustering on code-heavy graphs produces low-cohesion fragmentation (274 communities, cohesion 0.01) #446

Open

Description

@harnill

Context

Building on a Python analysis-engine corpus (297 code files, 54 docs, 59 images → 8333 nodes, 19086 edges), Leiden community detection returned 274 communities. The top-10 communities (~4400 nodes total) all reported cohesion between 0.010 and 0.020 — i.e., most communities have edge density barely above random.

Why this matters

  • The skill's GRAPH_REPORT.md surfaces communities + god nodes as the primary navigation layer.
  • When cohesion is ~0.01 across the top communities, the resulting labels are too broad to be useful ("Core Services & Viewer" holds 705 heterogeneous nodes).
  • Downstream: community-labelled wiki articles become dumping grounds, not focused references.

Root cause (hypothesis)

Code graphs are leaf-heavy: docstring summaries, helper functions, and per-file __init__ nodes all attach to a small number of hubs with degree=1. Leiden's modularity score collapses because these leaves don't contribute to within-community density.

Proposed fix

  1. Prune degree-1 leaves before clustering, attach them back post-cluster to their single neighbour's community.
  2. Tune resolution parameter — default Leiden resolution appears to be 1.0; for code graphs of 5K+ nodes, bumping to 1.5–2.0 yields fewer, denser communities in practice.
  3. Post-cluster merge step: merge any community smaller than N nodes (e.g., 10) into its most-connected neighbour.
  4. Optional: expose --cluster-resolution flag.

Repro data

  • Nodes: 8333, Edges: 19086
  • Communities: 274 (avg 30 nodes)
  • Top-10 community sizes: 705, 510, 506, 498, 478, 422, 342, 311, 292, 292
  • Top-10 cohesion: all between 0.010 and 0.020

Happy to share a sanitized graph.json if useful.

Activity

  1. arcticloud commented on Jul 9, 2026

    @arcticloud

    @safishamsi I'd like to take this one — here's the plan, following @harnill's points 1 and 3:

    1. Leaf-pruning (point 1). Exclude degree-1 leaves whose sole neighbour is a hub before partitioning — right alongside the existing hub-exclusion in cluster() — and reattach each to its neighbour's community afterwards (a leaf has exactly one neighbour, so it's unambiguous). I gate on the neighbour being a hub (degree >= 3) so short chains / small peripheral structures aren't disturbed on small graphs.

    2. Small-community merge (point 3). After the existing split passes, merge sub-N communities (default N = 10) into the most-connected substantial neighbour (itself >= N), deterministic tie-break; disconnected fragments left as-is.

    Resolution (point 2): the --resolution flag already exists (default 1.0), so I'll leave the default alone rather than risk a global regression — happy to add an opt-in adaptive bump for >5k-node graphs behind the flag as a separate change if you want it.

    One honest finding from prototyping on a synthetic leaf-heavy graph (560 nodes, 501 leaves): leaf-pruning is the real cohesion win (mean community cohesion 0.04 → 0.28), while the merge is a count-vs-cohesion trade-off — it cuts the tiny-fragment tail (the "274 communities" half) but slightly dilutes the largest communities it folds fragments into. So I've kept _MIN_MERGE_SIZE a module constant and the two stages independent, so you can go cohesion-maximal (pruning only) or fewer-communities (with merge) as you prefer.

    Both stages preserve the two invariants the tests rely on: all nodes covered, and deterministic community IDs. I have it implemented with tests passing on both the Leiden and Louvain-fallback paths, and I'll include before/after numbers on both in the PR.

    Does the approach look right to you? And @harnill — a sanitized graph.json from your repro would be great to validate against real numbers before I finalize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions