In statistics, single-linkage clustering is one of several methods of hierarchical clustering. It is based on grouping clusters in bottom-up fashion (agglomerative clustering), at each step combining two clusters that contain the closest pair of elements not yet belonging to the same cluster as each other.

A drawback of this method is that it tends to produce long thin clusters in which nearby elements of the same cluster have small distances, but elements at opposite ends of a cluster may be much farther from each other than two elements of other clusters. This may lead to difficulties in defining classes that could usefully subdivide the data.[1]

## Overview of agglomerative clustering methods

In the beginning of the agglomerative clustering process, each element is in a cluster of its own. The clusters are then sequentially combined into larger clusters, until all elements end up being in the same cluster. At each step, the two clusters separated by the shortest distance are combined. The definition of 'shortest distance' is what differentiates between the different agglomerative clustering methods.

In single-linkage clustering, the distance between two clusters is determined by a single element pair, namely those two elements (one in each cluster) that are closest to each other. The shortest of these links that remains at any step causes the fusion of the two clusters whose elements are involved. The method is also known as nearest neighbour clustering. The result of the clustering can be visualized as a dendrogram, which shows the sequence of cluster fusion and the distance at which each fusion took place.[2]

Mathematically, the linkage function – the distance D(X,Y) between clusters X and Y – is described by the expression

${\displaystyle D(X,Y)=\min _{x\in X,y\in Y}d(x,y),}$

where X and Y are any two sets of elements considered as clusters, and d(x,y) denotes the distance between the two elements x and y.

## Naive algorithm

The following algorithm is an agglomerative scheme that erases rows and columns in a proximity matrix as old clusters are merged into new ones. The ${\displaystyle N\times N}$ proximity matrix ${\displaystyle D}$ contains all distances ${\displaystyle d(i,j)}$. The clusterings are assigned sequence numbers${\displaystyle 0,1,......,n-1}$ and ${\displaystyle L(k)}$ is the level of the ${\displaystyle k}$-th clustering. A cluster with sequence number m is denoted (m) and the proximity between clusters ${\displaystyle (r)}$ and ${\displaystyle (s)}$ is denoted ${\displaystyle d[(r),(s)]}$.

The single linkage algorithm is composed of the following steps:

1. Begin with the disjoint clustering having level ${\displaystyle L(0)=0}$ and sequence number ${\displaystyle m=0}$.
2. Find the most similar pair of clusters in the current clustering, say pair ${\displaystyle (r),(s)}$, according to ${\displaystyle d[(r),(s)]=\min d[(i),(j)]}$where the minimum is over all pairs of clusters in the current clustering.
3. Increment the sequence number: ${\displaystyle m=m+1}$. Merge clusters ${\displaystyle (r)}$ and ${\displaystyle (s)}$ into a single cluster to form the next clustering ${\displaystyle m}$. Set the level of this clustering to ${\displaystyle L(m)=d[(r),(s)]}$
4. Update the proximity matrix, ${\displaystyle D}$, by deleting the rows and columns corresponding to clusters ${\displaystyle (r)}$ and ${\displaystyle (s)}$ and adding a row and column corresponding to the newly formed cluster. The proximity between the new cluster, denoted ${\displaystyle (r,s)}$ and old cluster ${\displaystyle (k)}$ is defined as ${\displaystyle d[(r),(s)]=\min\{d[(k),(r)],d[(k),(s)]\}}$.
5. If all objects are in one cluster, stop. Else, go to step 2.

## Working example

This working example is based on a JC69 genetic distance matrix computed from the 5S ribosomal RNA sequence alignment of five bacteria: Bacillus subtilis (${\displaystyle a}$), Bacillus stearothermophilus (${\displaystyle b}$), Lactobacillus viridescens (${\displaystyle c}$), Acholeplasma modicum (${\displaystyle d}$), and Micrococcus luteus (${\displaystyle e}$).[3][4]

### First step

• First clustering

Let us assume that we have five elements ${\displaystyle (a,b,c,d,e)}$ and the following matrix ${\displaystyle D_{1}}$ of pairwise distances between them:

a b c d e
a 017213123
b 170303421
c 213002839
d 313428043
e 232139430

In this example, ${\displaystyle D_{1}(a,b)=17}$ is the lowest value of ${\displaystyle D_{1}}$, so we cluster elements ${\displaystyle a}$ and ${\displaystyle b}$.

• First branch length estimation

Let ${\displaystyle u}$ denote the node to which ${\displaystyle a}$ and ${\displaystyle b}$ are now connected. Setting ${\displaystyle \delta (a,u)=\delta (b,u)=D_{1}(a,b)/2}$ ensures that elements ${\displaystyle a}$ and ${\displaystyle b}$ are equidistant from ${\displaystyle u}$. This corresponds to the expectation of the ultrametricity hypothesis. The branches joining ${\displaystyle a}$ and ${\displaystyle b}$ to ${\displaystyle u}$ then have lengths ${\displaystyle \delta (a,u)=\delta (b,u)=17/2=8.5}$ (see the final dendrogram)

• First distance matrix update

We then proceed to update the initial proximity matrix ${\displaystyle D_{1}}$ into a new proximity matrix ${\displaystyle D_{2}}$ (see below), reduced in size by one row and one column because of the clustering of ${\displaystyle a}$ with ${\displaystyle b}$. Bold values in ${\displaystyle D_{2}}$ correspond to the new distances, calculated by retaining the minimum distance between each element of the first cluster ${\displaystyle (a,b)}$ and each of the remaining elements:

${\displaystyle D_{2}((a,b),c)=min(D_{1}(a,c),D_{1}(b,c))=min(21,30)=21}$

${\displaystyle D_{2}((a,b),d)=min(D_{1}(a,d),D_{1}(b,d))=min(31,34)=31}$

${\displaystyle D_{2}((a,b),e)=min(D_{1}(a,e),D_{1}(b,e))=min(23,21)=21}$

Italicized values in ${\displaystyle D_{2}}$ are not affected by the matrix update as they correspond to distances between elements not involved in the first cluster.

### Second step

• Second clustering

We now reiterate the three previous actions, starting from the new distance matrix ${\displaystyle D_{2}}$ :

(a,b) c d e
(a,b) 0213121
c 2102839
d 3128043
e 2139430

Here, ${\displaystyle D_{2}((a,b),c)=21}$ and ${\displaystyle D_{2}((a,b),e)=21}$ are the lowest values of ${\displaystyle D_{2}}$, so we join cluster ${\displaystyle (a,b)}$ with element ${\displaystyle c}$ and with element ${\displaystyle e}$.

• Second branch length estimation

Let ${\displaystyle v}$ denote the node to which ${\displaystyle (a,b)}$, ${\displaystyle c}$ and ${\displaystyle e}$ are now connected. Because of the ultrametricity constraint, the branches joining ${\displaystyle a}$ or ${\displaystyle b}$ to ${\displaystyle v}$, and ${\displaystyle c}$ to ${\displaystyle v}$, and also ${\displaystyle e}$ to ${\displaystyle v}$ are equal and have the following total length: ${\displaystyle \delta (a,v)=\delta (b,v)=\delta (c,v)=\delta (e,v)=21/2=10.5}$

We deduce the missing branch length: ${\displaystyle \delta (u,v)=\delta (c,v)-\delta (a,u)=\delta (c,v)-\delta (b,u)=10.5-8.5=2}$ (see the final dendrogram)

• Second distance matrix update

We then proceed to update the ${\displaystyle D_{2}}$ matrix into a new distance matrix ${\displaystyle D_{3}}$ (see below), reduced in size by two rows and two columns because of the clustering of ${\displaystyle (a,b)}$ with ${\displaystyle c}$ and with ${\displaystyle e}$ :

${\displaystyle D_{3}(((a,b),c,e),d)=min(D_{2}((a,b),d),D_{2}(c,d),D_{2}(e,d))=min(31,28,43)=28}$

### Final step

The final ${\displaystyle D_{3}}$ matrix is:

((a,b),c,e) d
((a,b),c,e) 028
d 280

So we join clusters ${\displaystyle ((a,b),c,e)}$ and ${\displaystyle d}$.

Let ${\displaystyle r}$ denote the (root) node to which ${\displaystyle ((a,b),c,e)}$ and ${\displaystyle d}$ are now connected. The branches joining ${\displaystyle ((a,b),c,e)}$ and ${\displaystyle d}$ to ${\displaystyle r}$ then have lengths:

${\displaystyle \delta (((a,b),c,e),r)=\delta (d,r)=28/2=14}$

We deduce the remaining branch length:

${\displaystyle \delta (v,r)=\delta (a,r)-\delta (a,v)=\delta (b,r)-\delta (b,v)=\delta (c,r)-\delta (c,v)=\delta (e,r)-\delta (e,v)=14-10.5=3.5}$

### The single-linkage dendrogram

The dendrogram is now complete. It is ultrametric because all tips (${\displaystyle a}$, ${\displaystyle b}$, ${\displaystyle c}$, ${\displaystyle e}$, and ${\displaystyle d}$) are equidistant from ${\displaystyle r}$ :

${\displaystyle \delta (a,r)=\delta (b,r)=\delta (c,r)=\delta (e,r)=\delta (d,r)=14}$

The dendrogram is therefore rooted by ${\displaystyle r}$, its deepest node.

The naive algorithm for single linkage clustering is essentially the same as Kruskal's algorithm for minimum spanning trees. However, in single linkage clustering, the order in which clusters are formed is important, while for minimum spanning trees what matters is the set of pairs of points that form distances chosen by the algorithm.

Alternative linkage schemes include complete linkage clustering, average linkage clustering (UPGMA and WPGMA), and Ward's method. In the naive algorithm for agglomerative clustering, implementing a different linkage scheme may be accomplished simply by using a different formula to calculate inter-cluster distances in the algorithm. The formula that should be adjusted has been highlighted using bold text in the above algorithm description. However, more efficient algorithms such as the one described below do not generalize to all linkage schemes in the same way.

## Faster algorithms

The naive algorithm for single-linkage clustering is easy to understand but slow, with time complexity ${\displaystyle O(n^{3})}$.[5] In 1973, R. Sibson proposed an algorithm with time complexity ${\displaystyle O(n^{2})}$ and space complexity ${\displaystyle O(n)}$ (both optimal) known as SLINK. The slink algorithm represents a clustering on a set of ${\displaystyle n}$ numbered items by two functions. These functions are both determined by finding the smallest cluster ${\displaystyle C}$ that contains both item ${\displaystyle i}$ and at least one larger-numbered item. The first function, ${\displaystyle \pi }$, maps item ${\displaystyle i}$ to the largest-numbered item in cluster ${\displaystyle C}$. The second function, ${\displaystyle \lambda }$, maps item ${\displaystyle i}$ to the distance associated with the creation of cluster ${\displaystyle C}$. Storing these functions in two arrays that map each item number to its function value takes space ${\displaystyle O(n)}$, and this information is sufficient to determine the clustering itself. As Sibson shows, when a new item is added to the set of items, the updated functions representing the new single-linkage clustering for the augmented set, represented in the same way, can be constructed from the old clustering in time ${\displaystyle O(n)}$. The SLINK algorithm then loops over the items, one by one, adding them to the representation of the clustering.[6][7]

An alternative algorithm, running in the same optimal time and space bounds, is based on the equivalence between the naive algorithm and Kruskal's algorithm for minimum spanning trees. Instead of using Kruskal's algorithm, one can use Prim's algorithm, in a variation without binary heaps that takes time ${\displaystyle O(n^{2})}$ and space ${\displaystyle O(n)}$ to construct the minimum spanning tree (but not the clustering) of the given items and distances. Then, applying Kruskal's algorithm to the sparse graph formed by the edges of the minimum spanning tree produces the clustering itself in an additional time ${\displaystyle O(n\log n)}$ and space ${\displaystyle O(n)}$.[8]