Modularity Lab

What is Modularity?

Modularity measures how good a community partition is. It compares the actual number of edges within communities to the expected number if edges were placed randomly (keeping the same node degrees). A high modularity means the community structure is stronger than random.

Important: Modularity does NOT simply count internal edges. It asks: "Are there MORE internal edges than we'd expect by chance?" This comparison to a null model (random graph with same degrees) is what makes modularity meaningful.

The Formula

Modularity Q:
Q = (1 / 2m) Σij [ Aij − kikj / 2m ] · δ(ci, cj)

Symbol Definitions

Symbol Meaning
Aij 1 if nodes i and j are connected, 0 otherwise
ki degree of node i
kj degree of node j
m total number of edges
ci community assignment of node i
δ(ci, cj) 1 if ci = cj (same community), 0 otherwise
kikj / 2m expected number of edges between i and j in a random graph with the same degree distribution
Q modularity score — positive means stronger-than-random community structure

Interpretation

  • Q ≈ 0: no better than random
  • Q > 0.3: moderate community structure
  • Q > 0.7: strong community structure
  • Q can be negative if the partition is worse than random

Interactive Modularity Lab

Community Colors: Community 0 Community 1 Community 2
Click any node to cycle its community assignment (0 → 1 → 2 → 0...)

Network Graph

Modularity Score

Q = 0.0000

Community Membership

Modularity Contribution Table

Each pair of nodes contributes to the total modularity. Positive contributions (green) come from connected nodes in the same community. Negative contributions (red) come from unconnected nodes in the same community.

Node i Node j Aij ki kj Expected Same Community? Contribution

Move One Node Experiment

Explore how moving a single node affects modularity. Select a node and see the impact of moving it to different communities.

How Louvain Algorithm Maximises Modularity

The Louvain algorithm starts with each node in its own community, then greedily moves nodes to neighbouring communities to maximise Q. This is a simplified teaching version showing the first phase only.

Louvain Animation

Step Details

Click "Run Louvain" to start the algorithm.

Note: This is a simplified teaching implementation. The full Louvain algorithm also includes a graph aggregation phase where communities become single nodes and the process repeats.