Foundations to Frontier
Tests

Agglomerative Hierarchical Clustering (Single, Complete, Average, Ward Linkage)

Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two closest clusters until one remains. The record of merges is a dendrogram, and cutting it at any height gives a clustering. What 'closest' means is the linkage: single (closest pair of points), complete (farthest pair), average (mean pairwise distance) or Ward (smallest increase in within-cluster variance). Linkage decides cluster shape: single linkage follows chains and finds elongated or curved clusters but can be fooled by bridges; complete and Ward prefer compact groups. This page covers the algorithm, the Lance–Williams update, the minimum-spanning-tree view of single linkage, Ward's objective, dendrogram cutting, the cophenetic correlation, and cost. In the exercises you implement all four linkages and check them against SciPy; then you compare linkages on bridged blobs, moons, unequal blobs and parallel bars.

Level 0: Foundation

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

Merge the closest pair, over and over

Start with every point as its own tiny cluster. Find the two clusters that are closest and merge them. Repeat until everything is one cluster. The history of merges forms a tree, the dendrogram; cut it at any height and you get a clustering.

The only real choice is how to measure the distance between two groups, the linkage:

  • single uses the closest pair;
  • complete uses the farthest pair;
  • average uses the mean over all pairs;
  • Ward uses the increase in spread caused by merging.
Linkage decides the shape of clusters
Single linkage merges the two closest points of two groups, so it follows chains: perfect for moons, but a thin bridge glues two blobs together. Complete linkage uses the farthest pair and prefers compact, similar-diameter groups. Average sits between them. Ward merges whichever pair adds the least within-cluster variance, like k-means. Look at the dendrogram too: a long vertical gap below the cut means a clear cluster structure.
Why engineers care

You don’t need to choose k up front, and the dendrogram shows structure at every scale: fleets that split into types, then variants, then individual tails. Single linkage traces connected structures such as trajectories, flight corridors and filaments. Ward gives k-means-like compact groups with a full hierarchy.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Agglomerative clustering starts with…
  2. 2. Single linkage measures group distance by…
  3. 3. Which linkage separates the two moons in the demo?

Level 1: Beginner

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

Linkages by hand

Groups A = (0, 1) and B = (3, 5) on a line. The cross distances are 3, 5, 2, 4.

  • Single linkage: 2.
  • Complete linkage: 5.
  • Average linkage: (3 + 5 + 2 + 4)/4 = 3.5.

Ward. The increase in total within-cluster SSE from merging A and B is (n_A·n_B/(n_A + n_B))·‖mean_A − mean_B‖². For A = (0, 2) and B = (10): (2·1/3)·(1 − 10)² = 54.

Updating after a merge (Lance–Williams). Clusters i (1 point) and j (3 points) merge. Another cluster k has d(k, i) = 2 and d(k, j) = 6.

  • Single: min = 2.
  • Complete: max = 6.
  • Average: (1·2 + 3·6)/4 = 5.

There is no need to go back to the raw points.

Counting. n points need n − 1 merges. Cutting the dendrogram between the (n − k)-th and (n − k + 1)-th merge heights leaves k clusters.

The steps in plain English:

  1. Compute all pairwise distances; each point is a cluster.
  2. Find the closest pair of clusters under the chosen linkage and merge them, recording the height.
  3. Update the distances from the new cluster to all others (Lance–Williams).
  4. Repeat until one cluster remains. Cut the dendrogram at a height, or at the k you want.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Complete-linkage distance between (0, 1) and (3, 5)?
  2. 2. Merges for 100 points?
  3. 3. Ward increase for merging the single points 0 and 2?

Level 2: Intermediate

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

What each linkage optimises

Single linkage = minimum spanning tree. Build the MST of the complete distance graph. Single linkage merges along its edges in order of length, so the merge heights are exactly the MST edge lengths (verified in exercise 1). Cutting the k − 1 longest MST edges gives the k-cluster solution.

This explains both behaviours. Single linkage finds clusters of any shape as long as they are connected by short steps. It also chains: one thin line of points between two blobs joins them. Exercise 2: ARI 1.00 on moons and parallel bars, but 0.00 on bridged blobs.

Complete linkage keeps cluster diameters small. It finds compact, roughly equal-diameter groups and resists chaining, but it splits long clusters.

Average linkage (UPGMA) sits between the two. It is the classic choice in phylogenetics.

Ward merges the pair that increases the total within-cluster sum of squares least, a greedy k-means. It gives compact, similar-sized clusters and is the usual default for Euclidean features.

Cophenetic correlation. It correlates the original distances with the dendrogram heights at which pairs first join. It measures how faithfully the tree preserves distances, not whether the clusters are right. In exercise 2 single linkage had the lowest cophenetic correlation on the moons (0.48) exactly where its clusters were perfect.

from scipy.cluster.hierarchy import linkage, fcluster, dendrogram, cophenet
Z = linkage(X, method="ward")            # or "single", "complete", "average"
labels = fcluster(Z, t=3, criterion="maxclust")
c, _ = cophenet(Z, scipy.spatial.distance.pdist(X))
🐍 Exercise 1: agglomerative clustering from scratch
Implement Lance–Williams agglomeration for single, complete, average and Ward linkage returning a SciPy-style linkage matrix, and a dendrogram cut; check heights against SciPy, single linkage against the MST, and Ward's 3-cluster cut.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Single-linkage merge heights equal…
  2. 2. Ward linkage greedily minimises…
  3. 3. A high cophenetic correlation guarantees correct clusters?

Level 3: Advanced

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

Theory

Lance–Williams family. Many linkages satisfy

d(k, i ∪ j) = αᵢ d(k, i) + αⱼ d(k, j) + β d(i, j) + γ |d(k, i) − d(k, j)|.

  • Single: αᵢ = αⱼ = ½, β = 0, γ = −½.
  • Complete: γ = +½.
  • Average: αᵢ = nᵢ/(nᵢ + nⱼ), β = γ = 0.
  • Ward (on squared distances): αᵢ = (n_k + nᵢ)/(n_k + nᵢ + nⱼ), β = −n_k/(n_k + nᵢ + nⱼ), γ = 0.

Monotonicity. For these four linkages the merge heights never decrease, so the dendrogram has no inversions. Centroid and median linkage can invert.

Complexity. The naive algorithm is O(n³) time and O(n²) memory.

  • With priority queues it is O(n² log n).
  • SLINK (single) and CLINK (complete) run in O(n²).
  • The nearest-neighbour chain algorithm gives O(n²) for Ward, average and complete.

The O(n²) memory limits it to tens of thousands of points.

Kleinberg’s impossibility (2002). No clustering function satisfies scale-invariance, richness and consistency together. Single linkage with different stopping rules gives up exactly one of them. That is a reminder that every linkage embodies a choice.

Interview questions.

  • Which linkage would you choose? Ward for compact Euclidean clusters. Single for elongated or curved structure in clean data. Average or complete for general-purpose, distance-only data.
  • How do you choose where to cut? Look for the largest gap in merge heights, use silhouette at different k, or let the domain decide.
📝 Quick check (Level 3)
0/3 answered
  1. 1. In Lance–Williams, single linkage has γ =
  2. 2. Memory for agglomerative clustering on n points?
  3. 3. Which linkages can produce dendrogram inversions?

Level 4: Expert

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

Which linkage for which shape?

History.

  • K. Florek et al. (1951) and P. Sneath (1957) introduced single linkage.
  • J. H. Ward Jr. (1963) proposed minimum-variance merging.
  • G. Lance and W. Williams (1967) unified linkages with one update formula.
  • R. Sibson (SLINK, 1973) and D. Defays (CLINK, 1977) gave O(n²) algorithms.
  • F. Murtagh popularised the nearest-neighbour chain approach.

Exercise 2. 3 datasets per shape, cut at the true k. Each cell shows ARI, then the cophenetic correlation.

shape single complete average Ward
two blobs joined by a thin bridge 0.00, 0.49 0.98, 0.91 0.98, 0.93 0.95, 0.92
two moons 1.00, 0.48 0.33, 0.72 0.47, 0.73 0.32, 0.73
one big diffuse blob + two small tight ones −0.01, 0.61 −0.08, 0.56 0.46, 0.73 0.24, 0.66
two long parallel bars 1.00, 0.25 −0.00, 0.73 −0.00, 0.72 0.01, 0.71
  • No linkage wins everywhere. Single was perfect on moons and bars and useless on the bridged blobs. Complete, average and Ward reversed that.
  • The big + small blobs defeated everyone. Single and complete split off outliers of the big blob. Average did best (0.46) but still wrongly.
  • Cophenetic correlation pointed the wrong way. Single linkage had the lowest values on exactly the shapes it got perfectly right. Don’t use it to choose a linkage for cluster recovery.
🐍 Exercise 2 (research): linkage versus cluster shape
On bridged blobs, two moons, big-plus-small blobs and parallel bars, compare single, complete, average and Ward linkage by ARI at the true k and by cophenetic correlation.

Hierarchical clustering in AI, robotics and aerospace

  • Fleet and configuration taxonomies: dendrograms of aircraft or satellites by telemetry signatures.
  • Trajectory and corridor discovery: single linkage on track points to extract connected flight paths.
  • Sensor and channel grouping: average linkage on 1 − |correlation| to find redundant telemetry channels.

References

  1. J. H. Ward Jr. (1963). “Hierarchical grouping to optimize an objective function.” Journal of the American Statistical Association 58.
  2. G. N. Lance, W. T. Williams (1967). “A general theory of classificatory sorting strategies.” The Computer Journal 9(4).
  3. R. Sibson (1973). “SLINK: an optimally efficient algorithm for the single-link cluster method.” The Computer Journal 16(1).
  4. F. Murtagh, P. Contreras (2012). “Algorithms for hierarchical clustering: an overview.” WIREs Data Mining and Knowledge Discovery 2(1).
  5. J. Kleinberg (2002). “An impossibility theorem for clustering.” NeurIPS 15.