ML Systems Lab Open interactive version →
Intermediate 34 min read hierarchical clusteringdendrogramlinkage

Hierarchical Clustering

Linkage criteria, dendrograms, when hierarchy beats flat clustering

You have 500 customer support tickets. You need to organize them into a hierarchy — broad themes (Product Bug, Billing, UX) and sub-themes (Product Bug: Login, Checkout, API). K-means gives you flat clusters and requires you to commit to K before seeing results. Hierarchical clustering builds a tree — the dendrogram — that you cut at any level to get the granularity you want. One run gives you every possible K simultaneously.

Agglomerative clustering (bottom-up): start with each point as its own cluster. Merge the two closest clusters. Repeat until all points are in one cluster. The merge history forms the dendrogram, where the y-axis records the distance at each merge.

The linkage criterion determines what "distance between clusters" means. Single linkage: distance between the closest pair of points across clusters — produces elongated "chaining" clusters. Complete linkage: distance between the farthest pair — produces compact clusters. Average linkage: average distance between all pairs. Ward's linkage: merges the pair that minimizes total within-cluster variance after merging. Ward produces compact, equal-variance clusters and is almost always the best default.

Reading the dendrogram: long vertical segments mark natural cluster boundaries — a large jump in merge distance means the two groups being merged were genuinely far apart. Draw a horizontal cut line; each branch it crosses is one cluster. If there are no long segments, the data probably does not have discrete structure.

Complexity: O(n²) space for the distance matrix, O(n³) time for the naive merge sequence. Infeasible for n > 10,000. For large datasets use HDBSCAN or approximate methods.

NOT-this: "You need to specify K before running hierarchical clustering." The whole point is you do not. You run once, get the full dendrogram, inspect it, and decide where to cut based on the structure. You can cut at different heights for different analysis needs without rerunning — that is the main advantage over K-means.

Key points

Takeaway

The dendrogram encodes cluster structure at every granularity in one run — natural boundaries appear as long vertical segments, and the absence of those segments means discrete cluster structure is probably not there.

Recap

Check your understanding

Q1. You are clustering genes based on expression profiles. Why might hierarchical clustering with Ward linkage be more appropriate than k-means?

Q2. Two researchers use the same dataset but different linkage criteria. Researcher A uses single linkage and gets one large cluster containing 95% of the data. Researcher B uses Ward linkage and gets 5 balanced clusters. Which two statements correctly explain when each researcher is right?

Q3. How do you determine the optimal cut height on a dendrogram when there is no obvious long gap?

Q4. A colleague wants to run hierarchical clustering on 100,000 points. What do you tell them?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →