## 3 Resource Allocation under Total Power Constraint
### Power Allocation  
The Lagrangian in Equation (3) is given by  
\[ \begin{array}{ll}L&=\sum_{i=1}^{N}\sum_{j=1}^{N}\rho_{i,j}r_{i,j}+\lambda_ {1}\left[p_{T}-\sum_{i=1}^{N}\sum_{j=1}^{N}\rho_{i,j}\left(p_{s,i}+p_{r,j} \right)\right]+\mu_{1}\left[Q_{1}-\sum_{i=1}^{N}p_{s,i}c_{i}\right]+\mu_{2} \left[Q_{2}-\sum_{j=1}^{N}p_{r,j}d_{j}\right]\\ &=\sum_{i=1}^{N}\sum_{j=1}^{N}\rho_{i,j}\left[r_{i,j}-\lambda_{1}\left(p_{s,i }+p_{r,j}\right)-\mu_{1}p_{s,i}c_{i}-\mu_{2}p_{r,j}d_{j}\right]+\lambda_{1}P_{ T}+\mu_{1}Q_{1}+\mu_{2}Q_{2}\end{array}  \tag{4}\]  
where \(\lambda_{1}\), \(\mu_{1}\) and \(\mu_{2}\) are dual variables associated with the power and interference constraints. According to the Karush-Kuhn-Tucker criterion [25], we take a partial derivative on \(L\) with respect to \(p_{s,i}\) and \(p_{r,j}\) as  
\[\frac{\partial L}{\partial p_{s,i}}= \frac{a_{i}b_{j}^{2}p_{r,j}^{2}+a_{i}^{2}g_{i}p_{s,i}^{2}+b_{j}^{2 }g_{i}p_{r,j}^{2}+2a_{i}b_{j}g_{i}p_{s,i}p_{r,j}}{2\ln 2\left(a_{i}p_{s,i}+b_{j}p_{r,j} \right)\left(a_{i}p_{s,i}+b_{j}p_{r,j}+a_{i}b_{j}p_{s,i}p_{r,j}+b_{j}g_{i}p_{s,i }p_{r,j}+a_{i}g_{i}p_{s,i}^{2}\right)}-\lambda_{1}-\mu_{1}c_{i} \tag{5}\]  
\[\frac{\partial L}{\partial p_{r,j}}= \frac{a_{i}^{2}b_{j}p_{s,i}^{2}}{2\ln 2\left(a_{i}p_{s,i}+b_{j}p_{r,j} \right)\left(a_{i}p_{s,i}+b_{j}p_{r,j}+a_{i}b_{j}p_{s,i}p_{r,j}+b_{j}g_{i}p_{s, i}p_{r,j}+a_{i}g_{i}p_{s,i}^{2}\right)}-\lambda_{1}-\mu_{2}d_{j} \tag{6}\]Setting the partial derivative Equations (5) and (6) to be zero, we get the optimal power allocation given by  
\[p_{s,i}^{\star}=\left[\frac{a_{i}b_{j}^{2}f_{i,j}^{2}+a_{i}^{2}g_{i}+2a_{i}b_{j}g _{i}f_{i,j}+b_{j}^{2}g_{i}f_{i,j}^{2}}{2\ln 2\left(a_{i}+b_{j}f_{i,j}\right) \left(\lambda_{1}+c_{i}\mu_{1}\right)\left(b_{j}g_{i}f_{i,j}+a_{i}b_{j}f_{i,j} +a_{i}g_{i}\right)}-\frac{a_{i}+b_{j}f_{i,j}}{b_{j}g_{i}f_{i,j}+a_{i}b_{j}f_{ i,j}+a_{i}g_{i}}\right]^{+} \tag{7}\]  
\[p_{r,j}^{\star}= f_{i,j}p_{s,i} \tag{8}\]  
where  
\[f_{i,j}= \begin{cases}\frac{a_{i}\sqrt{\left(\lambda_{1}+d_{j}\mu_{2}\right) \left(a_{i}b_{j}\left(\lambda_{1}+c_{i}\mu_{1}\right)+b_{j}g_{i}\left(\lambda_ {1}+c_{i}\mu_{1}\right)-a_{i}g_{i}\left(\lambda_{1}+d_{j}\mu_{2}\right)\right) -a_{i}g_{i}\left(\lambda_{1}+d_{j}\mu_{2}\right)}}{b_{j}\left(a_{i}+g_{i} \right)\left(\lambda_{1}+d_{j}\mu_{2}\right)}&\text{if }b_{j}\left(\lambda_{1}+c_{i}\mu_{1} \right)\\ &>g_{i}\left(\lambda_{1}+d_{j}\mu_{2}\right)\\ 0&\text{otherwise}\end{cases}  \tag{9}\]  
and \(\left(\cdot\right)^{+}=\max\left(0,\cdot\right)\). Different from the standard water-filling form, the powers in Equations (7) and (8) are tailored for the interference management constraints by interference channel gains \(c_{i}\) and \(d_{j}\). As can be seen from the formula Equation (9), \(f_{i,j}\) is the criterion for choosing the relaying link and the direct link. If the condition \(b_{j}\left(\lambda_{1}+c_{i}\mu_{1}\right)>g_{i}\left(\lambda_{1}+d_{j}\mu_{2}\right)\) holds, the relaying link is more advantageous than the direct link. Otherwise, \(SS\) would prefer to choose a direct link rather than the aid from \(SR\). In particular, in the case of direct transmission, power allocation degenerates into a classical water filling algorithm given by  
\[p_{s,i}^{\star}=\left[\frac{1}{2\ln 2\left(\lambda_{1}+c_{i}\mu_{1}\right)}- \frac{1}{g_{i}}\right]^{+} \tag{10}\]  
and \(p_{r,j}=0\). Next, let's look at an interesting special case. When there is only one subcarrier (\(N=1\)), corresponding to a narrow band flat fading scenario, there are four different cases of optimal power allocation. (1) If the interference threshold of the primary network is large enough to tolerate the power value at which the secondary network achieves its maximum rate without interference constraint, then the optimal power allocation is exactly the same as that without interference constraint. Mathematically, the optimal power allocation is given by  
\[p_{s}^{(1)}= \begin{cases}\frac{b\left(g+\sqrt{ab-ag+bg}\right)P_{T}}{\sqrt{ab -ag+bg}\left(b+\sqrt{ab-ag+bg}\right)}&\text{if }b>g\\ P_{T}&\text{otherwise}\end{cases}