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.

Spectral clustering

Spectral clustering groups data by building a similarity graph, using the graph Laplacian and its eigenvectors to embed points, then clustering in that embedding (commonly with k-means).

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

Spectral clustering is used by engineers when clusters are not spherical or when cluster shapes are complex (non-convex), e.g. image segmentation, social networks, sensor networks and graph partitioning. It converts a clustering problem into a graph and uses linear algebra (eigenvectors of the graph Laplacian) to find a good partition.

Key ideas

Start from first principles and define terms you will use.

  • Similarity graph: a graph whose nodes are the data points x1,...,xn and whose edges encode pairwise similarity. You must choose a rule to build it (see below).
  • Adjacency matrix (W): the n×n matrix with entries w_{ij} giving the similarity between xi and xj. By convention w_{ii}=0. W is symmetric for undirected graphs.
  • Degree matrix (D): the diagonal n×n matrix with d_i = sum_j w_{ij} on the diagonal.
  • Unnormalized graph Laplacian (L): L = D - W. This is symmetric and positive semi-definite.
  • Normalized Laplacians: the symmetric normalized Laplacian L_sym = I - D^{-1/2} W D^{-1/2} and the random-walk normalized Laplacian L_rw = I - D^{-1} W.
  • Rayleigh quotient: for a vector f ∈ R^n, R(f) = (f^T L f)/(f^T f). Minimizing Rayleigh quotients leads to eigenvalue problems.
  • Indicator vector: for a set S of nodes, the vector 1_S has 1 for nodes in S and 0 otherwise. Exact clustering is a combinatorial search over indicator vectors; spectral methods relax this to real vectors.

Key theoretical facts (state them exactly in exams):

  1. L is symmetric and positive semi-definite. Assumptions: W is symmetric with non-negative entries, D defined as above.

    Proof sketch: for any f ∈ R^n, f^T L f = 1/2 sum_{i,j} w_{ij} (f_i - f_j)^2 ≥ 0. Therefore eigenvalues are ≥ 0.

  2. Multiplicity of eigenvalue 0: The number of connected components of the graph equals the multiplicity of the eigenvalue 0 of L. Assumption: graph is undirected with non-negative weights.

    Reason: If the graph has c disjoint connected components, then indicator vectors of components are linearly independent and satisfy L 1_S = 0.

  3. Relaxation to eigenproblem: The combinatorial goal (for one cut) minimize f^T L f subject to constraints on f leads to a generalized eigenproblem L f = λ D f. Typical derivation

    • Start with discrete objective for RatioCut or Normalized Cut (Ncut). Example: minimize f^T L f subject to f^T D f = 1 (a normalization). Replacing discrete indicator constraints with continuous f ∈ R^n leads to solving the generalized eigenproblem L f = λ D f.

    • For normalized Laplacian: solve (I - D^{-1/2} W D^{-1/2}) u = λ u; then x = D^{-1/2} u gives generalized-eigenvector form.

  4. Cheeger-type inequality (normalized Laplacian): For the normalized Laplacian, the second smallest eigenvalue λ2 is related to the best possible normalized cut (Cheeger constant) h(G) by h(G)^2/2 ≤ λ2 ≤ 2 h(G). Assumptions: undirected graph, definitions of h(G) as the minimal normalized cut over vertex subsets. (This is a standard inequality; check constants in your course.)

Algorithm (standard spectral clustering for k clusters — give as steps to write in exams):

  1. Build a similarity graph on data: choices are (a) k-nearest neighbours graph (symmetrize if needed), (b) ε-neighbourhood graph, (c) fully connected graph with a kernel (e.g. Gaussian).
  2. Form W (adjacency) and D (degree).
  3. Choose Laplacian: unnormalized L = D − W, or normalized L_sym = I − D^{-1/2} W D^{-1/2} (recommended when degrees vary).
  4. Compute the first k eigenvectors of chosen Laplacian (smallest eigenvalues; for L_sym eigenvectors u1,...,uk). Stack these vectors as columns to form n×k matrix U.
  5. If using L_sym, row-normalize U (each row to unit length) to get matrix Y. For L_rw you may use eigenvectors of generalized problem and then use k-means directly on rows.
  6. Run k-means on the rows of Y (each row is the embedding of a node); assign each original point to the corresponding cluster.

Why this works: eigenvectors from the Laplacian provide a low-dimensional embedding that respects graph connectivity; the relaxed minimization of Ncut/RatioCut yields these eigenvectors.

Diagram to draw for exam: Draw 6 points in 2D that form two moons. Draw edges between nearby points (thickness proportional to weight). Below, draw a table with W (6×6 partial), D diagonals, L = D − W. Beside it sketch eigenvector u2 plotted as node colours (positive vs negative) to show partition.

Formulas

w_{ij} = s(x_i, x_j) — similarity between points xi and xj (unitless). Typical choice below. w_{ij} = exp(-||x_i - x_j||^2 / (2 σ^2)) — Gaussian (RBF) kernel; ||·|| is Euclidean distance (SI: metres if data is in metres). Applies to fully connected graphs. d_i = sum_{j=1}^n w_{ij} — degree of node i (same units as w, unitless here). D = diag(d_1, ..., d_n) — degree matrix (unitless). L = D - W — unnormalized Laplacian (unitless). Applies to undirected graphs. L_sym = I - D^{-1/2} W D^{-1/2} — symmetric normalized Laplacian (unitless). Applicable when D is invertible for nodes with non-zero degree. L_rw = I - D^{-1} W — random-walk normalized Laplacian (unitless). Applies to nodes with non-zero degree. f^T L f = 1/2 sum_{i,j} w_{ij} (f_i - f_j)^2 — quadratic form, used in proofs; f ∈ R^n. Rayleigh(f) = (f^T L f) / (f^T f) — used to characterise eigenvalues of symmetric L. Generalised eigenproblem: L u = λ D u — arises from relaxed cut minimisation (unitless quantities). Definitions: cut(S, S̄) = sum_{i∈S, j∈S̄} w_{ij}; vol(S) = sum_{i∈S} d_i; RatioCut(A1,...,Ak) = sum_{l=1}^k cut(Al, Āl)/|Al|; Ncut(A1,...,Ak) = sum_{l=1}^k cut(Al, Āl)/vol(Al).

Notes on symbols: xi are data vectors (SI: metre if they represent positions); w_{ij}, d_i, entries of L are unitless similarity/weights.

Worked examples

Example 1 — small weighted graph (numerical, show eigenvector method for 2 clusters)

Data: a graph of 4 nodes with weights (unitless): W = [ [0, 1, 0.1, 0.1], [1, 0, 0.1, 0.1], [0.1, 0.1, 0, 1], [0.1, 0.1, 1, 0] ]

Step 1: degrees d1 = 1 + 0.1 + 0.1 = 1.2; similarly d2 = 1.2, d3 = 1.2, d4 = 1.2. So D = diag(1.2,1.2,1.2,1.2).

Step 2: L = D − W = [ [1.2, -1, -0.1, -0.1], [-1, 1.2, -0.1, -0.1], [-0.1, -0.1, 1.2, -1], [-0.1, -0.1, -1, 1.2] ].

Step 3: check f = [1,1,-1,-1]^T. Compute L f (component-wise shown for node 1): L f at node1 = 1.21 - (11 + 0.1*(-1) + 0.1*(-1)) = 1.2 - (1 -0.1 -0.1) = 1.2 - 0.8 = 0.4. For node3: L f at node3 = 1.2*(-1) - (1*(-1) + 0.11 + 0.11) = -1.2 - (-1 + 0.1 +0.1) = -1.2 - (-0.8) = -0.4. Thus L f = 0.4 f, so f is an eigenvector with eigenvalue 0.4.

Step 4: The smallest eigenvalue is 0 (constant vector [1,1,1,1]). The second smallest is 0.4 with eigenvector f above. Use sign of entries of second eigenvector to partition: nodes with positive entries {1,2} and negative {3,4}.

Final answer: clusters are {1,2} and {3,4}. (Bold indicates the final cluster sets.)

Example 2 — 1D points and Gaussian similarity (show building W and idea of embedding)

Points on a line (positions in metres): x1=0.0 m, x2=0.1 m, x3=0.2 m, x4=5.0 m, x5=5.1 m, x6=5.2 m. Choose σ = 0.5 m.

Step 1: compute a few Gaussian similarities w_{ij} = exp(- (Δx)^2 / (2 σ^2)). Example distances and weights (approx):

  • between x1 and x2: Δ = 0.1 ⇒ w_12 ≈ exp(-0.01/(0.5)) = exp(-0.02) ≈ 0.9802.
  • between x1 and x3: Δ = 0.2 ⇒ w_13 ≈ exp(-0.04/0.5) = exp(-0.08) ≈ 0.9231.
  • between x1 and x4: Δ = 5.0 ⇒ w_14 ≈ exp(-25/0.5) = exp(-50) ≈ ~1.9e-22 (≈0).
  • similarly x4 and x5: Δ = 0.1 ⇒ w_45 ≈ 0.9802.

Step 2: W is approximately block-diagonal (high weights inside {x1,x2,x3} and {x4,x5,x6}, negligible between blocks). Build D and L.

Step 3: compute the second eigenvector of L_sym or L (in exam you would show the idea). The eigenvector values will be positive for {x1,x2,x3} and negative for {x4,x5,x6} (or vice versa). Run k-means on the 1D embedding (rows of eigenvector) to get two clusters.

Final answer: clusters are {x1,x2,x3} and {x4,x5,x6}.

Note: In exam you should show at least one numeric weight calculation (as above) and argue block structure; full eigen computation is long but you must show the principles and the mapping from sign of eigenvector to partition.

Common mistakes

  • Mixing up L_sym and L_rw formulas; L_sym requires D^{-1/2} and row-normalization of eigenvector matrix before k-means.
  • Taking the largest eigenvectors instead of the smallest non-zero eigenvectors; spectral clustering uses the smallest eigenvalues (closest to zero).
  • Interpreting eigenvector signs without checking multiplicity of zero eigenvalue (if λ2=0 there are more components than expected).
  • Choosing σ in the Gaussian kernel without cross-validation — too large σ makes all weights similar, too small σ isolates points.
  • Not sparsifying W for large n — full W is O(n^2) memory and O(n^3) eigencost, impractical for large datasets.

For your exam

Kinds of questions you will see and what full-marks answers include:

  • Definition question: define similarity graph, adjacency matrix, degree matrix, and Laplacian. Full marks = definitions plus brief properties (symmetry, PSD) and short proof that f^T L f = 1/2 sum w_{ij}(f_i - f_j)^2 ≥ 0.
  • Short note: compare L, L_sym, L_rw and when to use each. Full marks = formulae, explanation about degree variation and when to row-normalize.
  • Derivation: show how relaxing a discrete cut objective leads to generalized eigenproblem L u = λ D u. Full marks = write Ncut or RatioCut objective, replace indicator by real vector f, set constraints, derive Rayleigh quotient and the eigenproblem.
  • Numerical: given a small W compute D, L, eigenvalues/eigenvectors, and give the partition. Full marks = all matrices computed, eigenvector check L f = λ f shown for at least the used eigenvector, final clusters clearly stated.
  • Diagram question: draw data points with edges weighted; show W, D, L and sketch second eigenvector values on nodes. Full marks = clear drawing, labelled matrices and explanation linking sign/values to clusters.
  • Complexity/programming: describe time complexity (building W: O(n^2) for full graph, O(n log n) or O(n k log n) with efficient k-NN; eigen decomposition: O(n^3) worst-case; for sparse Laplacians, iterative eigensolvers (e.g., Lanczos/ARPACK) cost O(nnz) per matrix–vector multiply and roughly O(nnz × k × iterations) to obtain k smallest eigenvectors). Full marks = state practical choices for large n (sparsify, use Lanczos/ARPACK) and memory considerations. Also mention the eigengap heuristic for choosing k (look for a large gap between consecutive eigenvalues of the chosen Laplacian).

Always state assumptions: undirected graph, non-negative weights, and whether isolated nodes (degree zero) exist (they need special handling).

Quick check

  1. What is the unnormalized Laplacian L for a graph with adjacency W and degree D?
  2. Which eigenvectors of the Laplacian are used for spectral clustering (largest or smallest)?
  3. Name two ways to build the similarity graph from point data.

Answers: 1. L = D - W. 2. The eigenvectors corresponding to the k smallest eigenvalues (starting from 0). 3. k-nearest-neighbour graph and ε-neighbourhood graph (also: fully connected with Gaussian kernel).

— End of notes. I am Aria. I may be unsure about constants in the Cheeger inequality statement and the exact constants used in some course texts; see verify list.

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

Stuck on something here?