Explain agglomerative clustering: linkage choices (single/complete/average/Ward), the dendrogram, and where to cut it. Why is it memory?

The Problem: A retail team wants 3 to 6 customer segments from 3,000 profiles with 8 standardized features, but a single-linkage pilot cut at returned clusters of 2,991, 6, 2 and 1 customers, while a Ward run shows top merge heights of 52.0, 38.5, 31.0, 12.4, 10.9 and 9.7. Next quarter the same job must run on 100,000 customers on a 32 GB machine, and nobody has decided the linkage, the cut, or whether it will fit.
Answer
Agglomerative clustering starts with every point as its own cluster and repeatedly merges the two closest clusters until one remains. Each merge and its height are recorded in a dendrogram, a binary tree with joins. The linkage defines “closest”: single uses the nearest cross pair, complete the farthest, average the mean over all cross pairs, and Ward the increase in within-cluster sum of squares. A flat clustering comes from cutting the tree horizontally, ideally inside a large gap between consecutive merge heights. The standard algorithm stores all
pairwise cluster distances and refreshes them after every merge with the Lance-Williams update. That matrix is the source of the
memory, and at
it holds about 5 billion float64 values, or 40 GB.
(1) Single Linkage: Distance between the two nearest members, which makes the hierarchy equivalent to the minimum spanning tree. It recovers elongated, non-convex shapes but chains clusters together through a few bridging points.
(2) Complete Linkage: Distance between the two farthest members, so every cluster formed below height has diameter at most
. Clusters come out compact, but one outlier inflates a diameter and large groups tend to be split.
(3) Average Linkage: Mean of all cross distances, also called UPGMA. It sits between the two extremes, dilutes single outliers, and works with any dissimilarity such as cosine or correlation.
(4) Ward Linkage: Merges the pair whose union adds the least within-cluster sum of squares, the same objective k-means minimizes. It yields compact clusters of similar size and only makes sense with Euclidean distance.
(5) Reading The Dendrogram: Only heights carry meaning, since the left-right order is one of equivalent flips. Cut at a fixed height, at the level that leaves
branches, or inside the largest gap between merge heights.
(6) Why Quadratic Memory: After merging and
, the Lance-Williams update needs the old distances from every other cluster to both, so the whole distance matrix must stay live. The nearest-neighbor chain algorithm brings time down to
from the naive
, but memory stays quadratic for complete and average linkage.
Mathematical Formulation:
Where:
and
are two current clusters with members
and
,
is a cluster size, and
is the base dissimilarity. Single, complete and average linkage accept any dissimilarity.
is the centroid of
, and
is the exact rise in total within-cluster SSE when
and
merge. It requires Euclidean geometry, and SciPy reports the merge height as
.
,
and
are pre-merge distances between an untouched cluster
and the just-merged clusters
and
, and
is the updated distance to their union.
select the linkage. Single uses
(a min), complete flips to
(a max), and average uses
with
.
- Ward uses
,
and
on squared Euclidean distances, where
are cluster sizes.
is the size of the condensed float64 distance matrix for
points: 36 MB at
, 40 GB at
, and double that for a full square matrix.
Worked Example:
On a line, take clusters ,
and
. The cross distances between
and
are 4, 9, 2 and 7, so single gives 2, complete gives 9 and average gives 5.5. Between
and
they are 8.5 and 3.5, so single gives 3.5, complete gives 8.5 and average gives 6.0. Single and average therefore merge
with
, while complete merges
with
because 8.5 beats 9. Ward compares SSE increases:
and
, so Ward also picks
. Same three clusters, two different trees after one step.

Figure 1: The same 315 points cut at under each linkage, with cluster sizes in each panel title. Single linkage follows nearest-neighbor chains, so the 12-point bridge and the isolated points decide its partition. Ward prices every merge by added variance and favors compact groups of similar spread, and complete and average land between these extremes.
In practice the choice follows from two questions: which dissimilarity do you trust, and what cluster shape do you expect? Ward is the default for standardized numeric features where k-means-like segments are wanted. Average linkage is the default for non-Euclidean similarities such as correlation between gene-expression profiles, and single linkage is reserved for clean data with genuinely elongated structure.
| Property | Ward | Average | Complete | Single |
|---|---|---|---|---|
| Merge criterion | Smallest rise in within-cluster SSE | Mean of all cross-pair distances | Farthest cross pair | Nearest cross pair |
| Cluster shape | Compact, similar size and variance | Moderately compact, tolerates size differences | Compact with bounded diameter | Arbitrary, elongated chains |
| Outliers and noise | Absorbed into the nearest compact group | Diluted by averaging | Inflate diameters and split large groups | Become singletons, and bridges chain clusters |
| Dissimilarity | Euclidean only | Any | Any | Any |
| Best exact algorithm | O(n²) time; O(nd) memory from stored centroids | NN-chain, O(n²) time and O(n²) memory | NN-chain, O(n²) time and O(n²) memory | MST or SLINK, O(n²) time and O(n) memory |
| Reach for it when | You want k-means-like segments plus a hierarchy | The similarity is cosine or correlation | Every cluster must stay within a maximum spread | Data are clean and shapes are non-convex |
Leave a Reply