IAE Academy

GATE DA

Clustering

K-means, k-medoids and hierarchical clustering

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 . 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 (); samples are the rows of the data matrix; the distance is Euclidean unless stated; no logarithms appear in this topic. denotes cluster , its number of points, its centroid, and the number of clusters. is a general dissimilarity when Euclidean distance is not required.

EUCLIDEAN DISTANCE

Default distance. k-means is built around its square.

MANHATTAN DISTANCE

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 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)

Squared Euclidean distance of each point to its own cluster's centroid, summed over all points.

Lloyd's algorithm (the standard k-means procedure)

  1. Choose and initial centroids (for example chosen data points).

  2. Assignment step: put each point in the cluster of its nearest centroid.

  3. Update step: recompute each centroid as the mean of the points assigned to it.

  4. Repeat steps 2 and 3 until the assignments no longer change (equivalently, the centroids stop moving).

ASSIGNMENT STEP

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)

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 .

WCSS FROM PAIRWISE DISTANCES

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 (every point moves to a centroid at least as close), and each update step cannot increase (the mean is the minimiser). is therefore non-increasing. With the tie rule above, 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 : neither step can improve it, but it need not be the global minimum.

Sensitivity to initialisation. The final partition and its 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 . In practice it is run from several starts and the lowest 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 , the squared distance from to the nearest centroid already chosen, which spreads the starting centroids out.

Each iteration costs . 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 . The minimum achievable WCSS never increases as grows, and it is at when the points are distinct (each point its own cluster). So you cannot choose by simply minimising . (The that Lloyd's algorithm actually returns from a particular start can even rise with , because it may stop at a poor local minimum.) The elbow method plots the best found against 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, is chosen by the user, not learned by the basic algorithm.

SILHOUETTE COEFFICIENT OF POINT I

= mean distance from point to the other points of its own cluster; = the smallest, over the other clusters, of the mean distance from to that cluster's points. : near 1 means well placed, negative means is on average closer to another cluster. By convention if is alone in its cluster. A higher average over all points indicates a better .

k-medoids keeps the same idea (a partition into clusters, each with a representative) but the representative, called the medoid, must be one of the data points. It works from a dissimilarity that need not be Euclidean, so it can be used when only a pairwise distance matrix is available.

MEDOID OF A CLUSTER

The member of the cluster with the smallest total dissimilarity to the other members. k-medoids minimises .

Two ways to run k-medoids

  1. 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.

  2. Swap (PAM): assign each point to its nearest medoid; for every medoid and every non-medoid , compute the total cost if replaced ; make the swap that lowers the cost most; stop when no swap lowers it.

  3. 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 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 singleton clusters and repeatedly merge the two closest clusters until one cluster remains. This takes exactly merges. Divisive (top-down). Start with all points in one cluster and repeatedly split a cluster. The number of ways to split points into two non-empty groups is , 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 clusters and 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 .

SINGLE LINKAGE (NEAREST NEIGHBOUR)

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)

Farthest pair across the two clusters. Favours compact clusters of similar diameter.

AVERAGE LINKAGE

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

The increase in total WCSS caused by merging and ; 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

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

  1. Start with each point as its own cluster and write the pairwise distance matrix.

  2. Find the smallest entry between two different clusters; merge them and record the distance as the height of this merge in the dendrogram.

  3. 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).

  4. Repeat until one cluster remains.

  5. Cut the dendrogram: a horizontal cut at height gives the clusters formed by merges at height ; to get clusters, undo the last merges.

AVERAGE-LINKAGE UPDATE AFTER MERGING A AND B

Weight the two old distances by cluster size. The plain average of and is wrong unless .

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 largest MST edges leaves the 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 from the pair's midpoint. The naive algorithm stores the distance matrix ( memory) and takes 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 and initial centroids 2 and 8 until convergence. Find the final centroids and the WCSS.

Solution
  1. 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.

  2. Update 1: cluster 1 = {1, 2, 4} has mean 7/3 (about 2.333); cluster 2 = {8, 10} has mean 9.

  3. 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.

  4. WCSS: cluster 1 gives ; cluster 2 gives . Total .

Answer: Final centroids 7/3 and 9; WCSS = 20/3, about 6.67.

WORKED EXAMPLE · MEDOID VERSUS MEAN

A cluster contains the points 1, 2, 4, 5, 20 on a line, with dissimilarity . Find its medoid and compare it with the mean.

Solution
  1. Total dissimilarity of each point to the others: 1 gives 1 + 3 + 4 + 19 = 27; 2 gives 1 + 2 + 3 + 18 = 24; 4 gives 3 + 2 + 1 + 16 = 22; 5 gives 4 + 3 + 1 + 15 = 23; 20 gives 19 + 18 + 16 + 15 = 68.

  2. The smallest total is 22, so the medoid is 4.

  3. 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 . Build the agglomerative dendrogram with single linkage and with complete linkage. Give the merge heights in order.

Solution
  1. Pairwise distances: AB = 1, AC = 5, AD = 8, AE = 14, BC = 4, BD = 7, BE = 13, CD = 3, CE = 9, DE = 6.

  2. 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.

  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.

  4. 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.

  5. 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 complete-linkage merges every cluster has diameter at most the -th height, so at that threshold the points already form at most connected groups, which is what the -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.

See these papers in full: GATE DA 2026 · GATE DA 2025 · GATE DA 2024

GATE 2026 DA Q.36

GATE 2026 · DAQ.36 · 2 marks · MCQ

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?

  1. (A)

    P1, P2

  2. (B)

    P2, P3

  3. (C)

    P3, P4

  4. (D)

    P2, P4

Answer and solution
See the question in the paper
GATE 2026 DA Q.36: answer and solution

GATE 2025 DA Q.30

GATE 2025 · DAQ.30 · 1 mark · MSQ

Let and be two sets of objects. Let be a measure of dissimilarity between two objects and . Consider the following definitions of dissimilarity between and .

Which of the following statements is/are correct?

  1. (A)

    Single Linkage Clustering uses DIS-1

  2. (B)

    Single Linkage Clustering uses DIS-2

  3. (C)

    Complete Linkage Clustering uses DIS-2

  4. (D)

    Complete Linkage Clustering uses DIS-1

Answer and solution
See the question in the paper
GATE 2025 DA Q.30: answer and solution

GATE 2024 DA Q.42

GATE 2024 · DAQ.42 · 2 marks · MCQ

Consider the table below, where the element of the table is the distance between points and . Single linkage clustering is performed on data points, .

| | | | | | |
|---|---|---|---|---|---|
| | 0 | 1 | 4 | 3 | 6 |
| | 1 | 0 | 3 | 5 | 3 |
| | 4 | 3 | 0 | 2 | 5 |
| | 3 | 5 | 2 | 0 | 1 |
| | 6 | 3 | 5 | 1 | 0 |

Which ONE of the following is the correct representation of the clusters produced?

GATE 2024 DA question 42: A 5x5 symmetric distance table (given in the stem) and four dendrograms (A to D), each with leaves in the order x1, x2…Tap to open full size
Figure from the GATE 2024 DA paper. It is part of the question.
Figure description

A 5x5 symmetric distance table (given in the stem) and four dendrograms (A to D), each with leaves in the order x1, x2, x3, x4, x5.

  1. (A)

    Figure: dendrogram in which x1 and x2 join as a pair; x4 and x5 join as a pair, and x3 then joins {x4,x5}; the two groups join at the top.

  2. (B)

    Figure: dendrogram in which x1 and x2 join, then x3 joins {x1,x2}; x4 and x5 join as a separate pair; the two groups join at the top.

  3. (C)

    Figure: dendrogram in which x1 and x2 join; x3 and x4 join, then x5 joins {x3,x4}; the two groups join at the top.

  4. (D)

    Figure: dendrogram in which x1 and x2 join; x3 and x4 join; these two pairs merge; x5 joins last at the top.

Answer and solution
GATE 2024 DA Q.42: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · MCQ

Which of the following statements about k-means (Lloyd's algorithm with squared Euclidean distance) is correct?

  1. (A)

    It always reaches the global minimum of the WCSS, whatever the initial centroids.

  2. (B)

    The WCSS never increases from one iteration to the next.

  3. (C)

    The final clusters do not depend on the initial centroids.

  4. (D)

    For the minimum WCSS is zero for any data set.

Answer and solution

Answer: (B)

  1. 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.

  2. Different starts can end at different local minima, so A and C are false.

  3. For 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 be the height of the final merge (the one that produces a single cluster) under single linkage and the same under complete linkage. Find .

Answer and solution

Answer: (accepted range )

  1. Pairwise distances: 2–3 = 1, 2–7 = 5, 2–12 = 10, 2–14 = 12, 3–7 = 4, 3–12 = 9, 3–14 = 11, 7–12 = 5, 7–14 = 7, 12–14 = 2.

  2. Both linkages merge {2,3} at 1, then {12,14} at 2 (the two smallest entries).

  3. 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 .

  4. 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 .

  5. .

PRACTICE 31 mark · MCQ

Cluster and cluster lie on a line, with distance equal to the absolute difference. What is the average-linkage distance between and ?

  1. (A)

    3

  2. (B)

    6

  3. (C)

    7

  4. (D)

    9

Answer and solution

Answer: (B)

  1. The four cross-cluster distances are |0−5| = 5, |0−9| = 9, |2−5| = 3 and |2−9| = 7.

  2. Average = (5 + 9 + 3 + 7)/4 = 24/4 = 6.

  3. 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 (squared Euclidean distance), starting from centroids 1 and 13. After the algorithm converges, what is the WCSS ?

Answer and solution

Answer: (accepted range )

  1. 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).

  2. Update 1: centroids become (1 + 3 + 5)/3 = 3 and (11 + 13)/2 = 12.

  3. 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.

  4. WCSS = (1−3)² + (3−3)² + (5−3)² + (11−12)² + (13−12)² = 4 + 0 + 4 + 1 + 1 = 10.

PRACTICE 52 marks · MSQ

Select ALL correct statements comparing k-means and k-medoids.

  1. (A)

    A medoid is always one of the data points of its cluster.

  2. (B)

    k-medoids can be run when only a matrix of pairwise dissimilarities is available.

  3. (C)

    In k-means the cluster mean minimises the sum of squared Euclidean distances from the cluster's points to it.

  4. (D)

    A k-means centroid is always one of the data points.

Answer and solution

Answer: (A), (B), (C)

  1. A is the definition of a medoid.

  2. B holds because both the assignment to the nearest medoid and the medoid update use only dissimilarities between data points.

  3. C is the property that makes the mean the correct update step for the squared-error objective.

  4. D is false: the mean of 1, 2, 4 is 7/3, which is not a data point.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE DA

Unsupervised Learning: clustering algorithms, k-means/k-medoid, hierarchical clustering, top-down, bottom-up: single-linkage, multiple-linkage

Preparing in Kolkata? IAE Academy's course for GATE DA.