Hierarchical & Density-Based Clustering
Dendrograms, linkage, DBSCAN and an impossibility theorem.
A dendrogram answers every "how many clusters?" question at once, and density-based methods let the data decide how many clusters there are and which points are noise. This lesson derives the classic linkages, proves that single linkage is a minimum spanning tree in disguise, makes DBSCAN, OPTICS and HDBSCAN precise, and proves Kleinberg's theorem that no clustering function can satisfy three natural axioms at once.
What you’ll learn
- Run agglomerative clustering with single, complete, average and Ward linkage, derive Ward's merge cost, and write all four as Lance–Williams updates.
- Prove that single-linkage clusters are the components of a minimum spanning tree with its heaviest edges removed, and explain chaining and the consistency of single linkage for density clusters.
- Define DBSCAN's core, border and noise points exactly, and explain how OPTICS and HDBSCAN replace the fixed radius with a hierarchy.
- State and prove Kleinberg's impossibility theorem, and describe the ways around it.
Agglomerative clustering and its linkages
Agglomerative hierarchical clustering starts with singleton clusters and repeatedly merges the two clusters with the smallest linkage distance , recording the height at which each merge happens. After merges everything is one cluster, and the record of merges is a dendrogram. Cutting it at any height gives a flat clustering, so one run serves every . The linkage decides what "closest clusters" means.
| Linkage | Typical behavior | |
|---|---|---|
| single | long, chained clusters; follows nonconvex shapes; a thin bridge of noise can merge two clusters | |
| complete | compact clusters of similar diameter; sensitive to outliers | |
| average | a compromise between the two | |
| Ward | , the rise in within-cluster sum of squares | compact, k-means-like clusters, for Euclidean data |
Let be the centroid of . The centroid identity, , applied to and to around :
Since and :
Ward's method merges the pair with the smallest , so each merge raises the total within-cluster sum of squares as little as possible. It is a greedy heuristic for the k-means objective, not an exact solver: an early merge can never be undone.
Two clusters of sizes 2 and 3 have centroids 4 apart. By how much does merging them raise the within-cluster sum of squares?
Show the answer
19.2
. The factor , half the harmonic mean of the sizes, makes Ward reluctant to merge two large clusters and willing to absorb small ones.
All four linkages can be updated from the old distances alone. After merging clusters and , the distance from any other cluster to the merged one is given by the Lance–Williams recurrence (Lance & Williams, 1967):
| Linkage | ||||
|---|---|---|---|---|
| single | ||||
| complete | ||||
| average | ||||
| Ward (with ) |
Storing the distance matrix takes memory, and naive merging time. For linkages with a reducibility property, which these four have, the nearest-neighbor-chain algorithm needs only time, and single linkage runs in time with memory (SLINK: Sibson, 1973). The same four produce monotone dendrograms, with merge heights that never decrease; centroid and median linkage can produce inversions, where a later merge sits lower than an earlier one.
Single linkage is a minimum spanning tree
Kruskal's algorithm scans pairs in increasing order of and accepts a pair when it joins two different components. Suppose it accepts , joining components and . Any closer pair with , was scanned earlier, when and were already in different components (components only grow, and now), so it would have been accepted and merged them. Hence:
The same argument shows that no other pair of current components is closer in single linkage: its closest pair would have been scanned, and accepted, earlier. So each accepted edge performs exactly the merge that single linkage performs next, at the same height:
Kruskal's accepted edges form a minimum spanning tree. Stopping after of them leaves components; the edges not yet accepted are the heaviest edges of .
Three consequences. Single linkage costs no more than a minimum spanning tree: with Prim's algorithm on the complete graph, and less for low-dimensional Euclidean points, whose spanning tree can be found without examining every pair. The height at which and merge is the largest edge on the tree path between them, the minimax path distance, which is an ultrametric. And single linkage chains: only the closest pair counts, so a trail of points spaced more tightly than the gap between two clusters merges them, while one isolated point splits off before any real structure does.
Two dense blobs 10 units apart are joined by a thin chain of points spaced 0.3 apart. Inside the blobs, nearest neighbors are 0.2 to 0.3 apart, and one stray point has its nearest neighbor 1.5 away. What does single linkage return with ?
- The two blobs, each with half of the chain
- The chain as one cluster and both blobs as the other
- The stray point on its own, and everything else together
- It depends on the order in which the points are listed
Show the answer
C. The stray point on its own, and everything else together
With , single linkage deletes the heaviest edge of the minimum spanning tree. Every edge along the chain and inside the blobs is at most 0.3, while the stray point's edge is 1.5. So the stray point is split off, and the two blobs stay chained together. This is why single linkage is usually run with noise handling, as in the density-based methods below.
Is single linkage statistically sound? Hartigan proposed a target for hierarchical clustering: for a density , the clusters at level are the connected components of , and all levels together form the cluster tree. An estimated hierarchy is consistent if, for any two disjoint such components and , the smallest estimated clusters containing the sample points of and of are disjoint with probability tending to 1. Hartigan (1981) proved that single linkage is consistent in one dimension but not in two or more, where sample points form chains across low-density gaps (a continuum-percolation effect); it is only fractionally consistent. A robust variant that connects points only where the -nearest-neighbor radius is small, that is in high-density regions, is consistent, with finite-sample rates and lower bounds on the sample size that any method needs (Chaudhuri, Dasgupta, Kpotufe & von Luxburg, 2014).
Carlsson & Mémoli (2010) found a complementary characterization. Treat a hierarchical method as a map from finite metric spaces to dendrograms, viewed as ultrametrics; under three natural conditions, one of which is in the spirit of Kleinberg's consistency axiom below, single linkage is the unique such method, and it is stable under small perturbations of the input. Complete and average linkage are not stable in their sense, and their dendrograms can depend on how ties are broken.
Density-based clustering: DBSCAN, OPTICS and HDBSCAN
The core points of each DBSCAN cluster form a connected component of the graph that joins core points at distance at most ; each cluster adds the border points within of its cores. That part is unique. A border point within of core points from two clusters can join either, so its label can depend on the order in which points are processed; the variant DBSCAN* treats border points as noise and removes the ambiguity. For choosing , the original paper sorts every point's distance to its 4th nearest neighbor, sets at the first "valley" of that curve, and fixes for two-dimensional data.
Speed is subtler than first claimed. The original paper's was an average case resting on a spatial index; in the worst case the algorithm is quadratic. Gan & Tao (2015) proved that for exact DBSCAN needs about time unless a geometric problem (unit-spherical emptiness checking) that is widely believed, like Hopcroft's problem, to need that much can be solved faster, and that a slightly relaxed, -approximate DBSCAN runs in expected time in any fixed dimension; in two dimensions an exact algorithm exists. Schubert, Sander, Ester, Kriegel & Xu (2017) replied that with sensible parameters and indexes, DBSCAN remains practical.
With and radius , a point has two other points within distance , so , and one of those two is a core point. What is ?
- A core point, because it has neighbors
- A border point of the core point's cluster
- Noise, because it is not a core point
- The seed of a new cluster
Show the answer
B. A border point of the core point's cluster
fails the core condition (, counting itself), but it lies within of a core point, so it is directly density-reachable from it and joins that cluster as a border point. Only points that are neither core nor within of any core point are noise.
One radius cannot fit clusters of different densities. OPTICS (Ankerst, Breunig, Kriegel & Sander, 1999) avoids choosing it. A point's core distance is the radius at which it would become a core point, and the reachability distance of from is . Visiting points in order of smallest reachability produces a plot in which clusters appear as valleys, for every up to a maximum at once.
HDBSCAN (Campello, Moulavi & Sander, 2013) turns this into a hierarchy and then selects from it. With the distance from to its -th nearest neighbor (counting ), define the mutual reachability distance
Two points are joined at level exactly when both are core points at radius and lie within of each other, so single linkage on , computed through a minimum spanning tree, contains the DBSCAN* clustering for every at once. HDBSCAN condenses this tree by ignoring splits that shed fewer points than a minimum cluster size, then picks the flat clusters with the greatest stability, with : clusters that persist over a long range of density levels win. Everything outside them is noise.
Kleinberg's impossibility theorem
Forcing pairs. Let be consistent and . Choose with at most every within-cluster distance of and at least every between-cluster distance. Any whose within-cluster distances are at most and whose between-cluster distances are at least is a -transformation of , so consistency gives:
Suppose are both in the range and refines , with forcing pairs and . Pick and , and build in three bands. Then is -forced: inside 's clusters every distance is at most (as ), and across them at least . So :
Now rescale by . Inside 's clusters, ; across them, (pairs across are farther still, as ). So is -forced, while scale-invariance demands :
Richness would put every partition in the range, including the singletons and the single cluster . For the first is a proper refinement of the second, so the set of all partitions is not an antichain. That proves the theorem.
| Single linkage, stopped by… | Scale-invariant | Rich | Consistent |
|---|---|---|---|
| clusters: stop when components remain | yes | no | yes |
| distance : add only edges of length at most | no | yes | yes |
| scale : add only edges up to times the largest distance () | yes | yes | no |
Think first DBSCAN with and a fixed : which of Kleinberg's axioms does it satisfy?
With every point is a core point (its neighborhood contains itself), so the clusters are the connected components of the graph joining points at distance at most . That is single linkage with the distance- stopping rule: rich and consistent, but not scale-invariant. Doubling every distance can break clusters apart, because is a length scale built into the method.
Kleinberg also showed that centroid-based clustering functions that choose of the input points as centers, including the -median function, violate consistency once is large compared with . Weakening the axioms helps only so much. With refinement-consistency (a -transformation may only refine the clustering) the impossibility survives, but barely: some scale-invariant, refinement-consistent functions produce every partition except the singletons. Changing the output helps more. Asking for a dendrogram instead of a partition turns non-existence into existence and uniqueness, the single-linkage theorem of Carlsson & Mémoli above.
Key takeaways
- Agglomerative clustering merges the closest pair of clusters under a linkage (single, complete, average or Ward), and the merges form a dendrogram that serves every ; all four follow the Lance–Williams recurrence, and Ward's merge cost is .
- Single linkage is Kruskal's algorithm: its clusters for are the components of a minimum spanning tree minus its heaviest edges. It chains through bridges of points, and it is consistent for Hartigan's cluster tree only in one dimension, while robust variants are consistent in general.
- DBSCAN defines clusters as density-connected sets of core points with their border points, and labels the rest noise; OPTICS and HDBSCAN remove the single radius, and HDBSCAN selects the clusters that persist longest across density levels.
- Kleinberg's theorem: no clustering function is scale-invariant, rich and consistent, because the first and third force the range to be an antichain. Each method gives up one axiom; with dendrograms as output, single linkage is the unique method satisfying an analogous set of axioms.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Ward's method considers merging a cluster of 2 points with a cluster of 3 points whose centroids are 2 apart. What is the merge cost ?
- 2.4
- 4.8
- 9.6
- 20
Show the answer
B. 4.8
: the increase in the total within-cluster sum of squares caused by the merge.
How do you obtain the single-linkage clustering into clusters from a minimum spanning tree of the points?
- Delete the heaviest edges of the tree and take the connected components
- Delete the lightest edges of the tree
- Keep only the heaviest edges
- Cut every edge longer than the median edge
Show the answer
A. Delete the heaviest edges of the tree and take the connected components
Kruskal's algorithm performs single linkage's merges in order, so the last edges it would add, the heaviest in the tree, are exactly the merges that have not yet happened when clusters remain.
Single linkage with the distance- stopping rule (merge only along edges of length at most ) satisfies which two of Kleinberg's axioms?
- Scale-invariance and richness
- Scale-invariance and consistency
- Richness and consistency
- All three, for a suitable
Show the answer
C. Richness and consistency
Any partition is reachable by making within-cluster distances below and the rest above it (richness). Shrinking within-cluster distances and stretching between-cluster ones keeps every within-cluster edge below and every between-cluster distance above it, so the output is unchanged (consistency). But rescaling all distances can push edges across , so it is not scale-invariant.
Why can DBSCAN's output depend on the order in which points are processed?
- Core points are chosen at random
- The number of clusters is estimated from a random sample
- The radius is adjusted as clusters grow
- A border point within of core points from two clusters can be assigned to either
Show the answer
D. A border point within of core points from two clusters can be assigned to either
Core points and the cluster structure among them are determined by the data alone. A border point is not core, so it cannot connect clusters, but it may be reachable from both; it joins whichever cluster reaches it first. DBSCAN* labels such points noise, which makes the result order-independent.
Further reading
- J. H. Ward Jr., "Hierarchical grouping to optimize an objective function," Journal of the American Statistical Association 58(301), 236–244 (1963).
- J. C. Gower & G. J. S. Ross, "Minimum spanning trees and single linkage cluster analysis," Journal of the Royal Statistical Society C (Applied Statistics) 18(1), 54–64 (1969).
- M. Ester, H.-P. Kriegel, J. Sander & X. Xu, "A density-based algorithm for discovering clusters in large spatial databases with noise," Proceedings of the 2nd International Conference on Knowledge Discovery and Data Mining (KDD-96), 226–231 (1996).
- R. J. G. B. Campello, D. Moulavi & J. Sander, "Density-based clustering based on hierarchical density estimates," Advances in Knowledge Discovery and Data Mining (PAKDD 2013), Lecture Notes in Computer Science, 160–172 (2013).
- J. Kleinberg, "An impossibility theorem for clustering," Advances in Neural Information Processing Systems 15 (NIPS 2002), 463–470 (MIT Press, 2003).