📋 KEY INSIGHTS
- Community detection finds densely connected groups of nodes in a graph — the core unsupervised task for social networks, biological networks, and knowledge graphs.
- Modularity Q measures partition quality: the fraction of edges within communities minus the expected fraction under a random null model. Maximising Q is NP-hard, so greedy approximations (Louvain, Leiden) are used in practice.
- The Louvain algorithm achieves near-optimal modularity in O(n log n) time by iteratively moving nodes between communities and merging well-connected groups, making it practical for billion-node graphs.
- Leiden (2019) fixes Louvain’s disconnected community bug by adding a refinement phase after each merge step, guaranteeing that every community in the final partition is internally connected.
- Spectral clustering embeds nodes using the graph Laplacian’s eigenvectors, then applies k-means in the embedding space — provably optimal for planted partition graphs but O(n^3) without approximations.
- GNN-based community detection (DMoN, Graph Autoencoder) learns differentiable soft community assignments end-to-end, enabling joint optimisation of clustering and downstream tasks like link prediction.
Graphs naturally partition into communities — dense subgraphs with sparser connections between them. In social networks these are friend groups; in citation networks they are research subfields; in protein interaction networks they are functional modules. Community detection algorithms identify these structures without labels, making them one of the most widely applied graph analysis tools.
This guide covers the full community detection landscape: modularity optimisation, Louvain and Leiden algorithms, spectral methods, overlapping communities, and deep learning approaches including graph autoencoders and differentiable clustering.
Modularity, Louvain and Leiden Algorithms
Modularity Q = (1/2m) Σ_{ij} [A_{ij} – k_i k_j / 2m] δ(c_i, c_j), where A is the adjacency matrix, k_i the degree of node i, m the total edge count, and δ(c_i, c_j) equals 1 when i and j are in the same community. A partition with Q > 0.3 is considered meaningful; most real-world networks achieve Q = 0.3–0.7. Maximising Q exactly is NP-hard, so all practical algorithms use greedy approximations.
The Louvain algorithm runs in two phases per iteration: first, each node is moved to the neighbour community that most increases Q (continuing until no improvement is possible); second, each community is collapsed into a single super-node, and the two phases repeat on the coarsened graph. This hierarchical coarsening continues until Q no longer improves. Louvain scales to graphs with hundreds of millions of nodes and is the default community detection algorithm in NetworkX, igraph, and Neo4j GDS.
Leiden adds a refinement phase between the aggregation steps: after moving nodes greedily, Leiden randomly reassigns nodes within each community to explore the local partition space, then merges only the refined communities. This eliminates the pathological cases where Louvain produces communities with disconnected subgraphs and achieves consistently higher modularity on benchmark networks. The connection to our clustering guide is direct: Louvain and Leiden are graph-structured analogues of the k-means algorithm described there — both iteratively reassign cluster memberships to maximise an objective, the difference being that community detection operates on the graph adjacency structure rather than Euclidean distance.
| Algorithm | Complexity | Overlapping? | Key Strength |
|---|---|---|---|
| Louvain | O(n log n) | No | Scalable; widely deployed |
| Leiden | O(n log n) | No | Fixes disconnected communities; higher Q |
| Spectral clustering | O(n^2) – O(n^3) | No | Theoretically grounded; good on planted partition |
| BigCLAM | O(n · C) | Yes | Overlapping communities; NMF-based |
| DMoN (GNN) | O(n · d) | Soft | Differentiable; joint with downstream task |
Spectral Methods, Overlapping Communities and Deep Graph Clustering
Spectral clustering on graphs uses the normalised graph Laplacian L_sym = I – D^{-1/2} A D^{-1/2}. The k smallest eigenvectors of L_sym embed each node into R^k, and k-means on these embeddings produces the community assignment. The spectral embedding captures the global graph structure: nodes with many connections between them project to nearby points, while nodes in separate communities project to distant points.
The resolution limit is a known limitation of modularity-based methods: communities smaller than √(2m) edges tend to be merged with neighbours regardless of their actual internal density. The resolution parameter γ in generalised modularity Q_γ = Σ_{ij} [A_{ij} – γ k_i k_j / 2m] δ(c_i, c_j) controls the granularity: γ > 1 favours smaller communities, γ < 1 favours larger ones. Multi-resolution community detection (running Leiden at multiple γ values and comparing results) reveals the hierarchical community structure at different scales.
Overlapping community detection allows nodes to belong to multiple communities simultaneously — appropriate for social networks (a person can belong to work, family, and hobby communities) and biological networks (a protein can participate in multiple functional complexes). BigCLAM models community membership as a non-negative matrix factorisation problem: each node has a membership vector F_u ∈ R^C_+, and the probability of an edge between u and v is P(A_{uv}=1) = 1 – exp(-F_u · F_v). Fitting BigCLAM via gradient descent on the log-likelihood recovers overlapping communities at scale. The connection to our dimensionality reduction guide is direct: BigCLAM’s NMF formulation is a probabilistic non-negative matrix factorisation of the adjacency matrix — the same NMF algorithm described there for feature extraction applied to the graph structure.
DMoN (Deep Modularity Networks, Tsitsulin et al., 2023) learns soft community assignments via a GNN: a stack of graph convolutional layers produces node embeddings, a softmax layer produces cluster assignment probabilities C ∈ R^{n×k}, and the loss combines a differentiable modularity term and a collapse regularisation (penalising trivial solutions where all nodes are assigned to one cluster). DMoN achieves state-of-the-art on the standard community detection benchmarks (SBM, real-world networks from SNAP) and enables end-to-end optimisation of community structure jointly with downstream classification or link prediction. The connection to our GNNs guide is direct: DMoN is a GNN-based clustering model — the graph convolutional layers use the message-passing framework described there to produce community-aware node embeddings, and the differentiable clustering head is an unsupervised extension of the standard GNN classification head.
✦ SUMMARIZE THIS ARTICLE WITH AI
Standard clustering (k-means, DBSCAN — the flat-data analogues of community detection) is in our Clustering guide. GNNs (message passing and node embeddings used by DMoN and graph autoencoders) are in our GNNs guide. Dimensionality reduction (NMF for overlapping community detection; spectral embeddings) is in our Dimensionality Reduction guide. Knowledge graphs (community detection for entity grouping in Neo4j GDS) are in our Knowledge Graphs guide. Graph foundation models (Leiden clustering used for Graph RAG community summaries) are in our Graph Foundation Models guide. Graph attention networks (GAT embeddings as input to community detection) are in our Graph Attention guide.



