## 4 Dynamic Weighted Clustering Algorithm with Dual Cluster Heads
### Algorithm Procedure  
The detailed procedure of DWCA-DCH is shown in Algorithm 1. In the initialization stage, parameters are set such as the ideal cluster size, weighting coefficients, and the UAV energy threshold, etc. The UAV status array contains its coordinates, speed, energy, and so on. The set \(\mathcal{U}\) includes all UAV nodes in the system initially. The set \(\mathcal{C}\), \(\mathcal{P}\), \(\mathcal{B}\) are defined in Section 3, respectively. The DWCA-DCH algorithm determines cluster heads according to the weight of nodes in the initial clustering stage. First, the weight of each node is calculated according to Equation (6). In each cycle, the node with the lowest pCH weight in the node set that has not been clustered is appointed as the pCH of the cluster, and the unclustered neighbors of the selected pCH node are included in the cluster. Afterward, the selected pCH and its cluster member are removed from \(\mathcal{U}\), until \(\mathcal{U}\) is empty. At this point, all UAV nodes have been clustered. After the clustering, the node with the lowest bCH weight in each cluster is selected as the bCH according to Equation (7). In this way, the initial clustering of UAVs based on the DWCA-DCH algorithm is completed. ```
Initialization:  Setup parameters (ideal cluster size, weighting coefficients, UAV energy threshold, etc.) Initialize the UAV state array (coordinates, velocity, energy) and the set of unclustered nodes \(\mathcal{U}\)  Seek for the set of unclusted neighbors of each node, define \(\mathcal{J}_{i},i=1,2,\ldots,N\) as the set of unclusted neighbor of node \(i\) (\(\mathcal{J}_{i}=N_{i}\cap\mathcal{U}\)) Initial clustering stage: while\(\mathcal{U}\
eq\phi\)do for all\(i\in\mathcal{U}\)do  Calculate \(W_{i}^{p}\) according to Equation (6) endfor \(\pi=\underset{i\in\mathcal{U}}{\operatorname*{arg\,min}}\ \{W_{i}\}\) \(\mathcal{P}_{h}\leftarrow\pi\) \(\mathcal{C}_{h}=\mathcal{P}_{h}\cup\mathcal{J}_{\pi}\) \(\mathcal{B}_{h}\leftarrow\) the node with the lowest \(W_{i}^{b}\) in the set \(\mathcal{C}_{h}\) \(\mathcal{U}=\mathcal{U}\setminus\mathcal{C}_{h}\) Update \(\mathcal{J}\) endwhile Dynamic adjustment stage:  Continue traversing all nodes (assume that the current traversing node is \(i\), its cluster is \(\mathcal{C}_{r}\), the pCH and bCH in \(\mathcal{C}_{r}\) are \(\mathcal{P}_{r}\) and \(\mathcal{B}_{r}\), respectively.) if The energy of node \(i\)\(E_{i}<E_{th}\) or node \(i\) fails then if\(i=\mathcal{P}_{r}\)then \(\mathcal{P}_{r}\leftarrow\mathcal{B}_{r}\) \(\mathcal{U}\leftarrow\mathcal{C}_{r}\) \(\mathcal{B}_{r}\leftarrow\) reselected bCH after calculating \(W_{i}^{b}\) endif if\(i=\mathcal{B}_{r}\)then \(\mathcal{U}\leftarrow\mathcal{C}_{r}\) \(\mathcal{B}_{r}\leftarrow\) reselected bCH after calculating \(W_{i}^{b}\) endif \(\mathcal{C}_{r}=\mathcal{C}_{r}\setminus i\) endif if\(dist(i,\mathcal{P}_{r})>range\)then if\(i=\mathcal{B}_{r}\)then \(\mathcal{U}\leftarrow\mathcal{C}_{r}\) \(\mathcal{B}_{r}\leftarrow\) reselected bCH after calculating \(W_{i}^{b}\) endif \(\mathcal{C}_{r}=\mathcal{C}_{r}\setminus i\) \(\mathcal{C}_{q}=\mathcal{C}_{q}\cup i\) (Assume that node \(i\) will join the cluster \(\mathcal{C}_{q}\)) endif \(\mathcal{U}\leftarrow\mathcal{C}_{r}\)  Calculate \(W_{i}^{p}\) if\(W_{i}^{p}\) is less than \(W_{\mathcal{P}_{r}}^{p}\) and stays there for a period of time then if\(i=\mathcal{B}_{r}\)then \(\mathcal{B}_{r}\leftarrow\) reselected bCH after calculating \(W_{i}^{b}\) endif \(\mathcal{P}_{r}\gets i\) endif ifCharging or maintenance of node \(i\) is complete then \(\mathcal{C}_{q}=\mathcal{C}_{q}\cup i\) (Assume that node \(i\) will join the cluster \(\mathcal{C}_{q}\)) endif
```  
**Algorithm 1** Dynamic Weighted Clustering Algorithm with Dual Cluster Heads  
In this way, the initial clustering of UAVs based on the DWCA-DCH algorithm is completed. During the dynamic adjustment stage, all nodes are continuously traversed and the cluster is maintained.