# Unsupervised Learning-Based WSN Clustering for Efficient Environmental Pollution Monitoring
## 3 Modified K-Means LEACH Algorithm  
As discussed previously, there exists a range of different implementations for K-LEACH discussed in recent research; however, the implementations mainly differ in the enduring and dynamic behavior of the most recent CHs. Our implementation relies on two important pillars, which are taking maximum advantage of the K-means algorithm on the proposed network by forming a separate set of nodes for the resulting CHs and conserving the energy of the most recent CHs to endure throughout the experiment. It can be noticed from Algorithm 1 that it incorporates two sets of nodes, namely, n_s and n_c, where n_s is the set of normal nodes that the simulation starts with. Our implementation of the K-LEACH algorithm utilized the learning capability of the K-means unsupervised classification algorithm to identify the most optimal CH positions throughout the simulation. Hence, a new set of nodes was mounted to the network, which was denoted by n_c and represents the CHs' positions in each round. Since the movement of CHs provided by the K-LEACH algorithm is limited, CHs do not change at some point in the simulation until the end of the simulation (which is the vth round in Algorithm 1). An energy conservation approach was taken into consideration to counter this issue, which entails calculating the necessary excess energy needed for the most recent CHs to endure until all n_s nodes die out (which is the wth round in Algorithm 1). n_c nodes are expected to die out first because every node in n_c is expected to be a CH at least once, and the energy dissipation for CHs' is higher than the energy dissipation for the normal nodes. Accordingly, the LEACH protocol part of the implementation was used to evaluate the residual energy and the alive/dead state of the nodes in n_s and n_c networks separately. Figure 2 represents the flowchart of our modified K-means LEACH algorithm, which is explained in more detail in Algorithm 1. The main difference in our algorithm is that we calculated the excess energy factor when all CHs are out of energy (dead) but the rest of the nodes in the network are still alive; so, this factor helps in prolonging the CH's lifetime until all nodes in the network die out. It is also worth noting the sole factor that the construction of n_c depends on the positions of the nodes of n_s. By controlling n_s and identifying the most optimal construction for it, it acts as an initialization for a customizable system where a highly optimized n_c can be achieved in terms of the number of nodes and the total cost of energy of n_c (excluding the excess energy needed for the CHs to live until the wth round). Figure 2: Flowchart of the modified K-means LEACH algorithm. ```
Output: forr = 1: rmax forl = 1: n  Store the distances between each node and each of the k centroids. Store the minimum distance from the k number of distances between each node and the k centroids and the cluster number of it. Declare a struct X and store the positions, minimum distances, cluster numbers, and initial energies of the n_s nodes. endfor  Calculate the residual energy of the n_s nodes and store them in each round. Store the number of dead nodes in each round. Update the positions of cluster heads and store them. endfor if all the n_s nodes die out during the rmax rounds  w is the index of the node at which all the n_s dies out. else w = rmax endif  Store the index of the vth round at which the cluster heads stop moving. Establish a new set of nodes n_c in the network with the positions of the cluster heads each round from round 1 to v.  Eliminate the duplicates from the new set of nodes n_c if any. Declare a struct Y and include the coordinates, minimum distances, cluster numbers, initial energies, and types of the n_c nodes. forr = 1: rmax ifr <= v  Reset the type of all the n_c nodes to \"N\".