Clustering groups unlabelled points so that points in a group are close and points in different groups are far apart. For GATE DA you need to run k-means by hand (assignment and update steps, the within-cluster sum of squares), know how k-medoids differs, and build and cut a dendrogram with single, complete and average linkage from a distance matrix.
4 previous-year questions · 4 solved · 5 practice questions
Clustering and distance measures
Clustering is unsupervised learning: there are no labels, only data points x1,…,xn∈Rd. The goal is to partition them into groups (clusters) so that points in one group are similar and points in different groups are dissimilar. What "similar" means is decided entirely by the distance you choose.
Convention used in these notes. Vectors are bold (x); samples are the rows of the data matrix; the distance is Euclidean unless stated; no logarithms appear in this topic. Ck denotes cluster k,∣Ck∣ its number of points, μk its centroid, and K the number of clusters. d(⋅,⋅) is a general dissimilarity when Euclidean distance is not required.
EUCLIDEAN DISTANCE
d2(x,y)=j=1∑d(xj−yj)2
Default distance. k-means is built around its square.
MANHATTAN DISTANCE
d1(x,y)=j=1∑d∣xj−yj∣
Sum of absolute coordinate differences. For (1,1) and (4,5): Manhattan = 3 + 4 = 7, Euclidean = 5.
EXAM TIP
Features on very different scales let one feature dominate the distance. Distance-based clustering is usually run after standardising the features, unless the question says otherwise.
k-means
k-means partitions the points into exactly K clusters, each represented by its centroid (the mean of its points). It searches for the partition that minimises the within-cluster sum of squares (WCSS), also called the inertia or distortion.
K-MEANS OBJECTIVE (WCSS)
J=k=1∑Kx∈Ck∑∥x−μk∥2
Squared Euclidean distance of each point to its own cluster's centroid, summed over all points.
Lloyd's algorithm (the standard k-means procedure)
Choose K and initial centroids μ1,…,μK (for example K chosen data points).
Assignment step: put each point in the cluster of its nearest centroid.
Update step: recompute each centroid as the mean of the points assigned to it.
Repeat steps 2 and 3 until the assignments no longer change (equivalently, the centroids stop moving).
ASSIGNMENT STEP
c(i)=argkmin∥xi−μk∥2
Nearest centroid in Euclidean distance (squaring does not change which centroid is nearest). Ties need a fixed rule: keep the point in its current cluster if that cluster is among the nearest, otherwise take the lowest index.
UPDATE STEP (CENTROID)
μk=∣Ck∣1x∈Ck∑x
Defined for a non-empty cluster. The mean is the point that minimises the sum of squared Euclidean distances to the cluster's points, so this step cannot increase J.
WCSS FROM PAIRWISE DISTANCES
x∈C∑∥x−μ∥2=2∣C∣1x∈C∑y∈C∑∥x−y∥2
The double sum runs over all ordered pairs (each unordered pair counted twice). Useful when only distances are given. Check: for {1, 2, 4} the right side is 28/6 = 4.667, the same as the left.
Convergence. Each assignment step cannot increase J (every point moves to a centroid at least as close), and each update step cannot increase J (the mean is the minimiser). J is therefore non-increasing. With the tie rule above, J strictly decreases whenever the partition changes, so no partition repeats; there are only finitely many partitions, so the algorithm stops after finitely many iterations. It stops at a fixed point of the two steps, usually called a local minimum of J: neither step can improve it, but it need not be the global minimum.
Sensitivity to initialisation. The final partition and its J depend on the starting centroids; different starts can end at different fixed points, and k-means is not guaranteed to find the global minimum of J. In practice it is run from several starts and the lowest J is kept. k-means++ seeding picks the first centroid uniformly at random from the data and each further centroid from the data with probability proportional to D(x)2, the squared distance from x to the nearest centroid already chosen, which spreads the starting centroids out.
Each iteration costs O(nKd). A cluster can end up empty; its mean is then undefined, and implementations either keep its old centroid or re-seed it (for example at a point far from its current centroid). The clusters produced are separated by straight boundaries (hyperplanes), because each is the set of points nearest one centroid, so k-means suits compact, roughly round clusters of similar size.
Choosing k, and k-medoids
Choosing K. The minimum achievable WCSS never increases as K grows, and it is 0 at K=n when the points are distinct (each point its own cluster). So you cannot choose K by simply minimising J. (The J that Lloyd's algorithm actually returns from a particular start can even rise with K, because it may stop at a poor local minimum.) The elbow method plots the best J found against K and looks for the point after which the drop becomes small. The silhouette score below, or validation on a downstream task, is also used. Whatever the method, K is chosen by the user, not learned by the basic algorithm.
SILHOUETTE COEFFICIENT OF POINT I
s(i)=max{a(i),b(i)}b(i)−a(i)
a(i) = mean distance from point i to the other points of its own cluster; b(i) = the smallest, over the other clusters, of the mean distance from i to that cluster's points. −1≤s(i)≤1: near 1 means well placed, negative means i is on average closer to another cluster. By convention s(i)=0 if i is alone in its cluster. A higher average s(i) over all points indicates a better K.
k-medoids keeps the same idea (a partition into K clusters, each with a representative) but the representative, called the medoid, must be one of the data points. It works from a dissimilarity d(x,y) that need not be Euclidean, so it can be used when only a pairwise distance matrix is available.
MEDOID OF A CLUSTER
mk=argx∈Ckminy∈Ck∑d(x,y)
The member of the cluster with the smallest total dissimilarity to the other members. k-medoids minimises ∑k∑x∈Ckd(x,mk).
Two ways to run k-medoids
Alternating (like Lloyd's): assign each point to its nearest medoid, then replace each medoid by the medoid of its cluster; repeat until nothing changes.
Swap (PAM): assign each point to its nearest medoid; for every medoid m and every non-medoid o, compute the total cost if o replaced m; make the swap that lowers the cost most; stop when no swap lowers it.
Both stop at a local optimum that depends on the initial medoids, as k-means does.
k-means versus k-medoids
Aspect
k-means
k-medoids
Representative
Centroid (mean); usually not a data point
Medoid; always an actual data point
Distance used
Squared Euclidean (the mean is optimal for it)
Any dissimilarity, including a given distance matrix
Update step
Average the members
Pick the member with least total dissimilarity
Effect of an extreme point
The mean is pulled towards it
The medoid stays on a real point and is pulled less
Cost of an update
Linear in the cluster size
Needs pairwise distances inside the cluster (quadratic in its size)
EXAM TIP
A k-means centroid is generally NOT a data point (the mean of 1, 2, 4 is 2.33). If an option says the centroid is always one of the points, it describes a medoid.
Hierarchical clustering and linkage
Hierarchical clustering does not need K in advance. It produces a tree of nested clusters, a dendrogram, and you choose the number of clusters afterwards by cutting the tree.
Agglomerative (bottom-up). Start with n singleton clusters and repeatedly merge the two closest clusters until one cluster remains. This takes exactly n−1 merges. Divisive (top-down). Start with all points in one cluster and repeatedly split a cluster. The number of ways to split n points into two non-empty groups is 2n−1−1, so exact divisive search is impractical and a heuristic split rule (for example running 2-means on the cluster) is used.
Agglomerative methods differ only in how the distance between two clustersA and B is defined. The syllabus names single linkage and "multiple-linkage"; the standard linkage criteria are below. Single, complete and average linkage need only the point distances d(a,b).
SINGLE LINKAGE (NEAREST NEIGHBOUR)
D(A,B)=a∈A,b∈Bmind(a,b)
Closest pair across the two clusters. Prone to chaining: a line of nearby points joins distant groups into one long cluster.
COMPLETE LINKAGE (FARTHEST NEIGHBOUR)
D(A,B)=a∈A,b∈Bmaxd(a,b)
Farthest pair across the two clusters. Favours compact clusters of similar diameter.
AVERAGE LINKAGE
D(A,B)=∣A∣∣B∣1a∈A∑b∈B∑d(a,b)
Mean of all cross-cluster pairwise distances. For the same pair of clusters it lies between the single-linkage and complete-linkage values.
WARD'S LINKAGE
DWard(A,B)=∣A∣+∣B∣∣A∣∣B∣∥μA−μB∥2
The increase in total WCSS caused by merging A and B; it merges the pair that increases the WCSS least. Needs Euclidean coordinates (centroids). Software scales the reported height differently, but the order of merges is the same.
CENTROID LINKAGE
D(A,B)=∥μA−μB∥
Distance between the cluster means. Needs Euclidean coordinates, and it can give inversions (a later merge lower than an earlier one).
Agglomerative procedure from a distance matrix
Start with each point as its own cluster and write the pairwise distance matrix.
Find the smallest entry between two different clusters; merge them and record the distance as the height of this merge in the dendrogram.
Recompute the distance from the new cluster to every other cluster with the chosen linkage (for single linkage take the smaller of the two old distances, for complete the larger, for average the size-weighted mean below).
Repeat until one cluster remains.
Cut the dendrogram: a horizontal cut at height h gives the clusters formed by merges at height ≤h; to get K clusters, undo the last K−1 merges.
AVERAGE-LINKAGE UPDATE AFTER MERGING A AND B
D(A∪B,C)=∣A∣+∣B∣∣A∣D(A,C)+∣B∣D(B,C)
Weight the two old distances by cluster size. The plain average of D(A,C) and D(B,C) is wrong unless ∣A∣=∣B∣.
EXAM TIP
With single linkage you update by taking the minimum of the two old distances, and with complete linkage the maximum, without going back to the raw points. Average linkage needs the size weights shown above.
Single linkage and the minimum spanning tree. Treat the points as a complete graph whose edge weights are the distances. The single-linkage merge heights are exactly the edge weights of a minimum spanning tree (MST), taken in increasing order (this is Kruskal's algorithm). Removing the K−1 largest MST edges leaves the K single-linkage clusters.
For single, complete, average and Ward's linkage the merge heights never decrease as you go up the tree, so the dendrogram has no inversions. Centroid linkage can invert: for three points at mutual distance 1, the first merge is at height 1, and the third point is then 3/2≈0.866 from the pair's midpoint. The naive algorithm stores the n×n distance matrix (O(n2) memory) and takes O(n3) time. Given the data and the linkage, the result has no random element, unlike k-means, which depends on its start. Ties between equal distances still have to be broken by a rule, and the choice can change the tree.
Worked examples
WORKED EXAMPLE · K-MEANS BY HAND (1-D)
Points: 1, 2, 4, 8, 10. Run k-means with K=2 and initial centroids 2 and 8 until convergence. Find the final centroids and the WCSS.
▸ Solution
Assignment 1: distances to (2, 8): point 1 gives (1, 7) so cluster 1; point 2 gives (0, 6) so cluster 1; point 4 gives (2, 4) so cluster 1; point 8 gives (6, 0) so cluster 2; point 10 gives (8, 2) so cluster 2.
Update 1: cluster 1 = {1, 2, 4} has mean 7/3 (about 2.333); cluster 2 = {8, 10} has mean 9.
Assignment 2: point 4 is 5/3 (1.667) from 7/3 and 5 from 9, so it stays in cluster 1; point 8 is 17/3 (5.667) from 7/3 and 1 from 9, so it stays in cluster 2. Points 1 and 2 are even nearer 7/3 and point 10 even nearer 9. No point moves, so the algorithm has converged.
The mean is (1 + 2 + 4 + 5 + 20)/5 = 32/5 = 6.4, which is not a data point and has been pulled towards the extreme point 20.
Answer: Medoid 4 (total dissimilarity 22); mean 6.4.
WORKED EXAMPLE · SINGLE VERSUS COMPLETE LINKAGE
Five points on a line: A = 1, B = 2, C = 6, D = 9, E = 15, with distance ∣x−y∣. Build the agglomerative dendrogram with single linkage and with complete linkage. Give the merge heights in order.
▸ Solution
Pairwise distances: AB = 1, AC = 5, AD = 8, AE = 14, BC = 4, BD = 7, BE = 13, CD = 3, CE = 9, DE = 6.
Both linkages first merge AB at height 1 (the smallest entry); in single, complete and average linkage a merge of two singletons happens at their point distance. Next, the smallest entries are CD = 3 against {A,B}–C = 4 (single) or 5 (complete), so both merge CD at height 3.
Single linkage: {A,B} to {C,D} is min(5, 8, 4, 7) = 4; {C,D} to E is min(9, 6) = 6; {A,B} to E is min(14, 13) = 13. Merge {A,B} and {C,D} at 4. Then E joins {A,B,C,D} at min(13, 6) = 6. Heights: 1, 3, 4, 6.
Complete linkage: {A,B} to {C,D} is max(5, 8, 4, 7) = 8; {C,D} to E is max(9, 6) = 9; {A,B} to E is max(14, 13) = 14. Merge {A,B} and {C,D} at 8. Then E joins at max(14, 13, 9, 6) = 14. Heights: 1, 3, 8, 14.
Check with the MST: in 1-D the MST joins neighbours, with gaps 1, 4, 3, 6. Sorted, these are 1, 3, 4, 6, the single-linkage heights.
Answer: Single linkage: 1, 3, 4, 6. Complete linkage: 1, 3, 8, 14. Here each complete-linkage height is at least the single-linkage height at the same step. This holds in general: after j complete-linkage merges every cluster has diameter at most the j-th height, so at that threshold the points already form at most n−j connected groups, which is what the j-th single-linkage height measures.
PREVIOUS-YEAR QUESTIONS
4 questions from the DA papers
Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.
Let four points in three-dimensional space be: P1: [2, 3, −1], P2: [3, 1, 1], P3: [5, −2, 3] and P4: [3, 3, 3].
Hierarchical Agglomerative Clustering is used to cluster the above points. If Manhattan Distance is used as the distance metric during clustering, which of the following options indicates the two points that will be merged first?
Let C1 and C2 be two sets of objects. Let D(x,y) be a measure of dissimilarity between two objects x and y. Consider the following definitions of dissimilarity between C1 and C2.
Euclidean distance based k-means clustering algorithm was run on a dataset of 100 points with k=3. If the points [11] and [−11] are both part of cluster 3, then which ONE of the following points is necessarily also part of cluster 3?
Consider the table below, where the (i,j)th element of the table is the distance between points xi and xj. Single linkage clustering is performed on data points, x1,x2,x3,x4,x5.
Which of the following statements about k-means (Lloyd's algorithm with squared Euclidean distance) is correct?
(A)
It always reaches the global minimum of the WCSS, whatever the initial centroids.
(B)
The WCSS never increases from one iteration to the next.
(C)
The final clusters do not depend on the initial centroids.
(D)
For K=1 the minimum WCSS is zero for any data set.
▸ Answer and solution
Answer: (B)
Neither the assignment step (each point moves to a centroid at least as close) nor the update step (the mean minimises the squared distances) can increase the WCSS, so B is true. Together with the finite number of partitions and a consistent tie rule, this is why the algorithm terminates.
Different starts can end at different local minima, so A and C are false.
For K=1 the WCSS is the total squared deviation from the overall mean, which is zero only if all points coincide, so D is false.
PRACTICE 22 marks · NAT
Agglomerative clustering is applied to the points 2, 3, 7, 12, 14 on a line, with distance equal to the absolute difference. Let hs be the height of the final merge (the one that produces a single cluster) under single linkage and hc the same under complete linkage. Find hc−hs.
Both linkages merge {2,3} at 1, then {12,14} at 2 (the two smallest entries).
Single linkage: {2,3} to 7 is min(5, 4) = 4; 7 to {12,14} is min(5, 7) = 5; {2,3} to {12,14} is min(10, 12, 9, 11) = 9. Merge {2,3,7} at 4. The final merge is at min(10, 12, 9, 11, 5, 7) = 5, so hs=5.
Complete linkage: {2,3} to 7 is max(5, 4) = 5; 7 to {12,14} is max(5, 7) = 7; {2,3} to {12,14} is max(10, 12, 9, 11) = 12. Merge {2,3,7} at 5. The final merge is at max(10, 12, 9, 11, 5, 7) = 12, so hc=12.
hc−hs=12−5=7.
PRACTICE 31 mark · MCQ
Cluster A={0,2} and cluster B={5,9} lie on a line, with distance equal to the absolute difference. What is the average-linkage distance between A and B?
(A)
3
(B)
6
(C)
7
(D)
9
▸ Answer and solution
Answer: (B)
The four cross-cluster distances are |0−5| = 5, |0−9| = 9, |2−5| = 3 and |2−9| = 7.
Average = (5 + 9 + 3 + 7)/4 = 24/4 = 6.
For comparison, single linkage would give 3 (option A) and complete linkage 9 (option D).
PRACTICE 42 marks · NAT
One-dimensional points 1, 3, 5, 11, 13 are clustered by k-means with K=2 (squared Euclidean distance), starting from centroids 1 and 13. After the algorithm converges, what is the WCSS J?
▸ Answer and solution
Answer:10 (accepted range ±0.01)
Assignment 1: 1, 3 and 5 are nearer to 1 (distances 0, 2, 4 versus 12, 10, 8); 11 and 13 are nearer to 13 (distances 2, 0 versus 10, 12).
Update 1: centroids become (1 + 3 + 5)/3 = 3 and (11 + 13)/2 = 12.
Assignment 2: 5 is 2 from 3 and 7 from 12, so it stays; 11 is 8 from 3 and 1 from 12, so it stays; the other points are nearer still. Converged.