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.

Clustering in social networks

Clustering groups nodes into communities in a social network; engineers use it to detect functional groups, compress graphs, recommend connections and find spread boundaries.

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

Clustering (or community detection) finds groups of nodes that are more connected to each other than to the rest of the network. Engineers use clustering to detect user communities, compress and index large social graphs, improve recommendations, find groups for targeted analytics, and to limit the spread of misinformation or failures.

Key ideas

Start from simple graph terms:

  • Graph: a pair G = (V, E) where V is the set of vertices (nodes) and E the set of edges (links). Undirected graphs are assumed unless stated otherwise.
  • Degree k_i: the number of edges incident to vertex i. (Defined before use.)
  • Induced subgraph: given S ⊆ V, the induced subgraph is the graph containing vertices S and all edges among S present in G.
  • Community / Cluster: a subset of vertices S such that vertices in S have more or stronger connections among themselves than with V\S. This is a qualitative definition; a quantitative definition requires a scoring function (modularity, conductance, etc.).

Clustering measures and definitions commonly used in exams:

  • Local clustering coefficient C_i: measures how close the neighbors of i are to being a clique. Exact definition: if node i has degree k_i ≥ 2 and there are t_i edges among its neighbors, then C_i = 2 t_i / (k_i (k_i - 1)). Assumption: simple undirected graph (no self-loops, no multi-edges).

  • Global clustering (transitivity): counts triangles relative to connected triples. If T is the number of triangles and Τ is the number of connected triples (three nodes with at least two edges, counted centered on the middle node), then Global clustering = 3 T / Τ. Relation: the global clustering is not the same as the average of local C_i; both are used and differ when degrees are heterogeneous.

  • Modularity Q (Newman–Girvan modularity): a widely used quality function for partitions. For partitioning nodes into communities c_i, modularity is Q = (1 / (2m)) \sum_{i,j} [A_{ij} - (k_i k_j)/(2m)] · δ(c_i, c_j), where A_{ij} is adjacency (1 if edge exists, 0 otherwise), k_i degree of node i, m number of edges, and δ(c_i, c_j) = 1 if nodes i and j are in same community else 0. This compares observed edges to expected edges under the configuration-model null model. Assumptions: undirected graph; observed graph is simple (A_{ii} = 0); null model: configuration model with P_{ij} = (k_i k_j)/(2m), and the modularity sum is over all i,j (including i=j).

    A common simplified expression used in derivations: for each community C let l_C be number of internal edges and d_C be sum of degrees of nodes in C. Then Q = \sum_C [ (l_C / m) - (d_C / (2m))^2 ]. Universities often ask to derive this from the pairwise definition.

  • Edge betweenness: for an edge e, its betweenness is the number of shortest paths between all pairs of nodes that run along e (if multiple shortest paths exist, they are counted fractionally). This is used by the Girvan–Newman algorithm.

Algorithms — what to know precisely:

  • Girvan–Newman (divisive): compute edge betweenness for all edges, remove the highest-betweenness edge(s), recompute betweenness, repeat until all edges removed. Record partition sequence and choose partition that optimises a score (often modularity). Key steps to draw for an exam: draw small graph, mark shortest paths, count how many go through an edge, remove edge with highest count, redraw components. Time complexity: O(n m^2) for unweighted graphs (using Brandes’ algorithm to recompute betweenness at each step), so it does not scale to large networks.

  • Louvain method (local greedy + aggregation): iteratively move nodes to neighbouring communities if modularity increases, collapse communities to super-nodes, repeat. Important formula: change in modularity when moving node i to community C depends on node's incident edges to C, the total degree of C and global m — use the modularity difference formula in exams (see Formulas section). Assumptions: works for weighted and unweighted graphs. Typical complexity: near-linear in the number of edges per pass; fast in practice but heuristic with no optimality guarantee.

  • Spectral clustering (graph cut view): use Laplacian L = D - A or normalized Laplacian L_sym = D^{-1/2} L D^{-1/2}, compute first k eigenvectors (smallest non-zero eigenvalues), form matrix of those eigenvectors, treat each node as a point in R^k (row vector), and run k-means to get clusters. Draw: adjacency matrix reordered by cluster should show block-diagonal appearance. Complexity: dominated by eigen-decomposition (O(n^3) for dense matrices; much less with sparse methods for the k smallest eigenpairs) plus k-means.

Limitations and practical points: modularity has a known resolution limit (can miss small communities inside large networks). Hierarchical and overlapping communities require specialized methods (not re-taught here — see Community detection topic). Also clustering is NP-hard in general if phrased as optimizing many objective functions exactly.

Formulas

C_i = 2 t_i / (k_i (k_i - 1)) — C_i: local clustering coefficient of node i (dimensionless); t_i: number of edges among neighbors of i (unitless count); k_i: degree of i (count). Applies for k_i ≥ 2.

GlobalClustering = 3 T / Σ_i [k_i (k_i - 1) / 2] — T: number of triangles (count); k_i: degree of node i. Dimensionless; applies to undirected simple graphs.

Q = (1 / (2 m)) Σ_{i,j} [A_{ij} - (k_i k_j)/(2 m)] δ(c_i,c_j) — Q: modularity (dimensionless); A_{ij}: adjacency (0/1); k_i: degree; m: number of edges. Applies for partitioning undirected graphs.

Q = Σ_C [ (l_C / m) - (d_C / (2 m))^2 ] — l_C: number of edges inside community C; d_C: sum of degrees in C; m: total edges. Dimensionless.

(Change of modularity when moving node i of degree k_i to community C — common form) ΔQ = [ (k_{i,in} / m) - (k_i d_C) / (2 m^2) ] — k_{i,in}: sum of weights (or number) of edges from i to nodes in C; d_C: sum of degrees in C. Dimensionless; used in greedy methods like Louvain. (Verify sign and constant factors for the exact implementation used.)

L = D - A — L: graph Laplacian (matrix); D: diagonal degree matrix; A: adjacency matrix. Units: dimensionless entries. Used in spectral clustering.

L_sym = D^{-1/2} L D^{-1/2} — normalized Laplacian (matrix).

Worked examples

Example 1 — compute local and global clustering for a small graph: Graph: nodes {1,2,3,4,5,6} with edges: (1-2,1-3,2-3) forming triangle; (4-5,5-6,4-6) forming triangle; and a bridge edge (3-4). Step-by-step:

  1. Count edges m = 3 + 3 + 1 = 7 (unitless). Degrees: k1=2,k2=2,k3=3,k4=3,k5=2,k6=2.
  2. Local clustering coefficients: t1=1 (neighbors 2 and 3 connected) => C1 = 2·1/(2·1)=1. C2=1. For node3: neighbors {1,2,4}, edges among neighbors = 1 (edge 1-2) => C3 = 2·1/(3·2)=1/3 ≈ 0.3333. Similarly C4 = 1/3, C5=1, C6=1.
  3. Average local clustering = (1 + 1 + 0.3333 + 0.3333 + 1 + 1)/6 = 4.6666/6 = 0.7778 (dimensionless).
  4. Triangles T = 2. Number of connected triples = Σ_i k_i(k_i-1)/2 = 10. Global clustering = 3 T / triples = 6/10 = 0.6 (dimensionless).

Example 2 — modularity of partition into two triangles: Using same graph and partition: C1 = {1,2,3}, C2 = {4,5,6}. We compute l_C and d_C:

  • For C1: l1 = 3 (internal edges), d1 = k1+k2+k3 = 2+2+3 = 7.
  • For C2: l2 = 3, d2 = 3+2+2 = 7. m = 7. Compute Q = Σ_C [ (l_C / m) - (d_C / (2m))^2 ] Term for C1 = 3/7 - (7/(14))^2 = 0.428571 - 0.25 = 0.178571. Same for C2. So Q = 2·0.178571 = 0.357142 (dimensionless). Check: single-community partition would give Q = 1 - 1 = 0, so this partition has positive modularity.

Example 3 — model answer (theory question): "Explain Girvan–Newman algorithm and how modularity is used to choose the best partition." Model answer should explain edge betweenness definition, iterative removal, recomputation, and that at each removal step the network splits into components; compute modularity of the partition at each step and select the partition with highest Q. Time complexity: recomputing betweenness after each removal yields roughly O(n m^2) for unweighted graphs (Brandes’ algorithm), so GN is costly on large networks; limitations include computational cost and that choosing by modularity inherits the resolution limit.

Common mistakes

  • Using expected edge count k_i k_j/(2m) but forgetting that it comes from the configuration-model null model assumption.
  • For Girvan–Newman: counting shortest paths incorrectly (must split ties fractionally) or forgetting to recompute betweenness after each removal.
  • For Louvain: using a wrong ΔQ sign or missing the (2m) normalisation.

For your exam

Question types you will see: definitions (define modularity, local clustering coefficient), short notes (compare algorithms: Girvan–Newman vs Louvain), derivations (derive simplified modularity form from pairwise definition), numericals (compute C_i, global clustering, compute Q for a given partition), algorithms (write steps or pseudocode for Girvan–Newman, Louvain or spectral clustering), and diagram questions (reorder adjacency matrix to show blocks, draw betweenness computation for an edge).

What full-marks answers include:

  • Definitions with symbols clearly stated and assumptions (undirected, simple graph).
  • Derivations shown step-by-step (for modularity show substitution of A_{ij} and expectation and collect terms by community).
  • Numerical answers with intermediate steps, units (dimensionless), and final bold value.
  • For algorithms: clear ordered steps, complexity statement, and short note on limitations.

Quick check

  1. Write the formula for local clustering coefficient C_i and explain symbols.
  2. State the modularity Q pairwise definition and what null model it uses.
  3. List the main steps of Girvan–Newman algorithm. Answers: 1) C_i = 2 t_i / (k_i (k_i - 1)), t_i edges among neighbours, k_i degree. 2) Q = (1/(2m)) Σ_{i,j} [A_{ij} - (k_i k_j)/(2m)] δ(c_i,c_j), null model: configuration model (expected edges k_i k_j/(2m)). 3) Compute edge betweenness, remove highest, recompute betweenness, repeat and record partition sequence (choose best by modularity).

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

Stuck on something here?