Hierarchical Clustering

Building a tree of nested clusters — either by merging (agglomerative) or splitting (divisive) — visualized as a dendrogram.

Hierarchical clustering — cut the dendrogram to choose K clusters
ABCDEF2 clusters at cut height 0.42cut0.130.160.210.300.77ABCDEFdist0
cut=0.42

Drag to cut the dendrogram — 2 clusters result

Definition

Hierarchical clustering builds a tree of nested clusters — a dendrogram — rather than a flat partition. You don't need to specify KK in advance; you cut the tree at the desired level to get any number of clusters.

Two strategies:

  • Agglomerative (bottom-up): start with each point as its own cluster, then repeatedly merge the two closest clusters
  • Divisive (top-down): start with one cluster containing all points, then recursively split

Agglomerative is far more common.

Linkage criteria define "distance between clusters" (since clusters contain multiple points):

  • Single linkage: distance = min pairwise distance (nearest neighbor)
  • Complete linkage: distance = max pairwise distance (farthest neighbor)
  • Average linkage: distance = mean pairwise distance
  • Ward linkage: minimize total within-cluster variance after merging
Key properties
  • Produces a full hierarchy of clusterings at once — any number of clusters can be read off by cutting at a different height
  • Merges are irreversible: once two clusters combine, they stay combined for the rest of the algorithm
  • Deterministic given a fixed linkage criterion — unlike k-means, there's no randomness from initialization
  • Doesn't require choosing KK in advance, only at the end (when cutting the dendrogram)
Common mistakes
  • Running it on very large datasets: naive agglomerative clustering's O(n2)O(n^2) memory and O(n3)O(n^3) time make it impractical past a few thousand points without specialized algorithms
  • Using single linkage when you expect compact clusters: the chaining effect can merge distinct dense groups through a thin bridge of intermediate points
  • Treating dendrogram structure as ground truth: greedy, irreversible merges mean an early suboptimal merge can never be undone later
Reading a dendrogram

A dendrogram has individual points as leaves. The height at which two branches merge indicates the distance at which those clusters were combined. Cut the dendrogram at height hh: any branches below hh are merged; any branches above hh stay separate. The number of cut lines you cross is the number of clusters.

Try it

Five points with pairwise distances: d(A,B)=1d(A,B)=1, d(C,D)=1.5d(C,D)=1.5, d(A,C)=3d(A,C)=3, d(B,C)=3.5d(B,C)=3.5, d(A,D)=d(B,D)=4d(A,D)=d(B,D)=4, d(E,)=6d(E,\cdot)=6 for all others. Trace the first two merges under single-linkage clustering.

Solution

Merge 1: smallest distance is d(A,B)=1d(A,B)=1. Merge AA and BB into cluster {A,B}\{A,B\}.

Merge 2: recalculate distances. d({A,B},C)=min(d(A,C),d(B,C))=min(3,3.5)=3d(\{A,B\},C) = \min(d(A,C),d(B,C)) = \min(3, 3.5) = 3 (single linkage). d(C,D)=1.5d(C,D) = 1.5 is still the smallest. Merge CC and DD into {C,D}\{C,D\}.

After these two merges we have: {A,B}\{A,B\}, {C,D}\{C,D\}, {E}\{E\}.

Related concepts