TL;DR
Single-linkage clustering is one version of hierarchical clustering. It starts with every point alone and repeatedly merges the two groups whose closest pair of points is nearest, so the distance between two groups is the distance between their closest members. This makes it good at finding long, winding groups, and it is also easily fooled by a few points that sit between two groups.
Builds on: Hierarchical Clustering, Euclidean Distance · Leads to: Minimum Spanning Tree
Where it's used
Sorting out shapes that are not round. A set of points that forms a curve, such as a river's path on a map of wells, is split badly by k-means, which prefers round blobs. Single linkage follows the curve, because each point is merged with its nearest neighbour along the path.
Finding the outliers. Because a single distant point stays alone until the very end of the merging, the last few merges show which points are far from everything else. A data analyst can read these heights from the tree to spot unusual records in a sensor log.
Grouping by connection. When the points are connected pairs, such as stations on a transit network joined by short hops, single linkage asks whether a path of short hops links two stations. The answer matches the question the analyst has: are these stations connected at all?
How it works
Take five points on a line: P = 0, Q = 1, R = 4, S = 6 and T = 10. The distance between two points is the absolute difference of their values, so the distances between each pair are:
- P-Q 1, P-R 4, P-S 6, P-T 10
- Q-R 3, Q-S 5, Q-T 9
- R-S 2, R-T 6
- S-T 4
The rule for the distance between two groups is the single linkage rule: it is the smallest distance between any point in one group and any point in the other.
flowchart TD PQ[P and Q merge at 1] --> PQRS[Group PQ and RS merge at 3] RS[R and S merge at 2] --> PQRS PQRS --> ALL[Everything merges with T at 4]
-
Start with five clusters: {P}, {Q}, {R}, {S}, {T}. Check: five groups, each with one point.
-
Find the closest pair. P-Q is 1, the smallest distance. Merge P and Q and record height 1. Check: four clusters, {P, Q}, {R}, {S}, {T}.
-
Find the closest pair of groups. From {P, Q} to R the distance is the smaller of P-R = 4 and Q-R = 3, which is 3. From R to S it is 2, which is smaller than all the others. Merge R and S and record height 2. Check: three clusters, {P, Q}, {R, S}, {T}.
-
Find the closest pair of groups. From {P, Q} to {R, S} the smallest of P-R 4, P-S 6, Q-R 3 and Q-S 5 is 3. From {R, S} to T the smaller of R-T 6 and S-T 4 is 4. The smallest is 3, so merge {P, Q} with {R, S} and record height 3. Check: two clusters, {P, Q, R, S} and {T}.
-
Merge the last two. From {P, Q, R, S} to T the smallest distance is S-T = 4. Record height 4. Check: one cluster containing all five points.
The recorded heights are 1, 2, 3 and 4. To get groups, cut the tree at a height. Cutting at height 2.5 keeps the merges at heights 1 and 2 and gives {P, Q}, {R, S} and {T}. Cutting at height 3.5 keeps the merges at heights 1, 2 and 3 and gives {P, Q, R, S} and {T}.
The math
Let be the distance between two points, here the Euclidean distance. For two groups and of points, the single-linkage distance is
Merging the closest pair (walkthrough steps 2 to 5)
This is a greedy algorithm: at each step it takes the best merge available and never undoes it. At each step the algorithm looks at every pair of current groups, finds the pair with the smallest , merges them into , and records as the merge height. With points there are exactly merges, so the process always ends in one group.
The single-linkage update (walkthrough step 3)
After the merge of P and Q, the distance from the new group to any other group is found from the two old distances:
In step 3 this is . Because only the minimum is needed, the algorithm can keep a table of group distances and update one row at a time instead of going back to the raw points.
Cutting the tree (walkthrough step 5)
Suppose the merge heights are . Cutting at height keeps every merge whose height is at most . The number of groups is minus the number of kept merges. In the example, the cut at keeps two merges, so there are groups.
A connection to spanning trees
Single linkage has a neat geometric meaning. Build a weighted Graph whose vertices are the points and whose edges join every pair, weighted by distance. A minimum spanning tree is the cheapest set of edges that connects everything without a cycle. It can be built with union-find, which is how Kruskal's method checks for cycles. Its edge weights, sorted from smallest to largest, are exactly the single-linkage merge heights. For the five points the spanning tree uses the edges P-Q (1), R-S (2), Q-R (3) and S-T (4), which are the same four heights as the merges above.
Worked examples
Points 0, 2 and 3 on a line.
- Distances: , , .
- The closest pair is 2 and 3, at distance 1. Merge them and record height 1.
- The distance from {0} to {2, 3} is .
- Merge the last two groups at height 2.
- Interpretation: cutting at height 1.5 keeps only the first merge and gives two groups, {0} and {2, 3}. Cutting at height 2.5 gives one group.
This is the example above, with the distances used at each step.
- Step 1: P-Q is 1, the smallest, so merge P and Q at height 1.
- Step 2: R-S is 2, smaller than every group distance from {P, Q}, which are 3, 5 and 9 to R, S and T, so merge R and S at height 2.
- Step 3: the distance from {P, Q} to {R, S} is , and the distance from {R, S} to T is . The smaller is 3, so merge at height 3.
- Step 4: the distance from {P, Q, R, S} to T is , so merge at height 4.
- Interpretation: the tree has merges at 1, 2, 3 and 4. T joins last because its nearest neighbour, S, is 4 away, and the heights match the spanning tree edges in the math section.
A bike-share system has five stations placed on a map. The points below are in kilometres, and the distance is the straight line between them. The figures are invented for the example, to show the method.
Stations A at (0, 0), B at (3, 4), C at (6, 4), D at (6, 8) and E at (20, 8).
- Distances that matter: A-B is 5, B-C is 3, C-D is 4, B-D is 5, A-C is about 7.21, and D-E is 14.
- Step 1: B-C is 3, the closest pair. Merge B and C at height 3.
- Step 2: the distance from {B, C} to D is . The distance from A to {B, C} is . The closest is D at 4, so merge D into the group at height 4.
- Step 3: the distance from A to {B, C, D} is , so merge A at height 5.
- Step 4: the distance from E to {A, B, C, D} is , so merge E at height 14.
- Interpretation: the chain A, B, C, D is joined by short hops, while E is 14 km from its nearest station and joins only at the top. Cutting at 10 gives {A, B, C, D} and {E}, which is the grouping a planner would want to check first.
- Try it: at what height does E join the others, and why is that height the distance from D? Answer: 14, because D is the station nearest to E, and single linkage uses the closest pair.
Common mistakes and tradeoffs
It is tempting to think that single linkage finds the centre of each group. In fact it has no centre at all: a group can be a long snake of points with its two ends far apart, because two groups merge as soon as any pair of their members is close. Its rule is local, like the nearest-neighbour idea: only the closest member of each group matters. This is called chaining, and it is the method's main strength and main weakness. It helps with curved shapes, but a few points that happen to lie between two real groups can join them into one.
It is also tempting to think that the number of clusters is set by the method. In fact the tree shows every number of clusters, and the choice comes from the height at which you cut it. A cut that looks good on one dataset can be far too coarse on another, so look at the heights before you cut.
Use single linkage when the groups may be long or curved, when you want to see the connections between points, and when you want to find outliers from the last merges. Do not use it when the data is noisy and two real groups may be bridged by stray points, or when you want compact, round groups. Alternatives are complete linkage, which uses the farthest pair and gives compact groups, and average linkage, which uses the mean distance and sits between the two. k-means is faster on large data but needs the number of groups in advance and prefers round groups. Distances depend on units, so put features on comparable scales first (Feature Scaling).
History
The idea was described in the early 1950s by a group of Polish statisticians who were dividing a set of points into groups by linking points whose distance was below a threshold. In the 1960s, numerical taxonomists in biology used the same merge rule to group species from measured traits. The method was slow to run on large sets, since each step scanned every pair of groups. In 1973 Robin Sibson published SLINK, an algorithm that computes the same tree with far less memory and time, which made the method practical for larger data and is still the basis of many implementations.
Recap
- Single linkage merges the two groups whose closest pair of members is nearest.
- The group distance is a minimum, so groups grow along chains of close points.
- The merge heights form a tree, and cutting it at a height gives groups.
- The heights match the edges of a minimum spanning tree, so the method follows its connections.
- Read next: Minimum Spanning Tree for the graph view, and K-Means Clustering for the method that needs the number of groups in advance.
Check yourself
QuestionFour points lie on a line at 0, 1, 5 and 6. What are the first two merge heights under single linkage?Show answer
The closest pair is 0 and 1, at distance 1, so the first merge height is 1. The closest pair of the remaining groups is 5 and 6, at distance 1, so the second merge height is also 1. The single-linkage rule takes the minimum of the member distances, and both pairs are at distance 1, so the heights are 1 and 1.
QuestionThree points are at 0, 2 and 3 on a line. At what height does the point at 0 join the group {2, 3}?Show answer
At height 2. The distance from the group {2, 3} to the point 0 is the smaller of the distances from 0 to 2 and from 0 to 3, which are 2 and 3. The minimum is 2, so the point at 0 joins at height 2.
QuestionWhy can single linkage join two real clusters through a few stray points between them, when k-means would not?Show answer
Single linkage merges two groups as soon as any one pair of members is close, so a chain of stray points that are each close to the next will connect the two groups before they are merged by any other route. K-means does not merge at all; it assigns each point to the nearest centre, so a few points between two clusters only shift a centre slightly and do not connect them.
QuestionA planner cuts a single-linkage tree at height 5 and gets four groups from 20 stations. Is that the number of groups she should use?Show answer
Not necessarily. The cut at height 5 gives four groups only for this data, and the number of groups is a choice made by where the cut falls. She should look at the merge heights near 5: if the heights just below and above 5 are close together, the cut is arbitrary, and a cut in a larger gap would separate the groups more clearly. The method reports the tree, and the choice of cut is a separate decision.