Hierarchical Clustering and Dendrograms

Merge the two closest clusters, repeat, and keep the whole tree. Choose how many clusters you want afterwards.

Overview

The algorithm

Agglomerative clustering is almost trivially simple:

  1. Every point starts as its own cluster.
  2. Find the two closest clusters and merge them.
  3. Repeat until one cluster remains.

Recording the distance at each merge gives a dendrogram — the chart above plots exactly that: merge number against the distance at which it happened.

Hierarchical Clustering and Dendrograms

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Start with every point as its own cluster. Repeatedly merge the two closest, until one remains.
The result is a tree. Cutting it at a height gives a clustering — and every height gives a different k.
Linkage defines the distance between two clusters, and changes the answer more than any other choice.
Single linkage chains through bridges; complete linkage insists on tight clusters; average sits between them.

Hierarchical Clustering and Dendrograms

Building the whole tree once, and choosing how many clusters you want after seeing it.

The point: k comes last

K-means and DBSCAN both need a decision before they run — how many clusters, or how dense. Hierarchical clustering needs neither. It builds one tree, and a horizontal cut through it produces a clustering.

Drag the cut height and watch the count change. A cut low down leaves many small clusters, a cut high up leaves few, and the tree itself never changed. That is genuinely useful when you do not know what structure to expect, because you can look at the merge distances before committing.

Large jumps in the curve are informative: a merge that costs much more than the one before it joined two things that were not really close. Cutting just below a big jump is the dendrogram equivalent of the [elbow](choosing_k.html).

Linkage changes everything

The algorithm needs a distance between two *clusters*, not two points, and there is no single right answer. Switch the control and the shape of the curve changes substantially.

Single linkage — distance between the nearest pair. Merges cheaply whenever any two members are close, so it can follow elongated shapes. Its failure mode is chaining: two well-separated blobs joined by a thin bridge of points merge into one, because the bridge provides a short hop at every step.

Complete linkage — distance between the furthest pair. A merge is only cheap if *every* member of one cluster is close to every member of the other, so clusters come out compact and roughly equal in diameter. It breaks up elongated structures that genuinely belong together, and is sensitive to outliers, since one distant member sets the whole distance.

Average linkage — the mean over all cross-pairs. Between the two, and the usual default.

Ward linkage, not offered here, merges the pair that increases within-cluster variance least. It behaves much like k-means and is the most common choice for roughly spherical clusters.

LinkageProducesFails at
Singleelongated, chain-followingbridges between clusters
Completecompact, similar sizedgenuinely elongated clusters
Averagea compromisenothing dramatic
Wardspherical, balancednon-convex shapes

Reading a real dendrogram

A conventional dendrogram draws each merge as a bracket at its height, with the leaves at the bottom. Two things are worth knowing about reading one.

Height is meaningful; horizontal position is not. The left-to-right order is chosen to keep the drawing untangled and carries no information. Two adjacent leaves are not necessarily similar.

The tree is greedy and permanent. Once two clusters merge they never separate, so an early mistake — two points joined because of noise — propagates all the way up. Hierarchical clustering does not revisit decisions.

Cost

The naive algorithm is O(n³), and careful implementations reach O(n² log n) with O(n²) memory for the distance matrix. That memory is usually the binding constraint: 100,000 points is a matrix of ten billion distances.

This is why hierarchical clustering is a small-to-medium-data method. Above a few tens of thousands of points, k-means or a sampled variant is the practical choice.

Where it goes wrong

Not scaling the features. As with every distance-based method.

Using single linkage on data with any bridge between clusters. Chaining will merge them.

Reading meaning into leaf order. It is a drawing convention.

Trying it on a large dataset. The distance matrix will not fit.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is the advantage of building the whole tree?

  2. What is chaining, and which linkage causes it?

  3. Why is hierarchical clustering impractical for large datasets?

Cheat sheet

Hierarchical Clustering and Dendrograms

Recording the distance at each merge gives a dendrogram — the chart above plots exactly that: merge number against the distance at which it happened.

MACHINE LEARNING · vizlearn.in/machine_learning/hierarchical_clustering.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.