The idea: merge the closest pair, again and again

Agglomerative hierarchical clustering starts with every point as a cluster of its own. Then it repeats one move: find the two clusters that are closest together and merge them. With n points that takes n − 1 merges, and at the end everything is one cluster. You do not choose the number of clusters up front. The result is a whole family of clusterings, one for every number from n down to 1, drawn as a tree called the dendrogram.

Left: five points A to E with numbered merge rings: A and B merge first, then D and E, then C joins D and E, and finally the two groups join. Right: the matching dendrogram, with each merge drawn as an upside-down U at the height of its distance; the last merge is the highest
Each merge becomes a ∩ in the dendrogram, drawn at the height of the distance between the two clusters it joins.

The algorithm

  1. Make one cluster per point.
  2. Compute the distance between every pair of clusters.
  3. Merge the pair with the smallest distance. Record the merge and its distance.
  4. Repeat from step 2 until one cluster is left.

On the canvas each merge takes three Steps: the closest pair is ringed and joined by an orange line, a box with its distance d appears; the box flies to the dendrogram; then the ∩ is drawn and the two clusters take one colour. The narration also names the runner-up pair, so you can see how close the race was. (Demo 1: build the dendrogram: 11 merges for 12 points, the first ones inside the blobs at heights 1.03 to 1.85, the last two between blobs at 5.64 and 5.96.)

Linkage: what "closest" means for two clusters

The distance between two points is clear. Between two groups of points there is a choice, called the linkage:

LinkageDistance between clusters A and BEffect
singlethe closest pair: min d(a, b)follows chains; can glue groups through a thin line of points
completethe farthest pair: max d(a, b)compact, similar-diameter clusters; sensitive to outliers
average (UPGMA)the mean of all |A|·|B| point distancesbetween single and complete
Wardthe increase in within-cluster sum of squares; scipy draws it as √(2·|A|·|B| / (|A|+|B|)) · ‖cA − cB‖round clusters of similar size, like K-Means; the default in scikit-learn
The same two clusters drawn three times. Single linkage: a short line between the two nearest points, one in each cluster. Complete linkage: a long line between the two farthest points. Average and Ward: a line between the two centroids, marked with crosses
Three ways to measure the gap between two clusters: nearest points, farthest points, or centroids.

The page joins the pair that defines the distance with an orange line: the two nearest points for single linkage, the two farthest for complete, and the two centroids (orange squares) for average and Ward, where the distance is a mean or a variance and no single pair defines it. With all four linkages the merge heights never go down from one merge to the next, so the dendrogram has no crossings. (Centroid and median linkage can produce such inversions; the page leaves them out.)

Reading and cutting the dendrogram

The height of a ∩ is the distance at which its two branches were joined. To get k clusters, draw a horizontal line that crosses exactly k vertical branches: everything hanging below one crossing is one cluster. A long vertical stretch with no merges means the groups below it are well separated, so the biggest gap between two consecutive merge heights is a natural place to cut.

  • Demo 2: cut at the biggest gap: average linkage on the three blobs. The heights jump from 1.85 to 5.64, so the cut at 3.74 gives 3 clusters {A,B,C,D} {E,F,G,H} {I,J,K,L}.
  • Demo 6: cut lower: at 1.64 the same tree gives 5 clusters; the blobs E–H and I–L each fall apart in two.
  • Demo 5: Ward linkage: the same 3 clusters, but the top merges are at 11.14 and 12.01: Ward's heights are on a different scale, so compare the shape of the tree, not the numbers, across linkages.

Single linkage and chaining

Single linkage only needs one close pair to merge two clusters. On the second data set two groups are joined by a line of points about 1 apart, and that is enough: every merge happens between 0.82 and 1.36, the dendrogram has no gap, and cutting it into 2 clusters splits off just {A,B} from the other 12 points (Demo 3). This is chaining. Complete linkage on the same data needs every pair within the distance, so the long chain cannot merge cheaply: the top merge is at 8.02 after a jump from 4.36, and the cut gives the left group with half the bridge, {A,…,H}, and the right group {I,…,N} (Demo 4).

Single linkage is the same as building a minimum spanning tree with Kruskal's algorithm: sort the point pairs by distance and join with a union-find structure; each MST edge is one merge, and its length is the merge height. Cutting the dendrogram into k clusters = deleting the k − 1 longest MST edges.

Cost and how it compares

The page's naive version recomputes all pair distances after every merge: O(n³) time. Real libraries keep an n × n distance matrix and update only the new cluster's row with the Lance–Williams formula, which gives O(n²) memory and O(n²) to O(n² log n) time (SLINK for single linkage, nearest-neighbour chains for the others). Either way the n² matrix limits it to tens of thousands of points.

HierarchicalK-MeansDBSCAN
number of clusterschosen after, by the cutK chosen beforefollows from eps, minPts
resulta whole tree of nested clusteringsone partitionone partition + noise
shapesdepends on the linkageround blobsany shape
costO(n²) memoryO(n·K) per iterationabout O(n log n) with an index
deterministicyes (ties aside)depends on the startyes, except some border points

Like any distance-based method (see k-nearest neighbours), features on different scales should be standardized first, or the largest one decides every merge.

What the page leaves out

The data is 2-D with at most 14 points and Euclidean distance; other metrics (Manhattan, cosine, correlation for gene expression) plug into the same loop. The page does not show the distance matrix, the Lance–Williams update, centroid or median linkage and their inversions, divisive (top-down) clustering, connectivity constraints, or measures for picking the cut such as the cophenetic correlation or the silhouette score. Ties are broken by cluster order; with real data ties are rare but can change the tree.

On the mind map

This page on the Machine Learning mind map · 62 pages in 10 branches · open it to see the whole map and test yourself in recall mode.