Graph-based clustering
Graph-based clustering represents data points as nodes in a graph and finds groups by cutting or partitioning the graph; common methods are MST-based clustering (single-link), graph cuts, and spectral clustering using the graph Laplacian.
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
Graph-based clustering turns geometric or feature similarity into a graph so we can use powerful combinatorial and linear-algebra tools. Engineers use it when data has irregular shapes, varying density, or when relationships are more naturally expressed as pairwise links (e.g., social networks, sensor networks, image segmentation).
Key ideas
Start with definitions.
- Graph (G): an ordered pair (V, E) where V is a set of vertices (nodes) and E is a set of edges. We consider undirected graphs unless said otherwise.
- Weighted graph: each edge (i, j) has a weight w_{ij} ≥ 0 representing similarity. If there is no edge, weight is 0.
- Adjacency matrix (W): matrix W with entries W_{ij} = w_{ij}. W is symmetric for undirected graphs.
- Degree (d_i) of node i: sum of weights of edges incident to i, d_i = Σ_j W_{ij}.
- Degree matrix (D): diagonal matrix with D_{ii} = d_i.
- (Unnormalized) Graph Laplacian (L): L = D − W. L is symmetric and positive semidefinite.
- Normalized Laplacian (L_sym or L_rw): common forms: L_sym = I − D^{-1/2} W D^{-1/2}, L_rw = I − D^{-1} W. For undirected graphs with nonnegative weights, the eigenvalues of L_sym lie in [0,2], and L_rw = I − P with P = D^{-1} W (the random-walk transition matrix). In all three Laplacians (L, L_sym, L_rw), the multiplicity of the eigenvalue 0 equals the number of connected components.
- Cut between sets A and B (disjoint, union subset of V): Cut(A, B) = Σ_{i in A, j in B} W_{ij}.
- Normalized cut (Ncut): Ncut(A, B) = Cut(A,B)/Vol(A) + Cut(A,B)/Vol(B), where Vol(A)=Σ_{i in A} d_i.
Important theorems and derivations universities ask for:
- Quadratic form of Laplacian: for any vector f ∈ R^{n} L's quadratic form equals half the sum of weighted squared differences: Σ_{i,j} W_{ij} (f_i − f_j)^2 = 2 f^T L f. Derivation: expand f^T L f = f^T D f − f^T W f = Σ_i d_i f_i^2 − Σ_{i,j} f_i W_{ij} f_j. Rearrangement gives the sum above. This identity shows L measures smoothness of f on the graph: small f^T L f means f is close on connected nodes.
- Multiplicity of eigenvalue 0: The number of connected components of G equals the multiplicity of the eigenvalue 0 of L. Proof sketch: For each connected component C, the indicator vector 1_C (entries 1 on C, 0 outside) satisfies L 1_C = 0; these are independent. If L x = 0, then Σ_{i,j} W_{ij} (x_i − x_j)^2 = 0 so x_i = x_j on every edge; hence x is constant on each connected component.
- Spectral clustering rationale (informal theorem): Minimizing cut-like objectives is NP-hard in general. Relaxing discrete partitioning to a real-valued optimization involving L (e.g., minimize f^T L f subject to constraints) yields an eigenproblem; the relaxed solution uses eigenvectors of L. Clustering is obtained by discretizing (e.g., sign of Fiedler vector or k-means on several eigenvectors).
Algorithms (high-level, exact steps universities expect):
- Single-link / MST clustering (graph view): Build a complete graph with weights = distances (dissimilarities); compute a Minimum Spanning Tree (MST) (Kruskal or Prim). If you start from similarities (larger = more similar), either convert them to distances (e.g., d_{ij} = 1/s_{ij} or d_{ij} = −s_{ij} after an appropriate shift) or use a Maximum Spanning Tree. To obtain k clusters, remove the k−1 largest edges from the MST (for distance weights). This is equivalent to single-link hierarchical clustering when weights are distances.
- Spectral clustering (Ng–Jordan–Weiss variant): Given W and desired k:
- Build similarity matrix W (symmetric, non-negative).
- Compute D and L_sym = I − D^{-1/2} W D^{-1/2}.
- Compute the k smallest eigenvectors of L_sym and form matrix U ∈ R^{n×k} with those eigenvectors as columns.
- Normalize rows of U to unit length.
- Apply k-means to rows of U; cluster assignments are the result. Alternative (Shi–Malik/L_rw): solve the generalized eigenproblem L u = λ D u (equivalently, use the k eigenvectors for the smallest eigenvalues of L_rw = I − D^{-1} W), stack them as U, and apply k-means to rows (row-normalization is not required for L_rw, though it is often still used). A common way to choose k is the eigengap heuristic: inspect the sorted eigenvalues and pick k where a large gap appears.
Assumptions and cautions: spectral methods assume the similarity matrix reflects cluster structure; normalizing rows helps when node degrees vary. For MST-based clustering, single-link finds elongated clusters but is sensitive to chaining and noise.
Diagrams to draw for exam answers:
- To explain MST clustering, draw 4 points in the plane with one distant outlier, label points, draw all pairwise edges with weights (numbers), then draw MST (solid) and the edge removed to form clusters (dashed). Label the removed edge and resulting clusters.
- For spectral clustering, draw a small graph with two dense subgraphs weakly connected; show adjacency, degree, Laplacian matrix block structure; indicate eigenvector signs splitting the graph.
Formulas
d_i = Σ_j W_{ij} — d_i is degree of node i (unit: same as weight units, e.g., dimensionless similarity or 1/m if weight=1/distance). Applies to weighted graphs.
L = D − W — unnormalized Laplacian; D diagonal degree matrix, W adjacency matrix.
L_sym = I − D^{−1/2} W D^{−1/2} — symmetric normalized Laplacian; I is identity.
f^T L f = 1/2 Σ_{i,j} W_{ij} (f_i − f_j)^2 — quadratic form; f any real vector; shows smoothness.
Cut(A,B) = Σ_{i∈A, j∈B} W_{ij} — cut value between sets A and B.
Ncut(A,B) = Cut(A,B)/Vol(A) + Cut(A,B)/Vol(B) — normalized cut; Vol(A)=Σ_{i∈A} d_i.
Note: weights W_{ij} can be computed from features x_i via a kernel such as W_{ij} = exp(−||x_i − x_j||^2 / (2σ^2)) or using k-nearest neighbors. Units depend on how you compute similarity (often unitless).
Worked examples
Example 1 — MST / single-link clustering (numerical, coordinates in metres) Given four points (units: metres): p1=(0,0), p2=(1,0), p3=(0,1), p4=(5,5). We use Euclidean distance as edge weight (lower = closer). Steps:
- Compute pairwise distances (d_{ij}): d_{12} = sqrt((1−0)^2 + (0−0)^2) = 1.00 m d_{13} = sqrt((0−0)^2 + (1−0)^2) = 1.00 m d_{23} = sqrt((1−0)^2 + (0−1)^2) = sqrt(2) = 1.41421356 m d_{14} = sqrt((5−0)^2 + (5−0)^2) = sqrt(50) = 7.07106781 m d_{24} = sqrt((5−1)^2 + (5−0)^2) = sqrt(16+25)=sqrt(41)=6.40312424 m d_{34} = sqrt((5−0)^2 + (5−1)^2) = sqrt(25+16)=sqrt(41)=6.40312424 m
- Build complete graph with these weights. Compute MST using Kruskal (pick smallest edges first, avoid cycles): order edges by weight: (1,2)=1.00, (1,3)=1.00, (2,3)=1.414, (2,4)=6.403, (3,4)=6.403, (1,4)=7.071.
- Pick (1,2) — connect p1 and p2.
- Pick (1,3) — connect p3 to the component {p1,p2} (no cycle).
- Now all first three are connected; next smallest edge (2,3) would create cycle — skip.
- Next pick smallest to connect p4 is (2,4) with 6.403 m. MST edges: (1,2)=1.00 m, (1,3)=1.00 m, (2,4)=6.40312424 m.
- For k=2 clusters, remove the largest edge of MST: remove (2,4)=6.40312424 m. Resulting clusters: {p1,p2,p3} and {p4}. Final answer: Clusters: {p1,p2,p3} and {p4}.
Example 2 — Spectral clustering on a small graph (exact algebraic example) Consider 4 nodes with similarity matrix W (unitless): W = [[0,1,0,0], [1,0,0,0], [0,0,0,1], [0,0,1,0]]. This represents two disconnected pairs (1–2) and (3–4).
- Degree matrix D = diag(1,1,1,1).
- Unnormalized Laplacian L = D − W = [[1,−1, 0, 0], [−1,1,0,0], [0,0,1,−1], [0,0,−1,1]].
- Solve L x = 0. There are two independent solutions (indicator vectors): x^{(1)} = [1,1,0,0]^T and x^{(2)} = [0,0,1,1]^T. Therefore multiplicity of eigenvalue 0 is 2, matching two connected components.
- Spectral clustering with k=2 uses the first two eigenvectors (which are these indicators). Row-wise k-means groups rows 1–2 together and 3–4 together. Final answer: Clusters: {1,2} and {3,4}.
Common mistakes
- Using unnormalized Laplacian with widely varying degrees without normalization — this biases cuts toward low-degree nodes.
- In MST clustering, removing the smallest instead of the largest MST edges when forming clusters (you must remove largest edges to split components).
- Interpreting Fiedler vector components directly without thresholding or k-means; for k>2 use k eigenvectors and clustering on rows.
- Not checking units when similarity uses distance (e.g., mixing metres and millimetres without conversion).
For your exam
Question types you will see: definitions and short notes (define Laplacian, cut, Ncut), derivations (derive quadratic form f^T L f equality, prove multiplicity-of-zero-eigenvalue statement), algorithm descriptions (steps of spectral clustering or Kruskal for MST), small numericals (compute MST and clusters for given points), and explanation/diagram questions (draw MST and cuts or graph showing Fiedler vector sign split). What a full-marks answer includes:
- Precise definitions using bolded terms (graph, Laplacian, cut).
- Clear assumptions (graph undirected, weights non-negative).
- Key identities shown step-by-step (derive quadratic form; show L 1_C = 0 for component indicator).
- For algorithms: give ordered steps, complexity (e.g., spectral clustering dominated by eigen-decomposition O(n^3) naive, MST O(E log V)), and mention practical choices (sparsify W, k-NN or ε-neighborhood), and how to choose k (eigengap heuristic).
- For numericals: show all distance/similarity calculations, MST steps (edge order), and final clusters.
Quick check
- Define the graph Laplacian L in one line.
- State what the multiplicity of eigenvalue 0 of L equals.
- In MST clustering, to form k clusters from MST you remove which edges? Answers: 1) L = D − W (D degree matrix, W adjacency). 2) The number of connected components. 3) Remove the k−1 largest edges of the MST.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?