k-means makes you commit to a number of clusters before you've even looked at the data. What if, instead, you built every possible clustering at once — from 5 separate points all the way down to 1 big cluster — and picked afterward?
Step through the merges one at a time. The closest pair joins first, then the next-closest pair or point, and so on, until everything is one cluster. Nothing here ever needed to be told how many groups to expect.
Agglomerative clustering starts with every point in its own cluster and repeatedly merges the two closest clusters, using single linkage — the distance between a pair of clusters is the distance between their single closest members:
- — two clusters, each a set of points.
- — a point from and a point from .
- — the smallest distance between any pair drawn one from each cluster.
- Start with n singleton clusters
Every point begins as its own one-member cluster — no assumption about count or shape yet.
- Merge the closest pair of clusters
Compute between every pair of current clusters, and merge whichever pair is smallest.
- Record the merge height
That minimum distance becomes the merge's height — how far apart the two groups were when they joined. Stacking every merge by height, in order, draws the dendrogram.
- Repeat until one cluster remains
Keep merging the closest pair, one merge at a time, until every point belongs to a single cluster. Cutting the finished dendrogram at any height recovers a full clustering — no re-running required.
Cut near height 0 and every point is alone. Cut anywhere between the tight inner merges and the one big final jump, and the two true groups fall right out. Push the cut past that final jump, and everything collapses into a single cluster — the same tradeoff posed for k-means, now expressed as where to slice, not how many bins to pre-declare.
Five points on a line — close together, then close together, with a wide gap between the two groups:
- Closest pair merges first, at height 1
Points at and are exactly apart — the smallest distance anywhere in the data. They merge first, at height .
- The third point joins at the same height
Point is distance from point (single linkage only needs the closest member) — it joins , also at height .
- The other pair merges independently, also at height 1
Points and merge into their own cluster, at height — completely unrelated to the first group so far.
- The two groups finally join at height 4
The closest pair across the groups is and , distance apart — a jump four times larger than any merge before it. That's the dendrogram's one big gap, and exactly where cutting recovers the two true clusters.
Drag the cut height until the dendrogram splits into exactly 2 clusters — the two true groups, A, B, C and D, E.
Agglomerative clustering trades k-means' single answer for the entire family of clusterings, stacked by merge height in a dendrogram — you decide how many clusters you want after seeing the structure, by choosing where to cut. The cost is speed: computing every pairwise cluster distance at every merge is far more expensive than k-means' handful of centroid updates, which is exactly why k-means remains the default choice for very large datasets.