learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Louvain Algorithm Visualizer

Greedy modularity, pass by pass

Moves each vertex into the neighbouring community that raises modularity most, then merges every community into one vertex and repeats

Time: O(E log V)
Space: O(V + E)
Use Case: Community detection, graph clustering, single-cell analysis
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Louvain Community Detection

The Louvain method is the most widely used algorithm for community detection: it splits a network into groups of vertices that are densely connected inside and sparsely connected to each other. It does so by maximizing modularity Q, which compares the number of edges inside the communities with the number expected in a random graph with the same degrees. Vincent Blondel and colleagues at the University of Louvain published it in 2008 and ran it on a mobile phone network of 2.6 million customers. It is fast, simple and finds a hierarchy of communities, which is why NetworkX, igraph and Gephi all include it.

How it works

Louvain works in passes of two phases. In the local-moving phase it goes through the vertices one by one and moves each into the neighbouring community that raises Q the most, if any move raises it. The gain of a move follows from the vertex's edges into the community and the community's total degree alone, so each move is cheap to evaluate. The sweeps repeat until no vertex moves. In the aggregation phase each community becomes a single vertex: edges inside it become a self-loop, edges between two communities one weighted edge. The next pass runs the local moving on this smaller graph, so whole communities merge at once. The method stops when a pass changes nothing. Here the vertices are taken in label order, so every run is the same.

Applications

Louvain finds groups of friends in social networks, topics in citation and co-authorship networks, functional modules in protein interaction networks and customer segments in transaction data. It is used to partition graphs for distributed computing, to detect fraud rings, and to cluster single-cell data in biology, where tools such as Seurat and Scanpy run Louvain or its successor Leiden on cell similarity graphs. Its known limits are the resolution limit of modularity, which can merge small communities, and communities that may come out internally disconnected, a flaw that the Leiden algorithm (2019) fixes.

Pseudocode

The method is two nested loops: local moving until nothing moves, then aggregation, repeated until a whole pass changes nothing.

Louvain(G):
    community[v] = v for every vertex v
    repeat:
        // phase 1: local moving
        repeat:
            for each vertex i:
                remove i from its community
                C = neighbouring community with the largest ΔQ
                put i into C (its old community if no gain)
        until no vertex moved
        // phase 2: aggregation
        G = graph with one vertex per community
    until a pass moved nothing
    return the communities

The gain of putting an isolated vertex i into community C is ΔQ = k(i,C) / m - Σtot(C) × k(i) / (2m²), with k(i,C) the weight of i's edges into C, k(i) its degree, Σtot(C) the total degree of C and m the total edge weight. It needs only local information, which is what makes each move cheap.

Worked example, step by step

Load the example: sixteen vertices in four tight groups, joined in pairs.

Example graph: A, B, C and D form a complete group of four, and so do E, F, G, H, then I, J, K, L and M, N, O, P. Five edges tie the first two groups together, five tie the last two, and a single edge C-I joins the two halves.

  1. Start. Every vertex is its own community. Q = -0.064: with no edge inside any community, modularity is negative.
  2. Pass 1, local moving. A joins B, then B moves on to C, D joins them, and A follows in the second sweep. Q rises with every move, and the other groups form the same way. After three sweeps the four groups of four are the communities, Q = 0.436.
  3. Aggregation. Each group becomes one vertex. Its six inner edges become a self-loop of weight 6, and the five edges between paired groups become one edge of weight 5.
  4. Pass 2. Now moving one vertex moves a whole group. {A, B, C, D} joins {E, F, G, H} with ΔQ = +0.018, and {I, J, K, L} joins {M, N, O, P}. Q rises to 0.471. Pass 3 finds no move that helps, so the method stops.

Two communities of eight, Q = 0.471. Local moving alone stopped at four groups, because no single vertex gains by leaving its group of four. Only the aggregated view shows that two whole groups are better together, which is why Louvain needs both phases.

Complexity, and where it comes from

Time: about O(E log V) · Space: O(V + E)

One sweep of local moving looks at every edge a constant number of times, so it costs O(E). The number of sweeps per pass is small, and each pass shrinks the graph by a large factor, so the first pass dominates the total work. Blondel and colleagues measured near-linear running times, roughly O(E log V), on networks with millions of vertices, but there is no proven polynomial bound on the number of sweeps. The graph and the community labels take O(V + E) memory.

When to use Louvain Community Detection, and when not to

Community detection algorithms trade speed, quality and determinism differently.

AlternativePrefer it whenCost
LouvainLarge networks where good modularity matters and a hierarchy of communities is useful.about O(E log V)
LeidenThe successor of Louvain: it guarantees connected communities and usually finds higher modularity. Prefer it when available.about O(E log V)
Girvan-NewmanSmall graphs, teaching, or a full divisive hierarchy by edge betweenness.O(E²V)
Label propagationHuge graphs where speed matters more than quality, with no objective to tune.O(E) per round

Common pitfalls

  • Resolution limit. Modularity cannot see communities smaller than about √(2m) edges: in a ring of many small cliques, Louvain merges neighbouring cliques. A resolution parameter γ in the formula shifts the scale of the communities it finds.
  • Order dependence. The result depends on the order in which the vertices are visited. Libraries shuffle that order, so two runs can give different communities. Run several times or fix a seed, as this visualizer does with label order.
  • Disconnected communities. A vertex that holds a community together can move away in a later sweep and leave the community split in two. Traag, Waltman and van Eck showed this in 2019 and designed the Leiden algorithm to prevent it.
  • Modularity is not ground truth. High modularity does not prove that a community structure exists: random graphs also have partitions with Q around 0.3. Compare against randomized networks before reading meaning into the groups.

Frequently asked questions

What is modularity?
Modularity Q is the fraction of edges inside communities minus the fraction expected if the edges were placed at random with the same degrees. It ranges from -0.5 to 1, and values above about 0.3 usually indicate a clear community structure.
Why does Louvain have two phases?
Moving single vertices gets stuck when a whole group would be better off elsewhere but no single member gains by leaving. Aggregating each community into one vertex lets the next pass move whole groups, which climbs past those local optima.
Is Louvain deterministic?
Only for a fixed vertex order. Most implementations shuffle the order, so results can vary between runs. Set a random seed for reproducible results, as in nx.community.louvain_communities(G, seed=42).
Should I use Louvain or Leiden?
Leiden is generally better: it is about as fast, finds partitions with equal or higher modularity, and guarantees that every community is connected. Louvain remains popular because it is simpler and available everywhere, and it is the classic method taught in courses.

Related algorithms: Girvan-Newman Communities, Label Propagation, Connected Components, Betweenness Centrality

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

Zoom Controls

44%
Nodes: 16
Edges: 35