Lloyd's algorithm repeats two simple moves. Could it go on forever, with points hopping between clusters in a cycle? And when it does stop, what do its clusters look like? Both questions have clean answers, and both rest on the same small fact.
One fact about means
Given any points , which single point minimises the total squared distance to all of them?
The gradient with respect to is . Setting it to zero gives , the mean. The function is a convex bowl in , so this is the global minimum: no point is closer, in total squared distance, to a set of points than their own mean.
The objective can only go down
Let the current partition be , with cluster means , and suppose the algorithm has not converged, so at least one point moves. Compare three quantities.
- Before: the objective of the current partition,
- Intermediate: each point measured against the old mean of the cluster it is moving to,
- After: the objective of the new partition, with the new means,
. A point moves only when some other old mean is strictly closer than its own; points that stay contribute the same amount to both sums. At least one point moves, so the inequality is strict.
. Group both sums by cluster in the new partition. In , the points now in cluster are measured against the old mean ; in , against their own new mean . By the fact above, a set of points is closest to its own mean, so each cluster's contribution can only fall.
Putting them together,
Every round that changes anything strictly lowers the objective. Therefore no partition can ever be visited twice (it would have to have a lower objective than itself). Since there are only finitely many partitions, the algorithm must stop.
Two caveats keep this honest.
- The argument bounds the number of rounds by the number of partitions, which is astronomically large. In practice Lloyd's algorithm usually converges in a handful of rounds; the proof only rules out cycling.
- Converging is not the same as finding the best partition. The algorithm stops at a partition where no single reassignment helps: a local optimum. A different start can stop somewhere better.
What converged clusters look like
At convergence, every point is at least as close to its own cluster's mean as to any other mean. Start with and means . A point in cluster 1 satisfies
Expand both sides; the terms cancel, leaving
This is a linear inequality in : the boundary is a straight line (a hyperplane in higher dimensions), perpendicular to . The midpoint satisfies it with equality, so the boundary is the perpendicular bisector of the segment joining the two means. Cluster 1 is everything on 's side of it.
With clusters, a point in cluster 1 must beat every other mean, so cluster 1 is the intersection of half-spaces, one for each bisector between and another mean. An intersection of half-spaces is a convex polygon (a convex polytope in higher dimensions). The space is carved into such cells, one per mean: a Voronoi diagram.
What k-means cannot find
Because every cluster is a convex cell with straight edges, k-means can only produce convex, roughly round clusters separated by flat boundaries. Two consequences follow.
- Rings. Take points on two concentric circles. The natural clusters are the inner ring and the outer ring, but no straight boundary separates a disc from the ring around it. With , k-means will draw a straight line through both rings and cut each in half.
- Size and spread. The bisector sits halfway between the means regardless of how spread out each cluster is, so a large diffuse cluster next to a small tight one gets its edge points stolen.
The first problem has the same cure as PCA's: map the data to a feature space where the clusters are separated by flat boundaries, and run k-means there using only dot products. That is kernel k-means, closely related to spectral clustering, which clusters points using the leading eigenvectors of a similarity matrix. The second problem is addressed by the probabilistic view of clustering in the next module, where each cluster gets its own shape and spread.