Hierarchical Clustering
Hierarchical Clustering — Build a tree of clusters by repeatedly merging the closest pair.
Hierarchical clustering doesn’t need you to pick the number of clusters up front. It merges the two closest points, then the next closest, building a tree (dendrogram) you can cut at any level.
- Clusters at cut: 24
- Most recent join
- Hovered join
- Cut line
Linkage controls
The idea in plain words
Hierarchical clustering doesn’t need you to pick the number of clusters up front. It repeatedly merges the two closest groups — starting from single points — building a tree called a dendrogram. Cut that tree at any height and you get that many clusters.
Drag the cut line and the clusters reorganize live. The linkage rule (how “distance between groups” is defined) matters enormously: single linkage can chain distant points into one straggly cluster, unlike the tighter groups k-means would find.
Now, the math
Each step merges the two clusters with the smallest linkage distance:
- the distance between clusters A and B under the chosen linkage.
- the distance between two individual points.
▸ Show the derivation
The dendrogram’s vertical axis is the merge height. Cutting at height h keeps every merge below h and separates the rest, so lowering the cut yields more, smaller clusters. Single linkage merges on the single closest pair, so a thin bridge of points can fuse two otherwise-distinct clusters — the chaining effect.
Trace it by hand
Four points on a line: a = 0, b = 1, c = 4, d = 5 (all distances are plain gaps on the line). We build the dendrogram bottom-up and see how the linkage choice changes the final merge height.
Step 1 — all pairwise point distances
Every point starts as its own cluster; each step merges the closest pair of clusters.
Step 2 — the two closest pairs merge first
Two merges at height 1 leave two clusters: the pair on the left and the pair on the right.
Step 3 — the linkage rule decides the final merge height
Same clusters, three different merge heights — the dendrogram's shape depends on the linkage, not just the data.
Step 4 — cut the single-linkage tree at height 2
Cutting keeps every merge below the line: 2 clusters here, 4 clusters if you cut below 1, and 1 cluster above 3.
What just happened: The merge sequence was identical, but the last merge happened at height 3, 4, or 5 depending on the linkage — and a cut at height 2 recovered the two natural pairs without ever choosing k in advance.
Now Break It
Try this: The wrong linkage criterion (single linkage) chains distant points into one straggly cluster.
Control: Linkage method selector (set to single)
What happens: Chaining effect! Single linkage strings distant points together into one straggly cluster.
Where hierarchical clustering is used
Hierarchical clustering builds a tree of nested groups, which makes it valuable whenever the relationships between items matter as much as the final partition. Biologists use it on gene expression data to produce heatmaps with dendrograms, revealing which genes and samples behave similarly across conditions. It appears in phylogenetics to reconstruct how species relate, in document organization to build topic hierarchies, and in market research to see how products or customers nest into progressively broader groups. Because the agglomerative version repeatedly merges the closest pair of clusters, it yields a full dendrogram that you can cut at any level, letting analysts explore coarse or fine groupings from a single run without committing to a number of clusters up front.
A frequent misunderstanding is treating the dendrogram's horizontal arrangement as meaningful; only the merge heights, which encode distance, carry information, and branches can be rotated freely without changing the structure. Another pitfall is ignoring the linkage choice. Single linkage tends to chain points into straggly clusters, complete linkage favors compact ones, and Ward's method targets low within-cluster variance, so the same data can yield very different trees depending on the rule. People also underestimate the cost: standard agglomerative clustering scales roughly with the square or cube of the number of points, making it impractical for very large datasets. Finally, merges are greedy and permanent, so an early mistake cannot be undone later.
Frequently asked questions
What is a dendrogram and how do I read it?
What is the difference between agglomerative and divisive clustering?
Which linkage method should I use?
How is hierarchical clustering different from K-Means?
Does hierarchical clustering scale to large datasets?
Written & reviewed by the ML Visualization team · Last updated .