Skip to main content

Community Detection

Community detection groups nodes under a structural objective. It does not discover a unique natural truth. Modularity compares within-group edge weight with a degree-preserving null expectation, and algorithms such as Louvain heuristically search a large space of partitions.

Prerequisites​

Complete centrality and its failure modes. Be prepared to justify every conversion from the directed weighted multigraph.

Learning goals​

  • Describe the graph representation and null model behind modularity.
  • Show how the resolution parameter changes a partition.
  • Recognize the resolution limit, heuristic randomness, and near-optimal degeneracy.
  • Treat community labels as exploratory summaries rather than identities or causes.

Analysis contract​

  • Graph type: an undirected weighted simple projection; reciprocal and parallel event strengths are summed, self-loops are removed because they are events rather than pre-aggregated communities, and Gia remains an isolate.
  • Normalization: weighted modularity compares observed within-community strength with the configuration null model at declared resolution γ\gamma; no cross-resolution score comparison is treated as absolute.
  • Disconnected case: each isolate appears as a singleton, and disconnected components cannot be joined by observed edges; component-wise analysis may be clearer in larger data.
  • Interpretation limit: a partition and its score are evidence, not ground-truth groups, identity, cohesion, or causality.

Resolution and degeneracy​

For modularity, lower γ\gamma favors larger groups and higher γ\gamma favors smaller groups. The resolution limit means modularity at a fixed scale can merge small, otherwise intuitive communities. Conversely, high resolution can fragment useful structure.

Louvain is a heuristic. Node order and its seeded random shuffle can alter the returned local optimum. Many distinct partitions can have equal or nearly equal objective values—degeneracy—so a single run should not be presented as unique. Sensitivity across seeds, resolution values, graph construction, and plausible missing edges matters more than a colorful partition plot.

Worked example​

import csv
from pathlib import Path

import networkx as nx

fixture = Path("tests/fixtures/graph-data-science")
with (fixture / "interaction-nodes.csv").open(encoding="utf-8") as handle:
node_ids = [row["node_id"] for row in csv.DictReader(handle)]
with (fixture / "interaction-relationships.csv").open(encoding="utf-8") as handle:
rows = [row for row in csv.DictReader(handle) if row["record_status"] == "valid"]

graph = nx.Graph()
graph.add_nodes_from(node_ids)
for row in rows:
source, target = row["source"], row["target"]
if source == target:
continue
strength = float(row["weight"])
if graph.has_edge(source, target):
graph[source][target]["strength"] += strength
else:
graph.add_edge(source, target, strength=strength)

def ordered(partition):
return sorted((sorted(group) for group in partition), key=lambda group: (group[0], len(group)))

for resolution in (0.1, 1.0, 3.0):
partition = nx.community.louvain_communities(
graph, weight="strength", resolution=resolution, seed=53
)
print(f"resolution_{resolution:.1f}={ordered(partition)}")

Expected output:

resolution_0.1=[['ada', 'ben', 'cy', 'dia', 'eli', 'fox'], ['gia']]
resolution_1.0=[['ada', 'ben', 'fox'], ['cy', 'dia', 'eli'], ['gia']]
resolution_3.0=[['ada'], ['ben'], ['cy'], ['dia', 'eli'], ['fox'], ['gia']]

These are three answers to three objectives, not three estimates converging on one hidden label set.

Limitations​

  • The undirected projection discards direction; summing reciprocal events changes the null model.
  • Louvain can return poorly connected communities, and fixed seed gives repeatability rather than truth.
  • The resolution limit and partition degeneracy undermine claims of a uniquely correct scale.
  • Missing or erroneous edges can move nodes or merge groups; the fixture's missing relationship stays unknown.

Practice​

Run several seeds at γ=1\gamma=1 and compare partitions and modularity values. Then add one explicitly hypothetical bridge and describe the sensitivity without calling either result correct.

Next steps​

Continue to bipartite and multipartite networks before projecting data that naturally contains more than one node type.

Primary sources​

Checked 2026-10-18: NetworkX, louvain_communities; NetworkX, modularity; V. D. Blondel et al., Fast unfolding of communities in large networks; S. Fortunato and M. Barthélemy, Resolution limit in community detection.