What is the time complexity of k-means?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The K-means algorithm is one of the most widely used clustering techniques in unsupervised learning. It works by partitioning a dataset into a set number of clusters (denoted as ) by minimizing the variance within each cluster. A crucial aspect to understand when dealing with algorithms like K-means is its time complexity, which provides insights into its efficiency and scalability.
Understanding Time Complexity in K-means
The time complexity of an algorithm is an expression that quantifies the amount of time taken by an algorithm to run as a function of the length of the input. For K-means, the primary operations are assigning data points to the nearest cluster and updating the cluster centroids.
Steps Involved in K-means
- Initialization: Select initial centroids randomly.
- Assignment Step:
- For each data point, compute its distance to each centroid.
- Assign the data point to the nearest centroid.
- Update Step:
- Recalculate the centroids as the mean of all data points assigned to each cluster.
- Convergence Check:
- Check if centroids have changed. If not, the algorithm has converged.
- Otherwise, repeat steps 2 and 3.
Time Complexity Breakdown
1. Initialization Step
- Complexity: for selecting initial centroids randomly and initializing the centroids.
- Explanation: While selecting the initial centroids, randomness does not affect time complexity significantly. The initial selection is typically .
2. Assignment Step
- Complexity:
- Explanation: For each of the data points, we compute the distance to the centroids. Calculating the distance in -dimensional space takes time.
3. Update Step
- Complexity:
- Explanation: Each of the centroids is updated by averaging over the data points with the dimension factor coming into play while recalculating means.
4. Convergence Step
- Complexity: This step generally piggybacks onto the assignment step since the check is typically trivial if centroids remain unchanged.
Overall Time Complexity
The K-means algorithm typically iterates over the assignment and update steps until convergence. If we denote the number of iterations until convergence by , the overall time complexity can be expressed as:
- Overall Complexity:
Factors Affecting Time Complexity
- Number of Iterations (): In practice, significantly affects the runtime. It's algorithmically determined, striving for satisfactory convergence.
- Dimensionality (): High dimensional datasets can lead to increased computation time for both assignment and update steps due to more complex distance calculations.
- Number of Clusters (): A higher increases the computational load due to more distance calculations during the assignment phase.
- Data Size (): A larger dataset naturally increases the number of point-to-centroid calculations, scaling linearly with .
Optimizations for K-means
Certain optimizations and variations reduce the effective runtime of the algorithm:
- K-means++ Initialization: Improves initial centroid selection, potentially reducing by yielding faster convergence.
- Elkan's Algorithm: Utilizes the triangle inequality to avoid unnecessary distance calculations, leading to significant performance improvements.
- Mini-batch K-means: Uses a random subset of data to compute updates, significantly reducing computation for large datasets.
Time Complexity Summary
| Factor | Time Complexity | Details |
| Initialization | Random centroid selection along with minor preprocessing | |
| Assignment Step | Distance calculations for assigning points to clusters | |
| Update Step | Recalculate centroids as the mean of assigned points | |
| Overall | Iterative algorithm dependent on convergence |
Conclusion
The K-means algorithm is highly valuable for clustering tasks despite its relatively high time complexity due to repeated calculations. The workings and complexity can be optimized using various techniques and considerations, facilitating its application across numerous data-intensive fields. Understanding time complexity plays an essential role in effectively harnessing K-means' potential in practical scenarios.
Related reading
- What is the use of a .pb file in TensorFlow and how does it work?
- What is the use of DMatrix?
- What is the use of train_on_batch in keras?
- What is the utility of Tensor as opposed to EagerTensor in Tensorflow 2.0?
- What is the time complexity of my function?
- What is the time complexity of Ruby's built in permutation and repeated_permutation methods?
- What is the time complexity of popping an element from a dict in Python?
- What is the time complexity of the algorithm below?

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.