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?
- Connect nearby points into a graph.
- Compute a few special vectors of that graph (the eigenvectors of its Laplacian), in which each well-connected group becomes a tight blob.
- Run k-means on those blobs.
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.
- 1. Spectral clustering first builds…
- 2. It can separate two concentric rings because…
- 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):
- Build an affinity matrix W: RBF with width σ, or a symmetrised k-nearest-neighbour graph.
- Form D^(−½)WD^(−½) and take its k leading eigenvectors (equivalently, the k smallest of L_sym) as columns of U.
- Normalise each row of U to unit length.
- Run k-means (several starts) on the rows. Point i gets the cluster of row i.
- 1. Zero eigenvalues of L for a graph with 3 connected components?
- 2. RBF weight at distance 2 with σ = 2?
- 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σ²)- 1. Spectral clustering relaxes which problem?
- 2. If σ exceeds the gap between clusters…
- 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.
- 1. fᵀLf equals…
- 2. Unnormalised spectral clustering relaxes…
- 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.
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
- U. von Luxburg (2007). “A tutorial on spectral clustering.” Statistics and Computing 17(4).
- A. Y. Ng, M. I. Jordan, Y. Weiss (2002). “On spectral clustering: analysis and an algorithm.” NeurIPS 14.
- J. Shi, J. Malik (2000). “Normalized cuts and image segmentation.” IEEE TPAMI 22(8).
- L. Zelnik-Manor, P. Perona (2004). “Self-tuning spectral clustering.” NeurIPS 17.
- M. Fiedler (1973). “Algebraic connectivity of graphs.” Czechoslovak Mathematical Journal 23.