## 2 Background
### Leach  
LEACH is a cluster-based routing protocol designed for sensor networks that utilizes in-network hierarchical routing and incorporates three main concepts: (i) localized coordination and control for cluster setup and operations, (ii) rotation and randomization of cluster heads, and (iii) data aggregation, as described in Algorithm 2[54]. By forming clusters, LEACH enhances the longevity of the network through in-network aggregation, enabling close-range communication among nodes within a cluster while reserving long-range communication for interactions with the base-station. Additionally, the adaptive clustering and rotation of cluster heads help to balance node utilization across the sensor network, thereby improving overall energy efficiency. ```
1:Input
2:\(D\): a set of training samples {(x_1,y_1),...,((x_n,y_n))}
3:\(k\): the number of nearest neighbors
4:for each training sample (x_i,y_1) \(\in\)\(D\)do
5: Compute \(d(x\),x_i)
6:endfor
7: let \(N\subseteq D\) be the set of training samples with the \(k\) smallest distance
8:return the majority labile of the sample in \(N\)
```  
**Algorithm 2** k-NN Algorithm. LEACH operates under the assumption that all nodes start with the same energy level and that functioning as a cluster head consumes approximately the same amount of energy for each node. This sensor network approach works well for IoT applications characterized by a large number of nodes with limited energy resources, particularly in scenarios where data transmission distances can be minimized and where periodic data monitoring is required to maintain efficient and reliable operations. The operation of LEACH can be divided into two phases: the setup phase and the steady phase. During the setup phase, the network is organized into non-overlapping clusters, each with its own cluster head. LEACH maintains dynamic formation and selection of clusters and cluster heads. The selection of cluster heads occurs independently across the network, utilizing a mechanism that prevents any cluster head from being permanently assigned (i.e., static). Each node generates a random number (\(n\)) between 0 and 1, and nodes with an \(n\) value lower than a predefined threshold function \(T\)(\(n\)) are selected as cluster heads. This approach ensures that each node has the opportunity to become a cluster head at least once. The threshold function \(T\)(\(n\)) is computed as follows:  
\[T(n)= \begin{cases}\frac{p}{1-p(r\mod\frac{1}{p})}&n\in G\\ 0&\text{others}\end{cases}  \tag{1}\]  
In this context, \(G\) represents the set of nodes that have not served as cluster heads in the last \(\frac{1}{p}\) rounds, while \(r\) denotes the current round, and \(p\) indicates the desired percentage of cluster heads. The threshold \(T(n)\) ensures that once a node is selected as a cluster head, it will not be chosen again for the next \(\frac{1}{p}\) rounds. The subsequent phase, known as the cluster head advertisement phase, begins when the selected cluster heads use a CSMA MAC protocol to announce their presence to neighboring nodes, prompting non-cluster head nodes to activate their receivers. Once this phase concludes, non-cluster head nodes select the cluster to which they will belong for the current round, taking into account the signal strength of the cluster head's advertisement. Choosing the cluster head with the strongest signal is believed to minimize the energy required for communication within the cluster. In cases of a draw, the selection of a cluster head is resolved randomly. Furthermore, each node informs the cluster head of its intention to join the cluster, marking this stage as the setup phase. The following stage is the steady stage, during which each cluster head develops a TDMA schedule that allows cluster members to transmit their data and aggregate the received information. As a result, LEACH facilitates the aggregation of data collected from IoT devices in a distributed manner without intervention from the base-station.