## 6 Proposed Joint Mode Selection and Resource Allocation Scheme (SD-Scheme)
### Mode Selection and Channel Assignment Scheme (C-Scheme)  
Firstly, this study examines a mode selection technique for D2D communication by employing a greedy algorithm. The method focuses on direct, relay, and local route D2D modes to enhance the performance of the D2D scheme. The mode selection issue is tackled based on the maximum SINR to calculate the best mode for each D2D pair, while considering the distance between the transmitter and the receiver. This will guarantee the chosen mode optimizes signal quality while taking into account the physical relationship of the communication devices, thereby maximizing overall performance. However, a threshold level is considered for the distance between the transmitter and the receiver for each D2D communication in the proposed network. Let us suppose that \(\mathcal{M}\) is a set of 0 and 1 elements that are applied to represent which mode is chosen. The following formula is applied to select the best mode:  
\[\mathcal{M}_{D2 Dn}=\mathcal{M}_{D}+\mathcal{M}_{R}+\mathcal{M}_{L}, \tag{12}\]  
where \(\mathcal{M}_{D}\), \(\mathcal{M}_{R}\), and \(\mathcal{M}_{L}\) represent the mode selection sets of DM, RM, and LM, respectively. If the data rate in a DM is greater than in an RM and an LM for each given \(n\) D2D communication pair, then \(\mathcal{M}_{D}=1\) and \(\mathcal{M}_{R}=\mathcal{M}_{L}=0\), and similarly for other cases. While a value in the set of the mode selection \(\mathcal{M}_{D2 Dn}\) is 1, the associated mode is selected (\(\mathcal{M}_{D}\), \(\mathcal{M}_{R}\), or \(\mathcal{M}_{L}\)), subsequently adding that particular data rate of that D2D pair \(n\) to the sum data rate of the network. Conversely, when a value in the mode selection set \(\mathcal{M}_{D2 Dn}\) is valued as 0, it leads to a missing contribution to the sum data rate of that particular D2D pair \(n\). Once the optimal D2D mode is determined in a scenario including DM, RM, and LM, the gNB employs the matching method to assign optimum reused sub-channels for the D2D pairs, which increases the spectrum utilization in the proposed network. This part introduces the model of a channel assignment issue to optimize the sum data rate and SE accordingly. We define the channel assignment formula in which D2D pair \(n\) shares the sub-channel \(k\) with CU \(m\) at time slot t as follows:  
\[\Pi=\left(\Pi_{n,k}^{1},\Pi_{n,k}^{2},\;\Pi_{n,k}^{3}\right)_{N\times 3K^{ \prime}} \tag{13}\]  
The vectors \(\Pi_{n,k}^{1},\Pi_{n,k}^{2}\), and \(\Pi_{n,k}^{3}\) indicate the possibility that the D2D pair is assigned to DM, RM, or LM, using the shared sub-channel \(k\) with CUs. For each D2D mode in the proposed scheme, the quota (\(\varnothing\)) can be determined. \(\varnothing\) represents the threshold of mode-channel assignment for every D2D pair within the framework. The \(\varnothing\) features can be illustrated as follows:  
\[ \begin{array}{lclclcl}\bullet&\sum_{k=1}^{K}\sum_{n=1}^{N}\Pi\,\,\Pi_{n,k}^{ 1}\leq&\varnothing^{D},&\forall n\in\mathcal{M}^{D}\\ \bullet&\sum_{k=1}^{K}\sum_{n=1}^{N}\Pi\,\,\Pi_{n,k}^{2}\leq&\varnothing^{R}, &\forall n\in\mathcal{M}^{R}\\ \bullet&\sum_{k=1}^{K}\sum_{n=1}^{N}\Pi\,\,\Pi_{n,k}^{3}\leq&\varnothing^{L},& \forall n\in\mathcal{M}^{L}\end{array} \]  
Based on the above criteria, it is crucial that the overall set of D2D pairs across the modes, that are assigned to sub-channel \(k\), should not exceed \(\varnothing\). In the matching game, the establishment of the two-sided preference lists can be represented as follows:  
\[\ell^{p}=\Big{\{}R_{n,k}^{D}(t),R_{n,k}^{R}(t),\;R_{n,k}^{L}(t)\Big{\}}, \tag{14}\]\[\ell^{s}=\{P_{C}(t)G_{nm}\}, \tag{15}\]  
where \(\ell^{p}\) and \(\ell^{s}\) denote preference lists that consist of D2D pairs arranged in descending sequence according to their highest data rate and CUs organized in ascending sequence according to their minimal interference effect, respectively. While performing the matching game theory, every D2D pair within the cell is proposed to earn the sub-channels with its higher priority.