learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Leiden Algorithm Visualizer

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

Time: O(E log V)
Space: O(V + E)
Use Case: Communities in large networks, single-cell clustering, citation and social networks, graph databases
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Leiden Community Detection

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.

How it works

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.

Applications

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.

Pseudocode

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 changes

The 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.

Worked example, step by step

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.

  1. Fast local moving. Every vertex starts alone, Q = −0.115. A joins B, C joins them, D joins E, F joins G and H joins I. After five moves no single vertex can raise Q: 4 communities, Q = 0.32.
  2. Refinement. Each of the four communities starts apart again and rebuilds itself. Every one is a single well-connected piece, so nothing is split here, which is the usual case on a small clean graph.
  3. Aggregation and level 2. The four pieces become four vertices. Moving the piece D, E into the community F, G gains 0.02: 3 communities, Q = 0.34. Level 3 changes nothing.
  4. Second run. The method starts again from single vertices, keeping these communities. Now G is better off with H and I: moving it gains 0.02, Q = 0.36. Louvain merged G with F, D and E and can never move it again.
  5. Third run. Nothing moves, which proves that no single vertex can raise Q by switching community.

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}.

Complexity, and where it comes from

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.

When to use Leiden Community Detection, and when not to

Other community detection methods on the site.

AlternativePrefer it whenCost
LouvainA 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 propagationThe 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–NewmanThe graph is small and a whole hierarchy of splits is wanted. It removes the edge of highest betweenness again and again.O(m²n)

Common pitfalls

  • Stopping after the first run. The first run stops at Q = 0.34 here, exactly where Louvain stops. Only the second run moves G and reaches 0.36. Libraries iterate until stable or for a set number of iterations: keep that number above one.
  • Expecting the same answer every time. Real implementations visit the vertices in random order and refine randomly, so two runs can return different partitions of similar Q. Fix the seed to reproduce a result, and compare several runs before reading meaning into a small community.
  • Trusting modularity for small groups. Modularity has a resolution limit: in a large network it merges small dense groups into bigger ones. Raise the resolution parameter, or use the constant Potts model that Leiden also supports, when small communities matter.

Frequently asked questions

How can Louvain return a community that falls apart?
Louvain merges each community into one vertex and never splits it. If a vertex that linked two halves of a community later moves to another community, the two halves stay merged but are no longer connected. Leiden refines every community before merging it, so this cannot happen.
Does Leiden find the best possible modularity?
No. Maximising modularity is NP-hard. Leiden guarantees connected communities, and once a run changes nothing, that no single vertex can raise Q by switching community. In practice it usually finds a higher Q than Louvain, as on this page.
Why does GraphRAG use Leiden?
GraphRAG builds a knowledge graph from documents, runs a hierarchical Leiden to group the entities into communities at several levels, and asks a language model to summarise each community. Well-connected communities make coherent summaries, and the hierarchy lets it answer both broad and narrow questions.

Related algorithms: Louvain Community Detection, Girvan-Newman Communities, Label Propagation, Spectral Partitioning (Fiedler Vector), Clustering Coefficient

Built by Mohammed Islam HadjoudjWork with me

An example graph: you can change it, or start blank.

Zoom Controls

31%
Nodes: 9
Edges: 10