## 4 Optimal Resource Allocation
### Optimizing Primal Variables at a Given Dual Point
**(2) Optimal Subcarrier Allocation**: Substituting the obtained power allocation into Equations (21) and (22) respectively, we have \[L_{n,1}\left(\mathbf{\tau},\mathbf{\alpha},\mathbf{\beta}\right) =\sum\limits_{k=1}^{K}\left[\alpha_{k,1}\tau_{n,k,1}\ln\left(1+ \frac{h_{n,k,1}P_{s}}{\sigma^{2}N}\right)-\beta_{k}\tau_{n,k,1}\xi\left(h_{n,k,1 }\frac{P_{s}}{N}+\sigma^{2}\right)\right],\forall n \tag{25}\] \[L_{n,2}\left(\mathbf{\tau},\mathbf{\alpha},\mathbf{\beta}\right) =\sum\limits_{k=1}^{K}\left[\alpha_{k,2}\tau_{n,k,2}\ln\left( \frac{h_{n,k,2}\mathbf{\alpha}_{n,2}}{\sigma^{2}\beta_{k}}\right)-\beta_{k}\tau_{ n,k,2}\left(\frac{\mathbf{\alpha}_{n,2}}{\beta_{k}}-\frac{\sigma^{2}}{h_{n,k,2}} \right)\right],\ \forall n \tag{26}\]  
where \(P_{s}\) is the source's transmitted power. By extracting the common factors \(\tau_{n,k,1}\) and \(\tau_{n,k,2}\) respectively, Equations (25) and (26) can be expressed as  
\[L_{n,t}\left(\mathbf{\tau},\mathbf{\alpha},\mathbf{\beta}\right)=\sum\limits_{k=1}^{K} \rho_{n,k,t}H_{n,k,t},\ t=1,2 \tag{27}\]  
where  
\[H_{n,k,1} =\alpha_{k,1}\ln\left(1+\frac{h_{n,k,1}P_{s}}{\sigma^{2}N}\right) -\beta_{k}\xi\left(h_{n,k,1}\frac{P_{s}}{N}+\sigma^{2}\right) \tag{28}\] \[H_{n,k,2} =\alpha_{k,2}\ln\left(\frac{h_{n,k,2}\mu_{n,2}}{\sigma^{2}\beta_{ k}}\right)-\beta_{k}\left(\frac{\mu_{n,2}}{\beta_{k}}-\frac{\sigma^{2}}{h_{n,k,2}} \right). \tag{29}\]  
Based on the result of the above analysis, we can finally get  
\[g\left(\mathbf{\alpha},\mathbf{\beta}\right)=\sum\limits_{k=1}^{K}\beta_{k}\left(\sum \limits_{n=1}^{N}\xi\left(h_{n,k,1}\frac{P_{S}}{N}+\sigma^{2}\right)-P_{r} \right)+\sum\limits_{i=1}^{2}\sum\limits_{n=1}^{N}\left(\max\sum\limits_{k=1 }^{K}\tau_{n,k,i}H_{n,k,i}\right). \tag{30}\]  
It can be concluded that in order to maximize \(g\left(\mathbf{\alpha},\mathbf{\beta}\right)\), the optimal time-sharing parameter \(\left\{\tau_{n,k,t}^{*}\right\}\) should have the largest \(\sum\limits_{k=1}^{K}\tau_{n,k,t}H_{n,k,t}\) for subcarrier \(n\) in hop \(t\). As stated in Section 3, the optimal solution obtained using the time-sharing strategy approximates the optimal goal obtained utilizing integer channel allocation optimization. According to the constraints of (12) and (13), if the subcarrier \(n\) over hop \(t\) is allocated to the relay \(k\), then \(\tau_{n,k,t}=1\), otherwise \(\tau_{n,k^{\prime},t}=0\), for \(k^{\prime}\
eq k\). It can also be observed that subcarriers for information transmission in the same timeslot are independent of each other. Therefore, in ensuring the validity of Lagrange dual method, the optimal subcarrier allocation is given by  
\[\tau_{n,k,t}^{*} =\left\{ \begin{array}{ll}1,\ \text{if}\ k=\arg\max_{k}\left\{H_{n,k,t} \right\}\\ 0,\ \text{otherwise}.\end{array} \right. \tag{31}\]  
The complexity of calculating the \(\tau_{n,k,t}^{*}\) is \(O(NK)\). Therefore, the proposed algorithm complexity solving **P2** is \(O\left(\left(3K\right)^{\
u}\cdot 1\cdot NK\right)=O\left(NK(3K)^{\
u}\right)\). The flowchart representing the logic of our algorithm and its pseudocode are shown in Figure 2 and Algorithm 1. ```
1:Initialize \(\left\{\beta_{k},\alpha_{k,t}\right\}\), and given \(P_{S}\). 2:Repeat
3:Compute the optimal power allocations \(\left\{p_{n,k,2}^{*}\right\}\) in (24) and \(\left\{H_{n,k,i}\right\}\) in (28), (29). 4:Obtain the optimal subcarrier allocations \(\left\{S_{1,k}^{\ 1},S_{1,k}^{\ 1},S_{2,k}^{\ *},S_{2,k}^{\ *}\right\}\) according to (31). 5:Update \(\left\{\beta_{k},\alpha_{k,t}\right\}\) by the subgradient method defined in (17), (18). 6:Until \(\left\{\beta_{k},\alpha_{k,t}\right\}\) converge. ```  
**Algorithm 1** Proposed Algorithm for **P2**