## 3 The Proposed Resource Allocation Scheme for D2D Communication
### Subcarrier Assignment  
In this subsection, we first need to solve the optimization problem for the case that one D2D pair \(k\) shares the same subcarrier with the CU \(m\). Then we find the optimal matching of subcarriers between the D2D pairs and CUs by exploiting the feasible local optimum power allocation. Finally, we propose a heuristic subcarrier assignment algorithm to decide the subcarrier set \(\mathcal{M}_{k}\) for each D2D pair. Consider that the D2D pair \(k\) shares the \(m\)th subcarrier with CU \(m\). To maximize the sum spectrum efficiency of \(k\) and \(m\), the considered problem can be formulated as  
\[ \begin{array}{ll}\max\limits_{p_{k,m}^{\mathrm{D}},k,m}&R_{k,m}^{CD}(p_{k,m }^{\mathrm{D}})\triangleq R_{k,m}^{D}(p_{k,m}^{D})+R_{k,m}^{C}(p_{k,m}^{D})\\ s.t.\mathrm{C1}:&\frac{p_{k,m}^{\mathrm{D}},\mu_{k,m}^{DD}}{p_{k,m}^{\mathrm{D }}k_{k,m}^{\mathrm{D}}+\sigma_{0}^{2}}\geq\frac{p_{k,m}^{\mathrm{D}}\mu_{k,m}^ {\mathrm{D}}+\sigma_{0}^{2}}{\sigma_{0}^{2}},\\ &\mathrm{C2}:p_{k,m}^{\mathrm{D}}\mu_{k,B}^{\mathrm{D}}\triangleq\frac{p_{k, m}^{\mathrm{D}}\mu_{k,B}^{\mathrm{D}}}{2^{\mathrm{K}}\mu_{k,m}^{\mathrm{D}}}- \sigma_{0}^{2},\\ &\mathrm{C3}:0\leq p_{k,m}^{D}\leq p_{\mathrm{thr}}^{D}.\end{array}  \tag{15}\]  
According the constraints C1 and C2 in problem (15), we can easily get  
\[\left\{ \begin{array}{ll}p_{k,m}^{\mathrm{D}}\geq\frac{\sigma_{0}^{2}\big{(} p_{k,m}^{\mathrm{D}}\mu_{k,m}^{\mathrm{D}}+\sigma_{0}^{2}\big{)}}{\sigma_{0}^{2} \big{(}h_{k,B}^{\mathrm{D}}-h_{k,B}^{\mathrm{D}}\big{)}-p_{k}^{\mathrm{D}}h_{k, B}^{\mathrm{D}}\big{)}}\triangleq p_{k,m}^{\mathrm{D}},\\ p_{k,m}^{\mathrm{D}}\leq\frac{p_{m}^{\mathrm{D}}\mu_{k,B}^{\mathrm{D}}}{h_{k,B}^{ \mathrm{D}}\left(2^{\mathrm{K}}\mu_{k,m}^{\mathrm{D}}-1\right)}-\frac{\sigma_{ 0}^{2}}{h_{k,B}^{\mathrm{D}}}\triangleq p_{k,m}^{\mathrm{D}}\max\limits.\end{array} \right. \tag{16}\]  
and we define  
\[p_{k,m}^{\mathrm{D}}\triangleq\max\left\{0,\frac{\sigma_{0}^{2}\big{(}p_{m}^ {\mathrm{D}}h_{k,m}^{\mathrm{D}}+\sigma_{0}^{2}\big{)}}{\sigma_{0}^{2}\big{(}h_ {k}^{\mathrm{D}}-h_{k,B}^{\mathrm{D}}\big{)}-p_{m}^{\mathrm{C}}h_{k,m}^{ \mathrm{D}}h_{k,B}^{\mathrm{D}}\big{)}}\right\} \tag{17}\]\[p_{k,m}^{D\text{ max}}\triangleq\min\left\{P_{\text{thr}}^{D},\frac{p_{m}^{C}h_{m,B} ^{CB}}{h_{k,B}^{\text{DB}}\left(2^{R_{m,\min}^{C}}-1\right)}-\frac{\sigma_{0}^{2} }{h_{k,B}^{\text{DB}}}\right\} \tag{18}\]  
as the minimum and maximum possible powers of the D2D-Tx \(k\) on the \(m\)th subcarrier, respectively. Generally, \(p_{k,m}^{D\text{ min}}\leq p_{k,m}^{D\text{ max}}\) should be satisfied. If \(p_{k,m}^{D\text{ min}}>p_{k,m}^{D\text{ max}}\), it means that D2D pair \(k\) cannot share resources with CU \(m\). Then (15) can be  
repeats until all subcarriers of the CUs have been assigned or cannot be assigned due to the maximum transmit power threshold being reached or because the transmit power is zero. If there are some subcarriers remaining that are still unshared by any D2D pair, we will try to allocate the remaining cellular resources to the D2D pairs that can further improve the spectrum efficiency gain. ```
1:Initialization: Set \(\mathcal{C}_{k}=\mathcal{M},\forall k\), \(\mathcal{M}_{k}=\varnothing,\forall k\), \(P_{k}^{ini}=0,\forall k\). \(\pi_{k,m}=0,\forall k\), \(m\), \(\Omega=\mathcal{M}\); Obtain all optimum transmitting power of DU \(p_{k,m}^{D*}\) and sum spectrum efficiency \(R_{k,m}^{CD}\Big{(}p_{k,m}^{D*}\Big{)}\) by solving the optimization problem (15).