liveBook · Manning

Leiden algorithm

As explained in Domain-Specific Small Language Models

A community-detection algorithm that improves graph clustering by optimizing modularity through iterative node movement and aggregation.

The Leiden algorithm detects communities in a graph by optimizing modularity. It begins with each node in its own community, moves nodes to improve the score, refines the resulting partition, aggregates communities into a new network, and repeats until improvement stops. section 14.1

It improves on Louvain by refining communities before aggregation, addressing Louvain’s possibility of producing internally disconnected communities. NetworkX does not provide Leiden directly, so a simple pure-Python implementation can supply the missing step. section 14.1

A practical Leiden pass follows this sequence:

  • Initialize a singleton partition with one community per graph node. section 14.1
  • Evaluate moving each node into neighboring communities using modularity gain, retaining moves that improve the partition. section 14.1
  • Refine the partition, aggregate nodes belonging to the same community, and repeat for a bounded number of iterations or until no improvement remains. section 14.1

Community detection in GraphRAG

After extracted entities and relationships are merged into a NetworkX graph, Leiden assigns community identifiers. The resulting groups can be converted into node lists, summarized as related document groups, and used by global search to produce intermediate answers before combining them into a final response. section 14.1 section 14.4

How Domain-Specific Small Language Models covers it

How deep it goes
Explains the iterative algorithm and modularity-gain calculation through a simple implementation, while omitting production validation and optimization details.
Examples
Uses a NetworkX entity graph, community assignments, node lists, community summaries, and combined global-search answers.
What it assumes
Assumes graphs with nodes and edges plus entity relationships, while introducing modularity gain, partition refinement, and aggregation operationally.
What you can do after
Build it from scratch in pure Python, assign graph nodes to communities, and feed those groups into GraphRAG summarization.
How it connects
Connects modularity and community detection with Louvain, knowledge graphs, GraphRAG, and global search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.