liveBook · Manning

Community

As explained in Retrieval Augmented Generation, The Foundational Ideas

A graph community is a group of nodes identified as more strongly or densely connected to one another than to the rest of the graph.

A graph community is a densely connected cluster of nodes, usually found with a community-detection algorithm such as Leiden. In graph-based retrieval, it groups related entities and relationships so systems can summarize themes that no single text chunk expresses. section 12.1

Communities address global questions that vector lookup handles poorly, such as identifying recurring themes across hundreds of vendor-dispute emails. GraphRAG detects communities and precomputes hierarchical summaries, trading higher indexing cost for fast retrieval. section 12.1

A practical community-detection workflow includes:

  • Build a directed NetworkX graph from extracted entities and relationships, retaining node attributes and edge weights. section 12.2.3
  • Copy the graph into igraph and apply Leiden modularity optimization until convergence. section 12.2.3
  • Map clustered igraph vertices back to NetworkX identifiers and store each community as a node collection. section 12.2.3

Recurring themes across disputes

A query asking, “What are the recurring themes in this year's vendor disputes?” can use community summaries to synthesize patterns across many connected documents instead of retrieving only the loudest individual examples. section 12.1

Listing 12.2 Community detection.
import igraph as ig
import leidenalg
 
def detect_communities(self) -> Dict[int, List[str]]:
    """Apply Leiden algorithm to detect communities."""
    g_ig = ig.Graph.from_networkx(self.graph)  #A
 
    partition = leidenalg.find_partition(  #B
        g_ig,
        leidenalg.ModularityVertexPartition,
        weights='weight',
        n_iterations=-1
    )
 
    self.communities = {}  #C
    for idx, cluster_nodes in enumerate(partition):
        node_labels = [
            g_ig.vs[i]['_nx_name'] for i in cluster_nodes  #D
        ]
        self.communities[idx] = node_labels
 
    return self.communities
 

The listing shows Leiden community detection with igraph and leidenalg, including weighted modularity partitioning and conversion back to NetworkX node identifiers. section 12.2.3

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Explains density, Leiden partitioning, modularity optimization, hierarchy, and identifier recovery, but not community-quality evaluation.
Examples
Uses vendor-dispute themes and a Leiden-based Python implementation as concrete examples.
What it assumes
Assumes entities, relationships, graphs, embeddings, and retrieval, while defining traversal, communities, and ego-graphs.
What you can do after
Build it from scratch with NetworkX, igraph, and leidenalg, then store node memberships for GraphRAG summarization.
How it connects
Connects community detection with GraphRAG, graph traversal, vector retrieval, hierarchical summarization, NetworkX, igraph, and Leiden.

Also explained in

Compare how 5 books explain community →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.