Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Community detection

Community detection finds groups of nodes (communities) in a network that are more strongly connected inside than outside; engineers use it to detect user-groups, recommenders, anomaly clusters and modularize systems.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: where an engineer uses this.

Community detection groups nodes that interact more with each other than with the rest of the network. An engineer uses it to discover user segments for recommendation and marketing, to find functional modules in software or hardware dependency graphs, and to detect anomalous or covert groups in security and fraud analysis.

Key ideas

Start from first principles, define every term before use.

  • Graph (network): a set V of nodes (vertices) and a set E of edges (links). We use undirected simple graphs unless stated otherwise.
  • Community (cluster): a subset C ⊂ V such that nodes in C have relatively many connections among themselves and relatively few connections to V\C. This is a qualitative idea; different methods formalize it differently.
  • Adjacency matrix A: A is an n×n matrix with A_ij = 1 if there is an edge between i and j, otherwise 0 (undirected: A is symmetric).
  • Degree k_i: for node i, k_i = ∑_j A_ij. This is the number of neighbours (unitless).
  • Total number of edges m: m = (1/2) ∑_i k_i (unitless).
  • Cut(S, T): for disjoint S and T, cut(S, T) = number of edges with one end in S and the other in T.
  • Volume vol(S): vol(S) = ∑_{i ∈ S} k_i.

Formal community quality measures often used:

  • Modularity (Q): measures density of edges inside communities compared with a random graph with same degree sequence. Exact definition: Q = (1/2m) ∑_{ij} (A_ij - (k_i k_j)/(2m)) δ(c_i, c_j), where δ(c_i,c_j)=1 if i and j are in same community, else 0. Assumption: undirected unweighted graph and null model is the configuration model (expected edge probability proportional to k_i k_j).

    Derivation often asked (two-way partition): define vector s with s_i = +1 if node i is in group 1, s_i = -1 if in group 2. Define modularity matrix B with B_ij = A_ij - k_i k_j/(2m). Then you can show Q = (1/4m) s^T B s for the two-way split. Steps to derive: write δ(c_i,c_j) = (1 + s_i s_j)/2, substitute into the modularity definition and simplify.

  • Modularity matrix B: B_ij = A_ij - (k_i k_j)/(2m). Community detection by modularity maximization looks for partitions that make Q large. Exact maximization is NP-hard.

  • Edge betweenness: For an edge e, its edge betweenness is the sum over all unordered node pairs (s,t) of the fraction of shortest paths between s and t that go through e: EB(e) = ∑{s<t} σ{st}(e)/σ_{st}, where σ_{st} is number of shortest paths between s and t, and σ_{st}(e) is number of those going through e. Girvan–Newman algorithm removes edges with highest EB iteratively to split communities.

  • Conductance φ(S): φ(S) = cut(S, V\S) / min(vol(S), vol(V\S)). Low conductance means strong community.

  • Normalized cut (Ncut): Ncut(A,B) = cut(A,B)/vol(A) + cut(A,B)/vol(B). Similarly, the RatioCut objective is RatioCut(A,B) = cut(A,B)/|A| + cut(A,B)/|B|. Minimizing Ncut or related normalized objectives produces balanced partitions.

  • Graph Laplacian L: L = D - A where D is diagonal with D_ii = k_i. Normalized Laplacian L_sym: L_sym = I - D^{-1/2} A D^{-1/2}. Random-walk Laplacian L_rw = I - D^{-1} A is also common, especially for Ncut. For spectral clustering, the eigenvectors of L or L_sym are used: the second smallest eigenvector (Fiedler vector) gives a relaxed solution to balanced cut problems.

    Standard derivation asked in exams: minimizing RatioCut or Ncut is NP-hard; relax the discrete partition vector to real values and show that the relaxed problem becomes an eigenvalue problem for L or L_sym. Then use the k eigenvectors associated with the smallest eigenvalues (excluding the trivial one) and, for L_sym, row-normalize the eigenvector matrix before applying k-means to its rows to get k clusters (spectral clustering pipeline).

  • Heuristic greedy methods: Louvain method (fast modularity maximization) runs local node moves to improve modularity and then aggregates communities into super-nodes, repeating until convergence. Assumptions: modularity as objective, works well in practice on large networks.

Diagrams to draw (describe exactly):

  • For Girvan–Newman: draw two triangles connected by a single edge (label nodes 1..6). Mark the bridge edge, show its high edge-betweenness, and show removal splitting graph into two communities.
  • For spectral clustering: draw a small graph with two clear clusters; draw its Laplacian eigenvector (a 1D plot of Fiedler vector values per node), with positive vs negative sign separating clusters.

Formulas

k_i = ∑_j A_ij — degree of node i; unitless; for undirected graphs.

m = (1/2) ∑_i k_i — number of edges; unitless.

Q = (1/2m) ∑_{ij} (A_ij - (k_i k_j)/(2m)) δ(c_i,c_j) — modularity of a partition; unitless; assumes undirected graph and the configuration null model.

B_ij = A_ij - (k_i k_j)/(2m) — modularity matrix; unitless.

Q (two-way) = (1/4m) s^T B s where s_i ∈ {+1, -1} — modularity for binary split; unitless; derived using δ(c_i,c_j) = (1 + s_i s_j)/2.

EB(e) = ∑_{s<t} σ_{st}(e)/σ_{st} — edge betweenness for edge e; unitless; sums over unordered node pairs.

cut(S,T) = ∑_{i∈S, j∈T} A_ij — cut size; unitless.

vol(S) = ∑_{i∈S} k_i — volume of set S; unitless.

φ(S) = cut(S, V\S) / min(vol(S), vol(V\S)) — conductance; unitless.

Ncut(A,B) = cut(A,B)/vol(A) + cut(A,B)/vol(B) — normalized cut; unitless. RatioCut(A,B) = cut(A,B)/|A| + cut(A,B)/|B| — ratio cut; unitless.

L = D - A and L_sym = I - D^{-1/2} A D^{-1/2} — (combinatorial and normalized) Laplacians; unitless.

Worked examples

Example 1 — modularity computation (numerical, exam style) Problem: Graph has two triangles connected by a single bridge edge. Nodes 1–3 form triangle (1–2,2–3,1–3). Nodes 4–6 form triangle (4–5,5–6,4–6). There is one bridge edge (3–4). Partition S = {1,2,3} and T = {4,5,6}. Compute modularity Q.

Step 1: Count edges. Each triangle has 3 edges, plus the bridge gives m = 3 + 3 + 1 = 7.

Step 2: Compute Σ_in and Σ_tot for community S. Number of internal edges in S = 3, so Σ_in(S) = sum of A_ij for i,j ∈ S = 2 × 3 = 6 (adjacency counted for both directions). Σ_tot(S) = sum of degrees in S = k1 + k2 + k3 = 2 + 2 + 3 = 7.

Same for T: Σ_in(T) = 6, Σ_tot(T) = 7.

Step 3: Use modularity per community formula: Q = ∑_c [Σ_in(c)/(2m) - (Σ_tot(c)/(2m))^2].

Compute numbers: 2m = 14. For S: Σ_in/(2m) = 6/14 = 0.428571; (Σ_tot/(2m))^2 = (7/14)^2 = 0.25. So contribution = 0.428571 - 0.25 = 0.178571.

T contributes same, so Q = 2 × 0.178571 = 0.357142.

Final answer: Q = 0.35714 (unitless).

Example 2 — Girvan–Newman reasoned answer (exam long question model) Question: Explain the Girvan–Newman algorithm and show why it splits the two-triangle graph correctly. What is its complexity and main drawback?

Model answer (points to include):

  • Define edge betweenness and give formula EB(e) = ∑{s<t} σ{st}(e)/σ_{st}.
  • Describe algorithm: compute EB for all edges, remove the edge with maximum EB, recompute EB on the resulting graph, repeat and build a dendrogram of splits.
  • Apply to the two-triangle graph: all shortest paths between nodes in different triangles must cross the bridge edge (3–4). There are 3 × 3 = 9 unordered pairs across the two communities, each with a unique shortest path crossing that bridge, so EB(3–4) = 9 (others are smaller). Removing (3–4) separates the graph into exactly the two triangles, so GN recovers the intuitive community structure.
  • Complexity: computing EB repeatedly is expensive; with Brandes' algorithm, computing EB for all edges in an unweighted graph costs O(n·m) per recomputation, and because EB is recomputed after each edge removal the overall worst-case complexity is O(n·m^2). Main drawback: not scalable to very large graphs and biased to find small communities first.
  • Conclude with pros/cons: clear interpretation and good for small graphs, but slow for large networks.

Common mistakes

  • Using ordered pairs instead of unordered pairs when computing edge betweenness (counts differ by factor 2); state clearly which definition you use.
  • Thinking modularity maximization always finds the "correct" number of communities — modularity has a resolution limit and can merge small communities.
  • In spectral clustering, using the largest eigenvectors of L instead of the smallest (for combinatorial Laplacian) — remember the Fiedler vector is the second smallest eigenvector of L.

For your exam

Kinds of questions and what full-marks answers include:

  • Definitions: define modularity, conductance, Laplacian, edge betweenness clearly and with formulas.
  • Short notes: explain Girvan–Newman, Louvain, spectral clustering; list steps, complexity, advantages/disadvantages.
  • Derivations: derive Q = (1/4m) s^T B s for two-way split (show substitution of δ and algebra); derive relaxed eigenproblem for RatioCut/Ncut (show discrete → continuous relaxation leads to eigenvectors of L or L_sym).
  • Numericals: compute Q or conductance for a small graph (show counting of Σ_in, Σ_tot, and steps). Carry units (unitless) and factors of 2 precisely.
  • Diagrams: draw small graphs, label nodes and edges, indicate removed edges in GN and sign of Fiedler vector values for spectral split.

A full-marks answer always: (1) define terms and assumptions, (2) show formulas used, (3) show step-by-step algebra or counting, (4) comment on limitations or interpretation.

Quick check

  1. Write the modularity formula for a partition using the adjacency matrix.
  2. What is edge betweenness and which algorithm uses it to find communities?
  3. Which eigenvector of the combinatorial Laplacian is used for a two-way spectral cut and why?

Answers: Q = (1/2m) ∑{ij} (A_ij - (k_i k_j)/(2m)) δ(c_i,c_j); Edge betweenness = ∑{s<t} σ_{st}(e)/σ_{st}, used by Girvan–Newman; the Fiedler vector (second smallest eigenvector of L) because it solves the relaxed cut minimization.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?