Community detection is the task of partitioning the nodes of a network into groups that are densely connected internally and only sparsely connected to the rest of the graph. Also called graph clustering or network community detection, it is one of the oldest unsupervised problems in graph learning and a common preprocessing step for analytics and machine learning on graphs.
A community can represent a circle of friends, a functional protein complex, a topic cluster of web pages or a coordinated fraud ring. Because there is no single correct definition of a community, the field offers several objective functions and algorithms, each encoding a different idea of what a good group looks like.
What makes a group of nodes a community
The intuitive definition compares internal edge density with external edge density: members link to each other more often than to outsiders. Formal definitions turn this intuition into a measurable quantity, and the chosen quantity strongly shapes the result.
The most widely used measure is modularity, usually written Q. It compares the fraction of edges inside communities with the fraction expected in a random graph with the same degree sequence. Values range from about -0.5 to 1, and partitions of real networks with clear structure typically score between 0.3 and 0.7. Higher modularity means more edges fall inside groups than chance would predict.
Other criteria emphasize different properties, as the following table shows.
| Criterion | What it measures | Typical use |
|---|---|---|
| Modularity | Internal edges versus a random baseline | General-purpose partitioning |
| Conductance | Edges leaving a group relative to its volume | Finding well-separated local clusters |
| Normalized cut | Balanced cost of cutting between groups | Spectral clustering |
| Map equation | Description length of a random walk | Flow-based networks |
| Likelihood | Fit of a generative block model | Statistical inference |
Main families of community detection algorithms
Algorithms differ in how they search for good partitions and in which criterion they optimize. Exact optimization of modularity is NP-hard, so practical methods rely on greedy heuristics, spectral relaxations or statistical inference.
| Algorithm | Approach | Scalability | Main caveat |
|---|---|---|---|
| Louvain | Greedy modularity merging in levels | Millions of edges | Can create disconnected communities |
| Leiden | Louvain with a refinement phase | Millions of edges | Still bound to modularity limits |
| Label propagation | Nodes adopt the most common neighbor label | Near-linear time | Unstable across runs |
| Spectral clustering | Eigenvectors of the graph Laplacian | Moderate graphs | Needs the number of groups |
| Infomap | Minimizes the map equation | Large graphs | Sensitive to edge directions |
| Stochastic block model | Bayesian fit of group connectivity | Moderate graphs | Higher compute cost |
The Leiden algorithm is now the common default for large undirected graphs because it guarantees connected communities and converges faster than Louvain. Stochastic block models are preferred when the analyst needs principled model selection or wants to detect structures other than dense clusters, such as core-periphery or bipartite patterns.
Community detection in graph learning pipelines
In modern graph machine learning, community detection plays three distinct roles: it is a task in its own right, a source of features, and a tool for scaling training.
- Unsupervised clustering with embeddings: node embeddings from DeepWalk or node2vec can be clustered with k-means, which often recovers communities because random walks stay inside dense regions.
- Learned clustering: graph neural networks can be trained with a differentiable modularity or cut objective, producing soft community assignments.
- Graph partitioning for training: Cluster-GCN splits a large graph into dense clusters so that mini-batches keep most edges, which reduces memory use.
- Features for downstream models: community membership and community size often improve node classification and link prediction.
The relationship with homophily is central. When connected nodes tend to share labels, communities align with classes and simple methods such as label propagation work well. Under heterophily, dense groups may mix classes, and community features become less useful.
Choosing a method for a given network
The right method depends on graph size, on whether communities may overlap, and on whether the goal is exploration or statistical evidence. The decision table below summarizes common situations.
| Situation | Recommended choice | Reason |
|---|---|---|
| Large undirected social graph | Leiden | Fast, connected, well tested |
| Nodes belong to several groups | Overlapping methods such as BigCLAM | Hard partitions hide overlap |
| Directed flows, such as citations | Infomap | Models the flow of a random walk |
| Small graph, need uncertainty | Bayesian stochastic block model | Selects the number of groups |
| Graph changes over time | Dynamic or incremental methods | Tracks splits and merges |
Limitations and common misinterpretations
Community detection always returns an answer, even on a random graph with no real structure. Modularity optimization on random graphs can produce surprisingly high scores, so results should be compared with a null model or a significance test before interpretation.
- Resolution limit: modularity cannot detect communities smaller than a scale set by the total number of edges, so small groups merge. A resolution parameter partly addresses this.
- Degeneracy: many very different partitions can have nearly the same modularity, so a single run is not a stable answer.
- Evaluation without ground truth: metadata such as departments or labels may not match structural communities; low agreement does not automatically mean the algorithm failed.
- Randomness: greedy algorithms depend on node order and seeds, so consensus clustering over many runs is advisable.
When ground truth exists, results are compared with normalized mutual information or the adjusted Rand index. Both measures should be read together with the number and size of detected groups.
Stripped to the essentials
Picture a big map of people and their friendships. Some people know each other very well and form tight groups. These groups have only a few links to other groups.
Community detection is a way for computers to find those groups automatically. It looks at who is connected to whom and draws borders around the dense clusters. Nobody has to label the groups in advance.
The key point is simple. A group counts as a community when its members are far more connected to each other than chance would explain.
Where it is applied
A payments company builds a graph in which accounts are nodes and shared devices, card numbers or shipping addresses are edges. The graph holds about 5 million accounts and 20 million edges, and analysts suspect organized fraud rings hide inside it.
The team runs the Leiden algorithm with a raised resolution parameter, which produces many small communities. They then flag groups of 5 to 50 accounts with unusually high internal density and a high share of recent chargebacks. Community size, density and chargeback rate become features for a fraud classifier.
Investigators review the top-ranked groups and confirm several coordinated rings that individual account rules had missed. The main gain is earlier detection of whole networks of accounts rather than one account at a time.