## 5 Heuristic Algorithm
### Routing Policy  
To facilitate the CH in evaluating the current network conditions to decide how to forward the data packets to the RSU/BS, \(u_{i}^{CV2X}\) is used to represent the average delay of \(x\) packets transmitted from \(a_{i}\) via C-V2X, and \(u_{i}^{DSRC}\) is used to represent the average delay of \(x\) packets transmitted from \(a_{i}\) via DSRC. Their calculation formulas are expressed as follows:  
\[u_{i}^{CV2X}=\frac{1}{x}\times\sum_{k=0}^{x}\left(\frac{S_{ik}}{f^{CV2X}}+q_{ ik}^{CV2X}\right),\theta_{ik}=1 \tag{9}\]  
\[u_{i}^{DSRC}=\frac{1}{x}\times\sum_{k=0}^{x}\sum_{e(m,n)\in Path_{k}}\left( \frac{S_{ik}}{f^{DSRC}}+q_{ik}^{m}\right),\theta_{ik}=0 \tag{10}\]  
This paper assumes that \(u_{i}^{CV2X}\leq l^{min}\) is always true [13]. When \(a_{i}\) needs to send data, it will first select the packet with the largest weight from \(\mathcal{B}_{i}\) according to Equation (5). If \(u_{i}^{DSRC}\leq l_{ik}\), \(a_{i}\) will use VANETs to send the packet; otherwise, C-V2X will be selected to send the packet after sending a packet. Update \(l_{ik},k\in\{1,2,\dots\}\) and \(\omega_{ik},k\in\{1,2,\dots\}\) of all packets in the buffer, and update \(u_{i}^{CV2X}\) or \(u_{i}^{DSRC}\), every \(x\) packet sent via C-V2X or VANET. As long as the data packets generated by \(a_{i}\) are sent to the BS through the C-V2X method, the cost will be paid by \(a_{i}\).  
The pseudocode of the routing policy is shown in Algorithm 3. Lines 2 to 8 are used to select the current packet that needs to be sent. Lines 9 to 13 are used to select the sending method. Line 14 is used to update the packet weight and maximum allowable delay in real-time. Line 15 is used to update the average delay according to Equations (9) and (10). After obtaining the routing paths of VANETs, each CH independently runs Algorithm 3. Combined with Equations (9) and (10), the real-time updated \(u_{i}^{CV2X}\) and \(u_{i}^{DSRC}\) enable the CH to grasp the current network state to a certain extent. If congestion occurs in the network, \(u_{i}^{CV2X}\) and \(u_{i}^{DSRC}\) obtained by feedback will inevitably increase, which will cause the CH to adjust the method employed for sending packets.  
In Algorithms 2 and 3, all constraints of Equation (8) are satisfied. By optimizing the VANET link, the delay of transmitting data packets through VANETs is reduced as much as possible, enabling as few packets as possible to be transmitted through C-V2X, reducing the communication costs of users. This approach achieves the goal of balancing QoS and communication costs.  
Figure 4: Example diagram of the running process of Algorithm 2. (**a**) The topology diagram between CH and RSU; (**b**–**h**) the running processes of Algorithm 2.  
```
Input:\(Path_{i_{k}},a_{i},l_{i}^{DSRC},\mathcal{B}_{i}\in a_{i},Path_{i}\) Output:\(None\)
1 while\(\mathcal{B}\
eq\varnothing\)do
2\(max\gets 0\);
3\(b_{K}\leftarrow None\);
4 forall\(b_{k}\) in \(\mathcal{B}\)do
5 if\(\omega_{ik}\geq max\)then
6\(b_{K}\gets b_{k}\);
7
8 end if
9 if\(l_{iK}\leq u_{i}^{DSRC}\)then
10 Send \(b_{K}\) to the next node of \(Path_{i}\) using VANETs;
11 else
12 Send \(b_{K}\) to BS using C-V2X;
13
14 end if
15 Update \(l_{ik}\) and \(\omega_{ik}\) for all packages in \(\mathcal{B}\);
16 Update \(u_{i}^{CV2X}\) and \(u_{i}^{DSRC}\);
17
18 end
```  
**Algorithm 3**Routing Policy. The algorithm used to choose which way (DSRC or C-V2X) to send the packet.