# Spectral clustering

In multivariate statistics, spectral clustering techniques make use of the spectrum (eigenvalues) of the similarity matrix of the data to perform dimensionality reduction before clustering in fewer dimensions. The similarity matrix is provided as an input and consists of a quantitative assessment of the relative similarity of each pair of points in the dataset.
In application to image segmentation, spectral clustering is known as segmentation-based object categorization.

## Definitions

Given an enumerated set of data points, the similarity matrix may be defined as a symmetric matrix $A$, where $A_{ij}\geq 0$ represents a measure of the similarity between data points with indices $i$ and $j$. The general approach to spectral clustering is to use a standard clustering method (there are many such methods, k-means is discussed below) on relevant eigenvectors of a Laplacian matrix of $A$. There are many different ways to define a Laplacian which have different mathematical interpretations, and so the clustering will also have different interpretations. The eigenvectors that are relevant are the ones that correspond to several smallest eigenvalues of the Laplacian except for the smallest eigenvalue which will have a value of 0. For computational efficiency, these eigenvectors are often computed as the eigenvectors corresponding to the largest several eigenvalues of a function of the Laplacian.

### Laplacian matrix

Spectral clustering is well known to relate to partitioning of a mass-spring system, where each mass is associated with a data point and each spring stiffness corresponds to a weight of an edge describing a similarity of the two related data points, as in the spring system. Specifically, the classical reference explains that the eigenvalue problem describing transversal vibration modes of a mass-spring system is exactly the same as the eigenvalue problem for the graph Laplacian matrix defined as
    $L:=D-A$,
where $D$ is the diagonal matrix
    $D_{ii}=\sum _{j}A_{ij},$
and A is the adjacency matrix.
The masses that are tightly connected by the springs in the mass-spring system evidently move together from the equilibrium position in low-frequency vibration modes, so that the components of the eigenvectors corresponding to the smallest eigenvalues of the graph Laplacian can be used for meaningful clustering of the masses. For example, assuming that all the springs and the masses are identical in the 2-dimensional spring system pictured, one would intuitively expect that the loosest connected masses on the right-hand side of the system would move with the largest amplitude and in the opposite direction to the rest of the masses when the system is shaken — and this expectation will be confirmed by analyzing components of the eigenvectors of the graph Laplacian corresponding to the smallest eigenvalues, i.e., the smallest vibration frequencies.

### Laplacian matrix normalization

The goal of normalization is making the diagonal entries of the Laplacian matrix to be all unit, also scaling off-diagonal entries correspondingly. In a weighted graph, a vertex may have a large degree because of a small number of connected edges but with large weights just as well as due to a large number of connected edges with unit weights.
A popular normalized spectral clustering technique is the normalized cuts algorithm or Shi–Malik algorithm introduced by Jianbo Shi and Jitendra Malik, commonly used for image segmentation. It partitions points into two sets $(B_{1},B_{2})$ based on the eigenvector $v$ corresponding to the second-smallest eigenvalue of the symmetric normalized Laplacian defined as
    $L^{\text{norm}}:=I-D^{-1/2}AD^{-1/2}.$
The vector $v$ is also the eigenvector corresponding to the second-largest eigenvalue of the symmetrically normalized adjacency matrix $D^{-1/2}AD^{-1/2}.$
The random walk (or left) normalized Laplacian is defined as
    $L^{\text{rw}}:=D^{-1}L=I-D^{-1}A$
and can also be used for spectral clustering. A mathematically equivalent algorithm takes the eigenvector $u$ corresponding to the largest eigenvalue of the random walk normalized adjacency matrix $P=D^{-1}A$.
The eigenvector $v$ of the symmetrically normalized Laplacian and the eigenvector $u$ of the left normalized Laplacian are related by the identity $D^{-1/2}v=u.$

### Cluster analysis via Spectral Embedding

Knowing the $n$-by-$k$ matrix $V$ of selected eigenvectors, mapping — called spectral embedding — of the original $n$ data points is performed to a $k$-dimensional vector space using the rows of $V$. Now the analysis is reduced to clustering vectors with $k$ components, which may be done in various ways.
In the simplest case $k=1$, the selected single eigenvector $v$, called the Fiedler vector, corresponds to the second smallest eigenvalue. Using the components of $v,$ one can place all points whose component in $v$ is positive in the set $B_{+}$ and the rest in $B_{-}$, thus bi-partitioning the graph and labeling the data points with two labels. This sign-based approach follows the intuitive explanation of spectral clustering via the mass-spring model — in the low frequency vibration mode that the Fiedler vector $v$ represents, one cluster data points identified with mutually strongly connected masses would move together in one direction, while in the complement cluster data points identified with remaining masses would move together in the opposite direction. The algorithm can be used for hierarchical clustering by repeatedly partitioning the subsets in the same fashion.
In the general case $k>1$, any vector clustering technique can be used, e.g., DBSCAN.

## Algorithms
### Basic Algorithm

* Calculate the Laplacian $L$ (or the normalized Laplacian)
* Calculate the first $k$ eigenvectors (the eigenvectors corresponding to the $k$ smallest eigenvalues of $L$)
* Consider the matrix formed by the first $k$ eigenvectors; the $l$-th row defines the features of graph node $l$
* Cluster the graph nodes based on these features (e.g., using k-means clustering)
If the similarity matrix $A$ has not already been explicitly constructed, the efficiency of spectral clustering may be improved if the solution to the corresponding eigenvalue problem is performed in a matrix-free fashion (without explicitly manipulating or even computing the similarity matrix), as in the Lanczos algorithm.
For large-sized graphs, the second eigenvalue of the (normalized) graph Laplacian matrix is often ill-conditioned, leading to slow convergence of iterative eigenvalue solvers. Preconditioning is a key technology accelerating the convergence, e.g., in the matrix-free LOBPCG method. Spectral clustering has been successfully applied on large graphs by first identifying their community structure, and then clustering communities.
Spectral clustering is closely related to nonlinear dimensionality reduction, and dimension reduction techniques such as locally-linear embedding can be used to reduce errors from noise or outliers.

## Costs

Denoting the number of the data points by $n$, it is important to estimate the memory footprint and compute time, or number of arithmetic operations (AO) performed, as a function of $n$. No matter the algorithm of the spectral clustering, the two main costly items are the construction of the graph Laplacian and determining its $k$ eigenvectors for the spectral embedding. The last step — determining the labels from the $n$-by-$k$ matrix of eigenvectors — is typically the least expensive requiring only $kn$ AO and creating just a $n$-by-$1$ vector of the labels in memory.
The need to construct the graph Laplacian is common for all distance- or correlation-based clustering methods. Computing the eigenvectors is specific to spectral clustering only.

### Constructing graph Laplacian

The graph Laplacian can be and commonly is constructed from the adjacency matrix. The construction can be performed matrix-free, i.e., without explicitly forming the matrix of the graph Laplacian and no AO. It can also be performed in-place of the adjacency matrix without increasing the memory footprint. Either way, the costs of constructing the graph Laplacian is essentially determined by the costs of constructing the $n$-by-$n$ graph adjacency matrix.
Moreover, a normalized Laplacian has exactly the same eigenvectors as the normalized adjacency matrix, but with the order of the eigenvalues reversed. Thus, instead of computing the eigenvectors corresponding to the smallest eigenvalues of the normalized Laplacian, one can equivalently compute the eigenvectors corresponding to the largest eigenvalues of the normalized adjacency matrix, without even talking about the Laplacian matrix.
Naive constructions of the graph adjacency matrix, e.g., using the RBF kernel, make it dense, thus requiring $n^{2}$ memory and $n^{2}$ AO to determine each of the $n^{2}$ entries of the matrix. Nystrom method can be used to approximate the similarity matrix, but the approximate matrix is not elementwise positive, i.e. cannot be interpreted as a distance-based similarity.
Algorithms to construct the graph adjacency matrix as a sparse matrix are typically based on a nearest neighbor search, which estimate or sample a neighborhood of a given data point for nearest neighbors, and compute non-zero entries of the adjacency matrix by comparing only pairs of the neighbors. The number of the selected nearest neighbors thus determines the number of non-zero entries, and is often fixed so that the memory footprint of the $n$-by-$n$ graph adjacency matrix is only $O(n)$, only $O(n)$ sequential arithmetic operations are needed to compute the $O(n)$ non-zero entries, and the calculations can be trivially run in parallel.

### Computing eigenvectors

The cost of computing the $n$-by-$k$ (with $k\ll n$) matrix of selected eigenvectors of the graph Laplacian is normally proportional to the cost of multiplication of the $n$-by-$n$ graph Laplacian matrix by a vector, which varies greatly whether the graph Laplacian matrix is dense or sparse. For the dense case the cost thus is $O(n^{2})$. The very commonly cited in the literature cost $O(n^{3})$ comes from choosing $k=n$ and is clearly misleading, since, e.g., in a hierarchical spectral clustering $k=1$ as determined by the Fiedler vector.
In the sparse case of the $n$-by-$n$ graph Laplacian matrix with $O(n)$ non-zero entries, the cost of the matrix-vector product and thus of computing the $n$-by-$k$ with $k\ll n$ matrix of selected eigenvectors is $O(n)$, with the memory footprint also only $O(n)$ — both are the optimal low bounds of complexity of clustering $n$ data points. Moreover, matrix-free eigenvalue solvers such as LOBPCG can efficiently run in parallel, e.g., on multiple GPUs with distributed memory, resulting not only in high quality clusters, which spectral clustering is famous for, but also top performance.

## Software

Free software implementing spectral clustering is available in large open source projects like scikit-learn using LOBPCG with multigrid preconditioning or ARPACK, MLlib for pseudo-eigenvector clustering using the power iteration method, and R.

## Relationship with other clustering methods

The ideas behind spectral clustering may not be immediately obvious. It may be useful to highlight relationships with other methods. In particular, it can be described in the context of kernel clustering methods, which reveals several similarities with other approaches.

### Relationship with k-means

The weighted kernel k-means problem shares the objective function with the spectral clustering problem, which can be optimized directly by multi-level methods.

### Relationship to DBSCAN

In the trivial case of determining connected graph components — the optimal clusters with no edges cut — spectral clustering is also related to a spectral version of DBSCAN clustering that finds density-connected components.

## Measures to compare clusterings

Ravi Kannan, Santosh Vempala and Adrian Vetta proposed a bicriteria measure to define the quality of a given clustering. They said that a clustering was an (α, ε)-clustering if the conductance of each cluster (in the clustering) was at least α and the weight of the inter-cluster edges was at most ε fraction of the total weight of all the edges in the graph. They also look at two approximation algorithms in the same paper.

## History and related literatures

Spectral clustering has a long history. Spectral clustering as a machine learning method was popularized by Shi & Malik and Ng, Jordan, & Weiss.
Ideas and network measures related to spectral clustering also play an important role in a number of applications apparently different from clustering problems. For instance, networks with stronger spectral partitions take longer to converge in opinion-updating models used in sociology and economics.