# Applying Machine Learning to DEEC Protocol: Improved Cluster Formation in Wireless Sensor Networks
## 3 Implementation
### Deec-K-nn
#### 3.2.1 Proposal Approach  
The following modification is proposed for the Distributed Energy-Efficient Clustering (DEEC) protocol:  
* Stage 1: Network Initialisation  
Let the WSN consist of \(n\) sensor nodes, each with initial energy \(E_{0}\) and spatial coordinates (\(x_{i},y_{i}\)), where \(i=1,2,\ldots,n\). Nodes are randomly deployed in a two-dimensional field of size \(100\,\mathrm{m}\times 100\,\mathrm{m}\). Initial CHs are selected according to the baseline DEEC protocol. The state of each node is stored in a database as shown in Equation (4):  
\[Node_{i}=\{x_{i},y_{i},E_{i},\,status_{i}\} \tag{4}\]  
where \(status_{i}\in\{\)alive, dead\(\}\). 0, '\(status\)': '\(alive\)'\(\}\) for \(i\) in \(range(n)\)].  
All nodes' positions, energy levels, and connectivity information are stored in a central database. Before each operational round, the network state is updated by removing nodes and CHs that have exhausted their energy reserves.  
* Stage 2: Calculation of \(D_{total}\) and \(E_{total}\)  
For each candidate CH, compute the Euclidean distance to the base station (BS) at (x\({}_{\text{BS}}\), yss) as shown in Equation (5):  
\[d_{i,BS}=\sqrt{\left(x_{i}-x_{\text{BS}}\right)^{2}+\left(y_{i}-y_{\text{BS}} \right)^{2}} \tag{5}\]  
Calculate the TOTAL distance (\(D_{total}\)) for a CH \(j\) as shown in Equation (6):  
\[D_{total,j}=\sum_{k\in C_{j}}\sqrt{\left(x_{i}-x_{j}\right)^{2}+\left(y_{i}-y_ {j}\right)^{2}} \tag{6}\]  
where \(C_{j}\) is the set of nodes assigned to CH \(j\).  
The total residual energy (\(E_{total}\)) for a CH is shown in Equation (7):  
\[E_{total,\ j}=\ E_{j}+\ \sum_{k\in\text{C}_{j}}E_{k} \tag{7}\]  
Stage 3: Cluster Head Selection Optimisation  
Cluster head selection is refined by applying a decision-making process that jointly considers \(D_{total}\) and \(E_{total}\). Specifically, nodes are evaluated based on their proximity to the BS and residual energy. The node with the optimal combination of minimal distance and maximal energy is designated the CH for its respective cluster. Therefore, cluster head selection is performed by evaluating each candidate node based on its distance to the BS and its residual energy. The selection rule can be formalised as shown in Equation (8):  
\[Select\ CH_{j}=agr\ max_{j}(\frac{E_{total,\ j}}{D_{total,\ j}+\ \in}) \tag{8}\]  
where \(\epsilon\) is a small constant to avoid division by zero.  
Stage 4: Node Assignment via K-NN Clustering  
For each non-CH node, the K-NN algorithm is used to identify the \(k\)-nearest CHs based on Euclidean distance. In cases where multiple CHs are nearby, preference is given to the CH with the highest residual energy and the shortest distance to the BS. This assignment process is iteratively repeated, ensuring that all nodes are efficiently clustered and that no CH is overloaded or underutilised.  
Stage 5: Iterative Re-Clustering and Network Update  
The clustering and CH selection process is repeated for each operational round. After each round, the network state is updated to reflect node energy levels and connectivity changes. Dead nodes and CHs are excluded from subsequent rounds, and the K-NN-based clustering is re-applied to adapt to the evolving network topology. This iterative process continues until the network ends its operational lifetime.