Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Louvain, but no community falls apart
Improves on Louvain: vertices move by a queue, and every community is refined into well-connected pieces before it is merged, so no community falls apart
Select an algorithm and generate steps to begin visualization
The Leiden algorithm finds communities, groups of vertices with many edges inside and few between, by raising the modularity Q, like Louvain. Traag, Waltman and van Eck introduced it in 2019 after showing that Louvain can return communities that are badly connected or even split in two: once Louvain merges vertices into one, it never takes them apart again, so a vertex that held a community together can leave and strand the rest. Leiden adds a refinement phase that guarantees every community is connected, and after enough runs, that no single vertex can raise Q by switching community.
Each level has three phases. Fast local moving takes vertices from a queue and moves each one to the neighbouring community, or an empty one, with the largest gain in Q. Only the neighbours of a vertex that moved go back into the queue, instead of Louvain's full sweeps. Refinement then starts every community again from single vertices and lets a vertex join a piece only when both are well connected to the rest of the community. Aggregation turns each refined piece into one vertex, but starts it in its community from the first phase, so the next level can still move a piece out. The whole method then runs again from its own result until nothing changes. The visualizer colours the communities, names each vertex's piece with a badge during refinement and draws the aggregated graph over the faded original. On the example ring Leiden ends at Q = 0.36 where Louvain stops at 0.34.
Leiden has become the default community detection method in many tools: Neo4j Graph Data Science, igraph and the leidenalg package, and scanpy, where it clusters cells in single-cell RNA sequencing. Microsoft's GraphRAG runs a hierarchical Leiden on knowledge graphs extracted from documents and summarises each community for a language model. It clusters citation networks into research fields, groups customers and accounts in fraud detection, and splits social and biological networks into modules. Like every modularity method it has a resolution limit, so very small communities can be merged into larger ones.
Three phases per level, then the whole method again from its own result.
Leiden(G):
P = every vertex in a community of its own
repeat:
P = FastLocalMove(G, P) // queue: revisit only neighbours of movers
if every community is one vertex of G: stop
Q = Refine(G, P) // merge only well-connected pieces,
// inside each community of P
G = Aggregate(G, Q) // one vertex per refined piece,
P = P on the new vertices // starting in its community of P
run again from the result until nothing changesThe refinement is what Louvain lacks. Inside each community found by local moving, every vertex starts alone again and may only join a piece that is well connected to the rest of the community. So a piece can never hang on one weak link, and when it is aggregated the community stays connected. The new vertices start in their old communities, so the next level can still move a piece out of one.
The nine-vertex ring loaded on this page.
Example graph: Edges A–B, A–C, B–C, B–E, C–I, D–E, D–F, F–G, G–H, H–I. A triangle A, B, C and a ring of six vertices round the back.
Communities {A, B, C}, {D, E, F} and {G, H, I}, Q = 0.36. Louvain stops at 0.34 on the same graph with {A, B, C}, {D, E, F, G} and {H, I}.
Time: about O(m) per level in practice · Space: O(n + m)
Fast local moving visits each vertex at least once and then only the neighbours of vertices that moved, so a level costs about the sum of the degrees, O(m), times a small number of revisits. Refinement and aggregation are linear too. The number of levels is small, often close to log n, and the repeated runs usually stop after a few rounds. As for Louvain, no useful worst-case bound is known, but Traag and colleagues measured Leiden faster than Louvain on large networks because of the queue.
Other community detection methods on the site.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Louvain | A quick partition is enough and a badly connected community would not matter. Same objective, simpler steps, up to a quarter of its communities badly connected on real networks according to the Leiden paper. | about O(m) per level |
| Label propagation | The graph is huge and speed matters more than quality. Every vertex takes the most common label of its neighbours, with no objective at all. | O(m) per round |
| Girvan–Newman | The graph is small and a whole hierarchy of splits is wanted. It removes the edge of highest betweenness again and again. | O(m²n) |
Read the full article: Community Detection in Graphs
Read the full article: Spectral Graph Theory in Machine Learning
Related algorithms: Louvain Community Detection, Girvan-Newman Communities, Label Propagation, Spectral Partitioning (Fiedler Vector), Clustering Coefficient
Built by Mohammed Islam Hadjoudj · Work with me