## 3 The Proposed Algorithm
### Cluster-Head Election Strategy Based on Cost Function  
Due to the characteristics of decentralization, each node in an ad hoc network can be the cluster head. Therefore, how to select a suitable node to be the cluster head and dynamically update it is a significant problem. A common method is to use multi-factor analysis to jointly evaluate the ability of each node as the cluster head. Based on this, we propose a cluster-head election strategy based on cost function (CHCF). The algorithm comprehensively considers two variables: distance and packet loss rate. There are two types of distance: the distance between the node and the next hop, as shown in Equation (4), and the distance between the node and the cluster center, as shown \begin{table}
\begin{tabular}{c c} \hline
**Stage** & **Function** \\ \hline Step 1: Initial phase & Initialize the network. At this time, all nodes do not belong to any cluster. \\ \hline Step 2: Flooding phase & All nodes in the network broadcast data packets containing their service type, geographic location, and IP address until each node knows the other nodes’ information. \\ \hline Step 3: Clustering phase & In-vehicle network: The service nodes with the same service type form a cluster, calculate the cost value based on the method in Section 3.2, and broadcast it in the cluster. \\ \cline{2-3}  & Vehicle–ground network: Nodes are clustered by the k-means method. Then, the cost value is calculated based on the method in Section 3.2 and broadcasted in the cluster. \\ \hline Step 4: Cluster-head electing phase & The node with the minimum cost becomes the cluster head. The selected information is broadcasted in the network. \\ \hline Step 5: Routing phase & The best route is selected according to the improved ACO method in Section 3.4. \\ \hline Step 6: Working phase & The member nodes and cluster heads start to work. Each cluster head node forwards data according to the method in Section 3.3. \\ \hline Step 7: Finish phase & When the network finishes a round, restart the network from Step 3. \\ \hline \end{tabular}
\end{table} 
Table 2: Network flow. Figure 2: Vehicle–ground network architecture. in Equation (5). Note that the center node here is a virtual node based on the location information of cluster member nodes. \[\text{dis}(i)=\sqrt{\left(x_{i}-x_{\text{next}}\ \right)^{2}+\left(y_{i}-y_{ \text{next}}\ \right)^{2}+\left(z_{i}-z_{\text{next}}\right)^{2}}, \tag{4}\]  
\[\text{cen}(i)=\sqrt{\left(x_{i}-\frac{1}{N}\underset{j=1}{\overset{N}{\underset {j=1}{\overset{N}{\underset{j=1}{\overset{N}{\underset{j}{\underset{j}{ \underset{j}{\underset{}}}}}}}}}}x_{j}\right)^{2}+\left(y_{i}-\frac{1}{N} \underset{j=1}{\overset{N}{\underset{j}{\underset{j}{\underset{j}{\underset{j}{ \underset{j}{\underset{j}{\underset{j}{\underset{}}}}}}}}}}y_{j}\right)^{2}+ \left(z_{i}-\frac{1}{N}\underset{j=1}{\overset{N}{\underset{j}{\underset{j}{ \underset{j}{\underset{j}{\underset{j}{\underset{j}{\underset{j}{\underset{j}{ \underset{}}}}}}}}}}}z_{j}\right)^{2}}, \tag{5}\]  
where \(\text{dis}(i)\) and \(\text{cen}(i)\) are the two separate types of distance. \((x_{i},y_{i},z_{i})\) represents the coordinate of node \(i\), \((x_{next},y_{next},z_{next})\) is the coordinate of the next hop node, and \(N\) refers to the node number in the cluster. The packet loss rate is essential for communication reliability. In CHCF, the cluster head is re-elected each time period, which is denoted by \(T\). At the end of each round, clusters calculate the average packet loss rate according to the packet number sent by the previous cluster and received by the current cluster head, as shown in Equation (6):  
\[loss\_rate=1-\frac{Packet\_receive}{Packet\_send}. \tag{6}\]  
We use the two variables to calculate each node's cost value, and the node with the minimum cost value is elected as the cluster head.