K-Means Clustering

Click to add points — watch centroids converge — Voronoi boundaries drawn in real time

K-means alternates between assignment (assign each point to nearest centroid) and update (move each centroid to mean of its cluster). It converges to a local minimum of the within-cluster sum of squares. K-means++ initialization reduces the chance of poor local minima.

K-means minimizes the within-cluster sum of squares (WCSS), an NP-hard problem in general, but the iterative algorithm always converges. The "elbow method" plots WCSS vs k — the optimal k is where adding more clusters yields diminishing returns. Voronoi tessellation shows the exact decision boundaries between centroids.