K-means algorithm variation with equal cluster size
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
The K-means algorithm is a popular clustering method primarily used in the field of data analysis and machine learning. It partitions a dataset into K distinct, non-overlapping subsets (clusters). However, in traditional K-means clustering, the sizes of clusters are not controlled, meaning they can be highly imbalanced. This presents a challenge in scenarios where balanced cluster sizes are necessary, such as load balancing or ensuring fair representation in sample selection. Variations of the K-means algorithm that aim to produce clusters of equal size, or approximately equal size, have been developed to address this limitation.
Technical Explanation
The standard K-means algorithm aims to minimize within-cluster variance, defined mathematically as:
where represents the cluster assigned to the cluster center .
For equal-sized clusters, an additional constraint is introduced such that the number of data points in each cluster equals , where is the total number of data points. This constrained optimization problem is stricter and often solved using alternative approaches:
1. Balanced K-Means Clustering
Balanced K-means aims to ensure that the clusters are of approximately equal size by modifying the assignment step. The strategy can involve a controlled process of assigning points to clusters to maintain balance. Permutations and optimized selections may be employed to manage the distribution efficiently.
Example Approach:
- Initialization: Randomly assign a point from each cluster until uniform size is achieved.
- Iterative Refinement: For each data point, find the nearest cluster center. Reassign point only if clusters maintain balance.
- Rebalancing: If the cluster becomes unbalanced, employ methods such as the Hungarian algorithm to optimize assignments with respect to distance metrics.
2. Constraint-driven Formulation
Another strategy involves embedding constraints directly into the optimization framework. This can be formulated via mixed-integer programming or by employing relaxation techniques that allow for easier computation while approximating the size constraints.
3. Max-Sum Clustering
Max-sum clustering is an alternative that balances variance minimization and equal-size constraints. It replaces the mean-based variance measures with sum-based measures over binary assignments that inherently strive for balanced partitions.
Applications
Industry Use-Cases
• Fair Sampling: When datasets need to be sampled in equal proportions from distinct categories. • Load Balancing: In network distribution, where tasks are distributed evenly across servers or nodes.
Challenges
- Computational Complexity: Adding constraints increases computation time, particularly with large-scale datasets.
- Convergence Issues: Ensuring that the solution converges while maintaining balance can be difficult to achieve.
- Scalability: Large datasets may require alternative infrastructures, such as distributed computing, to handle the increased load.
Example: Equal-size K-means in Practice
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.