## 5 Proposed Approach
**STAGE 3**: Choosing CHs depending on energy consumption and distance to the base station  
- Decision Making: Use conditional logic to choose CHs based on \(D_{total}\) and \(E_{total}\):  
if CHaDtotal<CHbDtotal&&CHaEtotal>CHbEtotal  
ChooseCHa as the cluster head in this cluster area and make CHb the normal  
node in the network. Elseif CHaDtotal>CHbDtotal&&CHaEtotal>CHbEtotal  
Choose CHa as the cluster head in this cluster area and make CHb the normal  
node in the network. Elseif CHaDtotal<CHbDtotal&&CHaEtotal<CHbEtotal  
Choose CHb as the cluster head in this cluster area and make CHa the normal  
node in the network. Elseif CHaDtotal<CHbDtotal&&CHaEtotal<CHbEtotal  
Choose CHb as the cluster head in this cluster area and make CHa the normal  
node in the network. - Store Modifications: save new modifications in \(DB_{2}\)  
**STAGE 4**: The KNN algorithm is applied to classify nodes based on their proximity to CHs  
- The following steps are performed in this stage:  
\(\circ\)  Input Data: All nodes' coordinates and energy levels are used as input features. \(\circ\)  Nearest Neighbour Search: The K nearest CHs are identified using the Euclidean distance metric for each node. \(\circ\)  CH Assignment: Nodes are assigned to the nearest CH with sufficient residual energy. If a CH's energy falls below a threshold, it is excluded from the selection process. \(\circ\)  CH Optimisation: If multiple CHs are located in close proximity, the CH with the highest residual energy and shortest distance to the BS is retained, while others are demoted to regular nodes. - Load \(DB2\): Load the modified database:  
Node=DB2(:,1:2); %get data=coordinate(x,y) from set of data \"DB2\" at (all row & column 1,2)  
- Assigned CH coordination:  
CH = [x,y];  
- Select Nearest Nodes: Use the KNN function to select nodes nearest to CH. [n,d] = knnsearch(CH,newpoint,'k',N);  
CH_closest=CH(n,:);  
% coordinate of N neighbour points  
% CH_closest:coordinate(x,y) of no neighbour points  
% n: labelling(order) of each neighbour point  
% d:distance from \"each neighbour point\" to \"CH\"  
- Implement Results: Highlight the nearest neighbour points in a figure. Preparing data for the next round:  
C=setdiff(M,CH_closest,'rows');  
% returns the data in M that is not in CH-closest,  
% with no repetitions. C is in sorted order  
**STAGE 5**: Repeat STAGE 4 until completion  
- The KNN algorithm is reapplied to reassign nodes to the nearest active CH. - The network is prepared for the next round. The proposed algorithm is based on two main concepts: the number of live cluster heads in the network, and the number of nodes that will join the live CHs using the KNN algorithm. The number of live cluster heads significantly impacts the management of nodes in the network because these cluster heads will manage the nodes in the network that decided to join this cluster head. On the other hand, the BS will receive all information from the nearest cluster heads, which will affect the energy level of the network. The KNN algorithm assists network nodes in determining which cluster heads to join. After excluding all dead nodes, each sensor will select and join only one active cluster head. MATLAB simulation was used to modify LEACH, SEP, TEEN, and DEC protocols. The following table shows the parameters used in the simulation of the protocols. These implementation modifications reduce energy consumption, enhance network stability, and balance the distribution of energy resources across the network.