# Joint Uplink and Downlink Resource Allocation for D2D Communications System
## 3 Resource Allocation Algorithm
### Channel Assignment  
In the above analysis, we have solved the power allocation problem for each CU-D2D pair. Now, we can find the optimal reuse partner for a D2D pair when more than one partner users are available. When a CU shares its uplink or downlink channel with the D2D pair, the optimal CU-D2D pairing problem can be transformed to  
\[\underset{\rho_{mk}^{u},\rho_{mk}^{u}}{\max}\underset{m\in M}{ \sum}\sum_{k\in K}\left(\rho_{mk}^{u}R_{sum}^{up}+\rho_{mk}^{u}R_{sum}^{down}\right) \tag{40}\] \[\text{s.t.}\sum_{m\in M}\rho_{mk}^{u}\leq 1,\sum_{m\in M}\rho_{mk}^{d }\leq 1,\forall k\in K\] (41) \[\rho_{mk}^{u},\rho_{mk}^{d}\in\{0,1\},\forall m\in M,\forall k\in K\] (42) \[\left(\sum_{m\in M}\rho_{mk}^{u}\right)\left(\sum_{m\in M}\rho_{ mk}^{u}\right)=0,\forall k\in K \tag{43}\]  
where \(R_{sum}^{up}\) is the total system capacity of CU and D2D pair in uplink phase, and \(R_{sum}^{down}\) is the total system capacity of CU and D2D pair in downlink phase.  
From the above Equation (40), it is known that the optimal CU-D2D pairing problem is a bipartite matching problem. We can build the system capacity matrix \(\mathrm{R}\)  
\[\mathrm{R}=\left[\begin{array}{ccccccccc}R_{1,1}^{u}&\cdots&R_{1,m}^{u}& \cdots&R_{1,M}^{u}&R_{1,M+1}^{d}&\cdots&R_{1,M+m}^{d}&\cdots&R_{1,2M}^{d}\\ \vdots&&\vdots&&\vdots&\vdots&&\vdots&&\vdots\\ R_{k,1}^{u}&\cdots&R_{k,m}^{u}&\cdots&R_{k,M}^{u}&R_{k,M+1}^{d}&\cdots&R_{k,M+ m}^{d}&\cdots&R_{1,2M}^{d}\\ \vdots&&\vdots&&\vdots&\vdots&&\vdots&&\vdots\\ R_{K,1}^{u}&\cdots&R_{K,m}^{u}&\cdots&R_{K,M}^{u}&R_{K,M+1}^{d}&\cdots&R_{K,M+ m}^{d}&\cdots&R_{1,2M}^{d}\end{array}\right] \tag{44}\]  
The traditional Hungarian algorithm can only be used to solve the uplink or downlink channel allocation problem, it cannot be applied to the JUAD channel allocation problem. Thus, a novel Hungarian algorithm is developed for the JUAD channel allocation scheme.  
Step 1: Use the big-value matrix minus the system capacity matrix, in which the maximization problem can be solved by Hungarian algorithm;  
Step 2: The Hungarian algorithm is used for channel allocation at the first time, in which the cost matrix is the new matrix obtained in Step 1. However, allocating resources based on the Hungarian algorithm results in some channel collisions, due to the restriction in Equation (43) that a D2D pair cannot reuse both the uplink and downlink channels. Hence, we use conflicting channels and corresponding D2D pairs to form a new capacity matrix;  
Step 3: We proceed to allocate resources using the Hungarian algorithm until the number of conflicting channels is 0.  
The detailed steps of the improved Hungarian algorithm are summarized in Algorithm 1.  
```
1:\(M\): The set of CU users \(K\): The set of D2D pairs
2:Using the Power allocation scheme to derive the optimal transmission power
3:for\(k=1:K\)
4:for\(m=1:M\)
5: calculate \(\Re\)
6:end
7:end
8:The big-value matrix \(R\_temp=100*ones(K,M)\)
9:\([assignmentSum,\text{ cost}]=Hungarian(RR\_temp-\Re)\)
10:Returns the assigned channel value using the Hungarian algorithm function
11:\([sumVal1]=sum(\Re,C)\) system capacity after returning the 1 st Hungarian algorithm
12:\(any(assignmentSum(1)\pm 10==assignmentSum(1:M))\) store the conflicting channel
13:if\(sum(A)\sim=0\) the 1 st Hungarian algorithm did not allocate all channels
14:\(\Re\_new=\Re(A,setdiff(D,C))\)
15:\([assignmentSum\_new,\text{ cost}\_new]=\text{Hungarian}(RR\_temp\_new-\Re \_new)\)
16:\([sumVal2]=sum(\Re\_new,assignmentSum\_new)\)
17:\(sumVal=sumVal1+sumVal2\)
18:else
19:\(sumVal=sumVal1\)
20:end
21:Allocate resources using the Hungarian algorithm until the number of conflicting channels is 0
22:The total system capacity is calculated by using the sum function
23:Complete joint uplink and downlink Channel allocation
```  
**Algorithm 1**Optimal Resource Hungarian Algorithm