# Improving Performance of Cluster Heads Selection in DEC Protocol Using K-Means Algorithm for WSN
## 4 DEC-KM (Deterministic Energy-Efficient Clustering Protocol with K-Means)  
DEC relies on frequent communication among nodes to coordinate cluster head rotation and other activities. This communication overhead can lead to increased energy consumption, especially in scenarios with high data traffic. As nodes start to choose CHs randomly in most scenarios, the DEC may not achieve perfect load balancing among nodes, leading to some nodes still experiencing higher energy depletion rates than others. Regarding the network topology and deployment, the DEC may still face the risk of premature cluster head exhaustion, where some nodes deplete their energy faster than others. While the rotation mechanism helps, it may not eliminate this issue. To modify the selection approach of cluster head in the DEC protocol, we propose using the K-means algorithm, an essential flat clustering algorithm. K-means clustering is based on centroids and minimises the variance within each cluster, resulting in clearly defined and compact clusters. K-means is particularly effective for spherical clusters, as it can accurately group data that naturally forms into such clusters. With deterministic convergence, K-means will consistently yield the same results when given the same initial centroids, ensuring reliability and repeatability. Additionally, it is versatile, performs well with different data types, and can be adjusted with various distance metrics to enhance accuracy for specific datasets [15]. The k-means is an iterative algorithm. It chooses the centroids depending on the position of other nodes, which can be defined as a centre point between all nodes in the same cluster area. The algorithm result depends on the choice of the initial centre points. To obtain a low level of energy consumption and improve the lifetime and stability of the network, we propose to modify the DEC protocol through three main stages:  
* STAGE 1: Calculate the initial cluster areas and cluster heads using DEC protocol. * STAGE 2: Apply the K-means algorithm for the data calculated in STAGE 1 and make a new clustering. This stage can be divided into three sub-steps:
* Step 1 Centro: Calculate the new centre point of each cluster area in the network from Equation (5): \[\mu_{i}(\omega_{i})=\frac{1}{|\mathbf{\omega}_{i}|}{\sum_{x\in\omega_{i}}^{n}}\mathbf{x}\]  
The results will be stored as \(x\) and \(y\) coordinates. The centroids will be used as a reference to choose the new cluster head for the nodes. * Step 2 Node Distance: The distances to the centre points will be calculated for every node, and the node will be assigned to the closest centre point. The metrics used to calculate the distances of the node to the centroids are given by Equation (6):  
\[D=\sum_{\alpha\in\omega_{n}}^{n}|x-\mu(\omega_{n})|^{2}\]  
Step 3 Stop condition: If there are no changes in clusters or a maximum number of iterations is reached, the clustering is finished; if not, return to _Cento_ and repeat until the stop condition is not fulfilled. STAGE 3: CH Selection:  
Step 1: For each CH calculated in Stage 1 using DEC protocol, check if it still belongs to the same cluster area and if its distance to the Base Station (BS) is shorter than the distance of cluster centroid to the BS:  
If Yes: return the CH as a confirmed CH. If No: Using the coordinates of centre points and distances, which were calculated in Stage 2, compare node distances to the centroid in the same cluster area and find the node nearest to the centroid. The node with the shortest length to the centroid point will be chosen as a tentative CH of this area. Step 2: For each tentative CH, calculate the energy threshold needed to transmit and receive data using the DEC energy model. Check if the CH energy level is over this threshold:  
If Yes: return the new CH as a confirmed CH. If No: choose the next tentative CH nearest to the centroid point and return to Step 2.