Foundations to Frontier
Tests

Spectral Clustering

Spectral clustering turns clustering into a graph problem. Build a similarity graph over the points (an RBF kernel or k-nearest neighbours), compute the leading eigenvectors of its normalised Laplacian, embed each point by its row of those eigenvectors, and run k-means in that embedding. Clusters that are connected through chains of near neighbours, such as rings or moons, become compact blobs in the embedding, even though they are not convex in the original space. It approximates a minimum normalised cut, and the eigenvalues hint at how many clusters there are. This page covers the construction (affinity, Laplacians, the Ng–Jordan–Weiss algorithm), the normalised-cut view, choosing σ or k for the graph, the eigengap heuristic and its pitfalls, and cost. In the exercises you implement spectral clustering and test eigengap rules; then you map how sensitive the result is to σ, to the k-NN graph's k, and to Laplacian normalisation.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Cluster by who’s connected to whom

k-Means groups points around centres, so it can’t separate two rings: their centres coincide. Spectral clustering asks a different question: which points are linked by chains of close neighbours?

  1. Connect nearby points into a graph.
  2. Compute a few special vectors of that graph (the eigenvectors of its Laplacian), in which each well-connected group becomes a tight blob.
  3. Run k-means on those blobs.
Spectral clustering: the affinity scale σ decides everything
spectral embeddingsmallest Laplacian eigenvalues
agreement with the true rings 100.0% · eigenvalues -0.000, 0.000, 0.003, 0.004, 0.013, 0.013
With σ well below the 1.6 gap between rings, points are linked only to neighbours on their own ring. The graph has two nearly separate pieces, two eigenvalues sit near 0, and the embedding collapses each ring to one spot. Raise σ past the gap and the rings link up: the second eigenvalue lifts off zero and the clustering degrades. At the far left, σ is so small that the graph breaks into many fragments: many eigenvalues sit at 0, and two eigenvectors can no longer capture the two rings. The colours in the embedding panel show the true rings.
Why engineers care

Spectral methods find clusters of any shape: road and corridor networks, segmented image regions, communities in communication graphs, groups of sensors that move together. The same machinery underlies graph partitioning for parallel simulation meshes, and Laplacian eigenmaps for dimensionality reduction.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Spectral clustering first builds…
  2. 2. It can separate two concentric rings because…
  3. 3. The final step is usually…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The pieces by hand

RBF affinity. w = exp(−d²/(2σ²)). At distance 1:

  • with σ = 1, w = 0.607;
  • with σ = 0.5, w = e^(−2) = 0.135.

Smaller σ makes the graph more local.

Laplacian. L = D − W, where D holds the degrees (row sums). Two nodes joined by weight 1 give L = [[1, −1], [−1, 1]], with eigenvalues 0 and 2.

  • The number of zero eigenvalues equals the number of connected components.
  • Constant vectors on each component lie in the null space.

Normalised Laplacian. L_sym = I − D^(−½)WD^(−½). Its diagonal is all ones.

Normalised cut. Ncut(A, B) = cut(A, B)/vol(A) + cut(B, A)/vol(B), where vol is the sum of degrees. Take two pairs, each joined internally by weight 1, with a single cross link of 0.1. Each side has volume 2.1, so Ncut = 2 × 0.1/2.1 = 0.095.

The steps in plain English (Ng–Jordan–Weiss):

  1. Build an affinity matrix W: RBF with width σ, or a symmetrised k-nearest-neighbour graph.
  2. Form D^(−½)WD^(−½) and take its k leading eigenvectors (equivalently, the k smallest of L_sym) as columns of U.
  3. Normalise each row of U to unit length.
  4. Run k-means (several starts) on the rows. Point i gets the cluster of row i.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Zero eigenvalues of L for a graph with 3 connected components?
  2. 2. RBF weight at distance 2 with σ = 2?
  3. 3. Unit-normalising the row (3, 4) gives…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Why it works, and how to tune it

Relaxed normalised cut. Finding the partition with minimum Ncut is NP-hard. Writing it with indicator vectors and relaxing them to real values gives an eigenproblem, whose solution is the smallest eigenvectors of the normalised Laplacian (Shi and Malik, 2000). k-Means on the embedding rounds the relaxed solution back to a partition.

Ideal case. If the graph has exactly k components, the k leading eigenvectors are indicator-like. After row normalisation every point of a component maps to the same point, so k-means is trivial. Real graphs are perturbations of that, and eigenvector perturbation theory (Davis–Kahan) explains why it still works when clusters are only weakly linked.

Exercise 1, two concentric rings:

  • spectral clustering reached ARI 1.000 with an RBF graph (σ = 0.3) and with a 10-NN graph;
  • k-means gave ARI 0.00.

The graph is the model.

  • σ (RBF) must be larger than the spacing within a cluster but smaller than the gap between clusters.
  • The k-NN graph’s k must connect each cluster without bridging clusters.
  • In exercise 2 the good σ range on rings was 0.05–0.32 (from 0.073 in the browser, where σ = 0.05 is numerically fragile). On moons (closer together) it was only 0.05–0.105. On moons, k = 5 neighbours worked but k ≥ 8 bridged them.

How many clusters? The eigengap. Take the k at which the sorted eigenvalues jump. In exercise 1 the eigenvalues were 0, 0, 0.0006, 0.0017, …:

  • a relative gap picked k = 2;
  • the classic absolute gap picked k = 6, because the eigenvalues within each ring keep growing.

Treat the eigengap as a hint, not a rule.

Cost. Building a dense W is O(n²); a full eigendecomposition is O(n³). Use sparse k-NN graphs with Lanczos (ARPACK) or LOBPCG for the few eigenvectors needed, or the Nyström approximation, for large n.

from sklearn.cluster import SpectralClustering
sc = SpectralClustering(n_clusters=2, affinity="nearest_neighbors", n_neighbors=10,
                        assign_labels="kmeans", random_state=0).fit(X)          # or affinity="rbf", gamma=1/(2σ²)
🐍 Exercise 1: spectral clustering
Implement RBF and symmetrised k-NN affinities, Ng–Jordan–Weiss spectral clustering (and the unnormalised variant), and two eigengap rules; separate concentric rings that defeat k-means.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Spectral clustering relaxes which problem?
  2. 2. If σ exceeds the gap between clusters…
  3. 3. In exercise 1 the absolute eigengap chose…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Laplacian facts.

  • For any vector f: fᵀLf = ½ Σᵢⱼ wᵢⱼ(fᵢ − fⱼ)² ≥ 0. So L is positive semidefinite, and f is in its null space exactly when f is constant on each connected component.
  • The second-smallest eigenvalue (the Fiedler value, or algebraic connectivity) measures how well-connected the graph is.
  • Cheeger’s inequality relates it to the best conductance cut: λ₂/2 ≤ h(G) ≤ √(2λ₂).

Normalised versus unnormalised.

  • Unnormalised spectral clustering relaxes RatioCut, which balances cluster sizes.
  • Normalised (L_sym or the random-walk version L_rw = I − D⁻¹W) relaxes Ncut, which balances volumes.

von Luxburg (2007) recommends the normalised versions: they are consistent under broader conditions, while the unnormalised version can fail. In exercise 2 the difference showed at very small σ. On rings with uneven density, normalised clustering scored ARI 1.00 natively (0.87 in the in-browser Python, because near-degenerate eigenvectors of a fragmented graph are numerically fragile), while unnormalised dropped to 0.66 on both, as its smallest eigenvectors latched onto tiny components.

Random-walk view. Ncut equals the probability that a stationary random walk jumps between clusters. So spectral clustering finds sets the walk rarely leaves, which links it to diffusion maps.

Interview questions.

  • When would spectral clustering beat k-means? With non-convex but connected clusters (rings, moons, manifolds), or when only a similarity matrix is available.
  • What are its weak points? Choosing the graph (σ or k), O(n³) eigendecomposition for dense graphs, and sensitivity when clusters differ in density or scale. Self-tuning local σ (Zelnik-Manor and Perona) helps.
📝 Quick check (Level 3)
0/3 answered
  1. 1. fᵀLf equals…
  2. 2. Unnormalised spectral clustering relaxes…
  3. 3. The Fiedler value is the…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How forgiving is the graph construction?

History.

  • M. Fiedler (1973) linked the second eigenvector to graph connectivity.
  • J. Shi and J. Malik (2000) introduced normalised cuts for image segmentation.
  • A. Ng, M. Jordan and Y. Weiss (2002) gave the popular row-normalised algorithm.
  • U. von Luxburg (2007) wrote the standard tutorial and consistency analysis.
  • L. Zelnik-Manor and P. Perona (2004) proposed self-tuning σ.

Exercise 2. 2 datasets each, k = 2. σ ranged from 0.05 to 3 on a log grid; the k-NN graph’s k ranged from 3 to 60. The table lists the values that gave ARI above 0.95.

data, Laplacian σ giving ARI > 0.95 k-NN k giving ARI > 0.95 ARI at σ = 0.05
rings (60 + 240 points), normalised 0.05–0.32 (0.073–0.32 in the browser) 8, 12, 20 1.00 (0.87 in the browser)
rings, unnormalised 0.073–0.47 8, 12, 20 0.66
two moons, normalised 0.05–0.105 5 1.00
two moons, unnormalised 0.05–0.105 5 1.00
  • The good σ range spanned about a factor of 6 on rings, but only 2 on moons, whose arms come close.
  • k-NN graphs were similarly narrow on moons. Only k = 5 worked; larger k bridged the arms.
  • Normalisation mattered at very small σ, where the graph fragments, though results there are numerically fragile: the normalised score at σ = 0.05 was 1.00 natively and 0.87 in the browser’s Python. Otherwise the two behaved alike.
  • No construction is tuning-free. Choose the scale from the data’s smallest gap, and check stability across nearby settings.
🐍 Exercise 2 (research): sensitivity to the graph
On rings of unequal density and two moons, sweep the RBF σ and the k-NN graph's k for normalised and unnormalised spectral clustering; report where ARI exceeds 0.95.

Spectral clustering in AI, robotics and aerospace

  • Image and terrain segmentation (normalised cuts) in perception pipelines.
  • Mesh and network partitioning for parallel CFD and FEM, and grouping satellites or sensors by communication or co-motion graphs.
  • Motion segmentation: clustering feature trajectories that move together, used to separate independently moving objects.

References

  1. U. von Luxburg (2007). “A tutorial on spectral clustering.” Statistics and Computing 17(4).
  2. A. Y. Ng, M. I. Jordan, Y. Weiss (2002). “On spectral clustering: analysis and an algorithm.” NeurIPS 14.
  3. J. Shi, J. Malik (2000). “Normalized cuts and image segmentation.” IEEE TPAMI 22(8).
  4. L. Zelnik-Manor, P. Perona (2004). “Self-tuning spectral clustering.” NeurIPS 17.
  5. M. Fiedler (1973). “Algebraic connectivity of graphs.” Czechoslovak Mathematical Journal 23.