Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Interactive k-means clustering tool
Groups cities into perfectly separated geographic territories.
Select an algorithm and generate steps to begin visualization
K-means clustering partitions points into k groups by assigning each point to its nearest cluster center and moving each center to the mean of its assigned points. Applied to logistics networks, it groups customers into service territories or candidate depot zones.
Lloyd's algorithm alternates two steps until stable: assign every point to the closest centroid, then recompute each centroid as the average of its points. Each iteration costs O(nk) distance computations and the objective, the sum of squared distances, never increases. Initialization matters: k-means++ spreads the starting centroids probabilistically and yields provably better expected results. The elbow method or silhouette scores guide the choice of k.
In supply chain design, k-means creates delivery zones and locates candidate warehouses at cluster centers before exact optimization. Beyond logistics it drives customer segmentation, image compression, anomaly detection baselines, and vector quantization in machine learning pipelines.
Two alternating steps, repeated until nothing moves. Assign every point to its nearest centroid, then move every centroid to the mean of its points.
KMeans(points, k):
initialise k centroids // see k-means++ below
repeat until assignments stop changing:
// Assignment step
for each point p:
cluster[p] = argmin over j of dist(p, centroid[j])
// Update step
for each cluster j:
centroid[j] = mean of all points assigned to j
// k-means++ initialisation:
// pick the first centroid uniformly at random, then pick
// each next one with probability proportional to the
// squared distance from the nearest chosen centroidEach step can only reduce the within-cluster sum of squares, and there are finitely many possible assignments, so the algorithm must terminate. What it terminates at is a local minimum, not necessarily the global one, which is why initialisation matters so much and why k-means++ is worth the extra pass.
Cluster four points into two groups, starting deliberately from a bad initialisation with both centroids inside the same true cluster.
Example graph: Points at (1,1), (2,1), (8,8) and (9,8). Two obvious clusters. Initial centroids placed at (1,1) and (2,1), both in the left-hand group.
The algorithm recovers the correct clustering in two iterations despite a deliberately poor start, with the objective falling from 61.33 to 1.00. It recovered here because the true clusters are far apart. With overlapping clusters the same bad initialisation can converge to a genuinely wrong local minimum and stay there, which is the entire motivation for k-means++ and for restarting from several seeds.
Time: O(n·k·d·i) · Space: O(n + k·d)
Each iteration computes the distance from every one of n points to each of k centroids in d dimensions, giving O(n·k·d) per iteration, and the update step is a single O(n·d) pass. With i iterations the total is O(n·k·d·i). In practice i is small, usually tens rather than hundreds. The worst-case number of iterations is superpolynomial, and instances exist requiring exponentially many, but they never arise in practice. Note that this is the cost of finding a local optimum: finding the globally optimal k-means clustering is NP-hard even for k equal to 2, and even in the plane. Space is the assignment array at O(n) plus the centroids at O(k·d).
k-means assumes spherical, similarly sized clusters and a known k. Break any of those assumptions and something else fits better.
| Alternative | Prefer it when | Cost |
|---|---|---|
| DBSCAN | Clusters are irregularly shaped, or you do not know k, or the data has noise and outliers to exclude. | O(n log n) |
| Hierarchical clustering | You want a dendrogram and the freedom to choose the number of clusters afterwards. | O(n^2 log n) |
| Gaussian mixture models | Clusters are elliptical or overlapping and you want soft, probabilistic assignments. | O(n·k·d^2) per iteration |
| k-medoids | Outliers are a problem, or the centre must be an actual data point, or your distance is not Euclidean. | O(k(n-k)^2) |
| k-means++ initialisation | Always. It is one extra pass and gives an O(log k) expected approximation guarantee. | O(n·k·d) |
Read the full article: Operations Research and Graph Theory
Related algorithms: Facility Location, Fleet Dispatching (mTSP)