Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 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.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
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
Select an algorithm and generate steps to begin visualization
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.
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.
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.
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 communitiesThe 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.
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.
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.
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.
Community detection algorithms trade speed, quality and determinism differently.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Louvain | Large networks where good modularity matters and a hierarchy of communities is useful. | about O(E log V) |
| Leiden | The successor of Louvain: it guarantees connected communities and usually finds higher modularity. Prefer it when available. | about O(E log V) |
| Girvan-Newman | Small graphs, teaching, or a full divisive hierarchy by edge betweenness. | O(E²V) |
| Label propagation | Huge graphs where speed matters more than quality, with no objective to tune. | O(E) per round |
Related algorithms: Girvan-Newman Communities, Label Propagation, Connected Components, Betweenness Centrality