Agglomerative Clustering Linkage Matrix

Quick Overview

Implement single and complete linkage agglomerative clustering from a square distance matrix and return a SciPy-format linkage matrix. It tests the merge loop, distance updates after each merge, cluster numbering and deterministic tie-breaking.

Agglomerative Clustering Linkage Matrix

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `agglomerative_linkage(D, method="single")` that clusters N points from a square distance matrix `D` using `"single"` (min) or `"complete"` (max) linkage. Return an `(N-1) x 4` float array where row `k` is `[id_a, id_b, distance, size]` for the k-th merge (SciPy format). - Original points are ids `0..N-1`. The cluster formed at step `k` gets id `N + k`. - Each row has `id_a < id_b`. `size` is the merged cluster's point count. - Distance ties go to the lexicographically smallest `(id_a, id_b)` pair. - Unknown `method`, a non-square `D` or an empty `D` raises `ValueError`. Example: ```python x = np.array([0, 1, 3, 7]) D = np.abs(x[:, None] - x[None, :]) # D[i][j] = |x[i] - x[j]| agglomerative_linkage(D, "single") # [[0, 1, 1, 2], # [2, 4, 2, 3], # [3, 5, 4, 4]] agglomerative_linkage(D, "complete") # [[0, 1, 1, 2], # [2, 4, 3, 3], # [3, 5, 7, 4]] ``` ```hint Distances to a merged cluster For single and complete linkage, the distance from a newly merged cluster to any other cluster follows from the two old clusters' distances alone. You never need to revisit the points. ``` ```hint Tie-breaking If you scan candidate pairs in ascending `(id_a, id_b)` order, think about which comparison keeps the first minimum you find. ``` ### Constraints & Assumptions - `N` is between 1 and 100. `N = 1` returns an array of shape `(0, 4)`. - `D` is symmetric with a zero diagonal and finite non-negative entries. - When there are no distance ties, the output must match `scipy.cluster.hierarchy.linkage(squareform(D), method)`. ### What a Strong Answer Covers - The merge loop: find the closest pair of active clusters, record the row, and replace the pair with the new cluster - The distance update for single and complete linkage, and why it is exact - Cluster numbering, the `id_a < id_b` ordering and the merged size in each row - Deterministic tie-breaking, the `ValueError` cases, and the empty `(0, 4)` result for `N = 1` - Time and memory cost of the naive algorithm ### Follow-up Questions - Your naive approach is O(N³). How would you speed it up? - How would you add average (UPGMA) and Ward linkage? - How do you turn the linkage matrix into flat cluster labels?

Overview: Implement single and complete linkage agglomerative clustering from a square distance matrix and return a SciPy-format linkage matrix. It tests the merge loop, distance updates after each merge, cluster numbering and deterministic tie-breaking.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement agglomerative_linkage(D, method="single") that clusters N points from a square distance matrix D using "single" (min) or "complete" (max) linkage. Return an (N-1) x 4 float array where row k is [id_a, id_b, distance, size] for the k-th merge (SciPy format).

  • Original points are ids 0..N-1 . The cluster formed at step k gets id N + k .
  • Each row has id_a < id_b . size is the merged cluster's point count.
  • Distance ties go to the lexicographically smallest (id_a, id_b) pair.
  • Unknown method , a non-square D or an empty D raises ValueError .

Example:

x = np.array([0, 1, 3, 7])
D = np.abs(x[:, None] - x[None, :])   # D[i][j] = |x[i] - x[j]|
agglomerative_linkage(D, "single")
# [[0, 1, 1, 2],
#  [2, 4, 2, 3],
#  [3, 5, 4, 4]]
agglomerative_linkage(D, "complete")
# [[0, 1, 1, 2],
#  [2, 4, 3, 3],
#  [3, 5, 7, 4]]

Constraints & Assumptions

  • N is between 1 and 100. N = 1 returns an array of shape (0, 4) .
  • D is symmetric with a zero diagonal and finite non-negative entries.
  • When there are no distance ties, the output must match scipy.cluster.hierarchy.linkage(squareform(D), method) .

What a Strong Answer Covers Guidance

  • The merge loop: find the closest pair of active clusters, record the row, and replace the pair with the new cluster
  • The distance update for single and complete linkage, and why it is exact
  • Cluster numbering, the id_a < id_b ordering and the merged size in each row
  • Deterministic tie-breaking, the ValueError cases, and the empty (0, 4) result for N = 1
  • Time and memory cost of the naive algorithm

Follow-up Questions Guidance

  • Your naive approach is O(N³). How would you speed it up?
  • How would you add average (UPGMA) and Ward linkage?
  • How do you turn the linkage matrix into flat cluster labels?