Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Hierarchical clustering and linkage methods

Hierarchical clustering groups data by building a tree (dendrogram) using agglomerative or divisive strategies and different linkage rules (single, complete, average, centroid, Ward) to measure inter-cluster distance.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Hierarchical clustering is used when an engineer needs to discover nested group structure in data without specifying the number of clusters in advance — for example, in image segmentation, gene expression analysis, or grouping sensors by behaviour. It produces a dendrogram (tree) that shows cluster relationships at all scales so you can choose the clustering level that fits an application.

Key ideas

  • Clustering: unsupervised grouping of patterns so points in the same group are more similar than points in different groups.
  • Hierarchical clustering: constructs a nested sequence of partitions represented as a tree (dendrogram). Two main strategies:
    • Agglomerative: start with each pattern as its own cluster and repeatedly merge the two closest clusters until one cluster remains.
    • Divisive: start with all patterns in one cluster and repeatedly split clusters (less common in practice).

Assumptions: distance (or similarity) between individual patterns is available and meaningful. We assume a metric or at least a dissimilarity measure D(x,y) (often Euclidean distance).

Linkage methods (how to compute distance between clusters A and B):

  • Single linkage (nearest neighbour): distance between clusters = minimum distance between any pair (a in A, b in B). This tends to produce chains (can link distant regions through a chain of close points).
    • Formal: d_single(A,B) = min_{a in A, b in B} D(a,b).
  • Complete linkage (farthest neighbour): distance = maximum pairwise distance. Produces compact clusters.
    • Formal: d_complete(A,B) = max_{a in A, b in B} D(a,b).
  • Average linkage (UPGMA — Unweighted Pair Group Method with Arithmetic mean): distance = average of all pairwise distances between A and B; this equals a size-weighted average of inter-cluster distances when updating recursively (UPGMA). A different variant, WPGMA, uses equal weights for the two clusters regardless of size.
    • Formal: d_average(A,B) = (1 / (|A||B|)) * sum_{a in A} sum_{b in B} D(a,b).
  • Centroid linkage: distance between cluster centroids (means). Let m_A = (1/|A|) sum_{a in A} a. Then d_centroid(A,B) = D(m_A, m_B).
    • Note: centroid linkage can cause inversions in the dendrogram (non-monotonic distances) and may not define an ultrametric.
  • Ward's method: choose the merge that minimally increases the total within-cluster sum of squares (SSE). For Euclidean distance and clusters A and B with means m_A and m_B and sizes n_A, n_B: increase in SSE on merging is (n_A * n_B) / (n_A + n_B) * ||m_A - m_B||^2. This favours merges of clusters that keep clusters compact and approximately spherical.

Algorithm (agglomerative) — step by step (what to draw and label in an exam):

  1. Start with N clusters, each containing one pattern. Draw N leaves horizontally labelled by pattern IDs.
  2. Compute inter-cluster distance matrix according to chosen linkage.
  3. Find the pair of clusters with smallest distance. Merge them into a new cluster. Record the merge height = that linkage distance.
  4. Update the distance matrix: compute distances between the new cluster and all others using the linkage formula. In exams, you can use the Lance–Williams recurrence to update distances after merging U = A ∪ B with any other cluster C: d(U,C) = α_A d(A,C) + α_B d(B,C) + β d(A,B) + γ |d(A,C) − d(B,C)|. Coefficients: - Single: α_A=α_B=0.5, β=0, γ=−0.5. - Complete: α_A=α_B=0.5, β=0, γ=+0.5. - Average (UPGMA): α_A=|A|/(|A|+|B|), α_B=|B|/(|A|+|B|), β=0, γ=0. - Centroid (UPGMC): apply the recurrence to squared Euclidean distances with α_A=|A|/(|A|+|B|), α_B=|B|/(|A|+|B|), β=−(|A||B|)/( |A|+|B| )^2, γ=0. - Ward (minimum variance, with squared Euclidean): apply the recurrence to squared Euclidean distances with α_A=(|A|+|C|)/( |A|+|B|+|C| ), α_B=(|B|+|C|)/( |A|+|B|+|C| ), β=−|C|/( |A|+|B|+|C| ), γ=0.
  5. Repeat steps 3–4 until a single cluster remains. The sequence of merges and heights forms the dendrogram. In your diagram draw the vertical axis as "linkage distance" and horizontal leaves as data points. At each merge draw a horizontal line at the merge height connecting the two clusters being merged.

Complexity: naive implementation updates an O(N^2) matrix and searches O(N^2) per merge leading to O(N^3) time and O(N^2) space. Special algorithms exist: SLINK for single linkage runs in O(N^2) time and O(N) auxiliary space; CLINK for complete linkage also achieves O(N^2) time and O(N) space; efficient implementations of other linkages often achieve O(N^2) time.

When to choose which linkage:

  • Single: good for detecting elongated shapes, but sensitive to noise (chaining).
  • Complete: produces compact clusters, sensitive to outliers.
  • Average: compromise between single and complete.
  • Centroid and Ward: assume Euclidean geometry; Ward is preferred for compact spherical clusters.

Limitations and cautions:

  • Choice of distance metric and scaling of features strongly affects results — always standardise features if on different scales.
  • Dendrogram heights depend on linkage; different linkages give different trees.
  • Algorithm is greedy: early merges cannot be undone.

Formulas

d_single(A,B) = min_{a in A, b in B} D(a,b)

  • D(a,b): dissimilarity (often Euclidean distance) between patterns a and b; unit: unitless or same as feature units; applies for single linkage.

d_complete(A,B) = max_{a in A, b in B} D(a,b)

  • Same symbols and units as above; applies for complete linkage.

d_average(A,B) = (1 / (|A| · |B|)) · sum_{a in A} sum_{b in B} D(a,b)

  • |A|, |B|: number of points in clusters A and B (unitless); applies for average (UPGMA) linkage.

m_A = (1 / |A|) · sum_{a in A} a

  • m_A: centroid (mean vector) of cluster A; components have same units as features; applies for centroid and Ward methods.

d_centroid(A,B) = D(m_A, m_B)

  • Distance between centroids; applies for centroid linkage; may cause dendrogram inversions.

ΔSSE(A,B) = (|A| · |B|) / (|A| + |B|) · ||m_A - m_B||^2

  • ΔSSE: increase in within-cluster sum of squared errors when merging clusters A and B (units: squared feature units); |A|, |B| as above; ||·|| is Euclidean norm; applies for Ward's method under Euclidean distance.

Complexity_naive = O(N^3) time, O(N^2) space

  • N: number of patterns (unitless); applies to naive agglomerative implementation.

SLINK_single = O(N^2) time, O(N) additional space

  • For single-linkage specialized algorithm SLINK; applies only for single linkage.

Worked examples

Example 1 — 1D points, show single and complete linkage steps. Data (unitless): A=1, B=2, C=5, D=8. Distance matrix D(x,y) = |x - y|. Pairwise distances:

  • d(A,B)=1, d(A,C)=4, d(A,D)=7
  • d(B,C)=3, d(B,D)=6
  • d(C,D)=3 Agglomerative clustering steps (single linkage):
  1. Initial clusters: {A},{B},{C},{D}.
  2. Smallest distance = 1 between A and B. Merge -> cluster AB; merge height = 1.
  3. Update distances to AB using single linkage: d(AB,C)=min(d(A,C),d(B,C))=min(4,3)=3; d(AB,D)=min(7,6)=6. Distances among remaining clusters: d(C,D)=3.
  4. Smallest distance = 3 (tie between AB–C and C–D). If you merge C and D: merge CD at height 3. If tie-breaker different you may merge AB–C. Suppose we merge C and D -> CD height=3.
  5. Now clusters AB and CD remain. d(AB,CD)=min(d(A,C),d(A,D),d(B,C),d(B,D))=min(4,7,3,6)=3. Merge AB and CD at height 3. Dendrogram description to draw: leaves left-to-right A, B, C, D; first connect A and B at height 1; connect C and D at height 3; connect the two clusters at height 3 (show the last merge at same height as previous — OK for single linkage). Final clusters at different cut heights: cut at height <1 -> 4 clusters; cut between 1 and 3 -> 3 clusters (AB, C, D); cut >3 -> 1 cluster.

Agglomerative clustering steps (complete linkage) on same data:

  1. Merge A and B at height 1 (same as before).
  2. d(AB,C)=max(d(A,C),d(B,C))=max(4,3)=4. d(AB,D)=max(7,6)=7. d(C,D)=3.
  3. Next smallest is d(C,D)=3; merge C and D at height 3.
  4. d(AB,CD)=max of pairwise distances = max(4,7,3,6)=7. Merge at height 7. Dendrogram: A-B at 1, C-D at 3, then join at 7. Complete linkage gives larger final merge height than single linkage (shows more compact clusters).

Example 2 — 2D points and Ward's method (Euclidean). Data (meters, but treated unitless for clustering): P1=(0,0), P2=(0,1), P3=(3,0). Pairwise Euclidean distances:

  • d12 = sqrt((0-0)^2+(0-1)^2)=1.000
  • d13 = sqrt((0-3)^2+(0-0)^2)=3.000
  • d23 = sqrt((0-3)^2+(1-0)^2)=sqrt(10)=3.1623 Ward's criterion: compute ΔSSE for possible merges (clusters are singletons initially, |A|=|B|=1, m_A=point).
  • ΔSSE(P1,P2) = (1·1)/(1+1) * ||m1 - m2||^2 = 0.5 * (1^2) = 0.5
  • ΔSSE(P1,P3) = 0.5 * (3^2) = 4.5
  • ΔSSE(P2,P3) = 0.5 * (sqrt(10)^2) = 0.5 * 10 = 5.0 So merge P1 and P2 (smallest ΔSSE = 0.5). New cluster C12 has mean m = (0,0.5) and size 2. Compute ΔSSE between C12 and P3: ΔSSE(C12,P3) = (2·1)/(3) * ||(0,0.5)-(3,0)||^2 = (2/3)* ( (3)^2 + (0.5)^2 ) = (2/3) * (9 + 0.25) = (2/3)*9.25 = 6.1667. Merge at height 6.1667 if you use ΔSSE as the linkage value; some software scales or takes square roots (e.g., Ward.D vs Ward.D2), but the merge order is unchanged. Dendrogram: join P1 and P2 at height 0.5, then merge with P3 at height 6.1667. Final clusters: {P1,P2,P3}.

Notes: show numbers with units where relevant. Final answers are the cluster sequences and merge heights (explicit above).

Common mistakes

  • Mixing up single vs complete linkage formulas (min vs max).
  • Using centroid distance but claiming it always produces an ultrametric (centroid can cause inversions).
  • Forgetting that Ward's method assumes Euclidean distance and uses SSE; applying Ward with non-Euclidean dissimilarities is incorrect.
  • Not updating the distance matrix correctly after a merge (wrong formula for average linkage or wrong weights for sizes).
  • Mistaking dendrogram height for the number of clusters; the height is a distance/similarity measure, not a count.

For your exam

  • Short note: compare single, complete, average and Ward. Full marks: list formulas, typical shapes produced, sensitivity to noise, and computational complexity.
  • Derivation: show formula for ΔSSE in Ward's method. Full marks: start from within-cluster SSE definitions, show algebra that merging increases SSE by (n_A n_B/(n_A+n_B))||m_A - m_B||^2.
  • Numerical: perform agglomerative clustering on small dataset and draw dendrogram. Full marks: show distance matrix, merge steps in order, update matrix, list merge heights, and draw labeled dendrogram (vertical axis linkage distance).
  • Program: implement agglomerative clustering (single linkage) on given points. Full marks: correct algorithm, handle ties, produce merge order and heights.
  • Diagram question: draw dendrogram and explain how cutting at different heights gives different clusterings. Full marks: correctly labelled axes, leaves, and merge heights.

Quick check

  1. What is the single linkage distance between clusters A and B? (one-line formula)
  2. Which linkage tends to produce compact approximately spherical clusters and uses SSE minimisation?
  3. For Ward's method merging clusters of sizes 4 and 6 with centroids 5 units apart, what is ΔSSE? (compute number) Answers: 1) d_single(A,B) = min_{a in A, b in B} D(a,b). 2) Ward's method. 3) ΔSSE = (4·6)/(4+6) · (5^2) = (24/10) · 25 = 2.4 · 25 = 60.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?