## 5 Matching-Iteration-Based Joint User Access and Resource Allocation Algorithm
### Matching-Based User Access and Resource Allocation Algorithm  
Gale-Shapley (GS) algorithm was first proposed by [PERSON] and [PERSON] to solve two types of problems, i.e., college admissions and marriage stability. The principle of GS algorithm is elaborated as follows. Members of either side have a preference list for the other members. Based on the preference lists, the active member in the one side will choose the best object to match while the chosen object can either accept the proposal or reject until it get the best proposal according to its preference list. These processes will continue until all members of the two sides reach a stable matching [31]. As thus, a stable matching will always exist in the GS algorithm. According to the matching theory, we given some definitions as follows:  
**Definition 1**. : _A matching \(\varphi\) is a one-to-many correspondence from the set \(\mathcal{C}\cup\mathcal{D}\), if \(\varphi(c_{i})=(d_{k},d_{l})\) and \(\varphi(d_{k})=(c_{i})\), \(\varphi(d_{l})=(c_{i})\), which means the D2D users \(d_{k}\) and \(d_{l}\) reuse the channel with the CU \(c_{i}\)._  
_If \(\varphi(c_{i})=(c_{i})\) or \(\varphi(d_{k})=(d_{k})\), which means CU \(c_{i}\) or D2D user \(d_{k}\) is unpaired. The user can make a request to preferred user according to the preference list then form a matching._  
**Definition 2**. : _A matching \(\varphi\) will remain stable when there does not exist blocking pair._  
_Intuitively speaking, for matching pair \(\varphi(c_{i})=(d_{k},d_{l})\), there is CU \(c_{m}\) or D2D user \(d_{n}\), in which \(c_{i}\) prefers \(d_{n}\) more than \(d_{k}\) and \(d_{l}\), or \(d_{k}\) prefers \(c_{m}\) rather than its match \(c_{i}\), then \(\varphi(c_{i})=(d_{k},d_{l})\) is a blocking pair. If \(c_{i}\) prefers \(d_{n}\) or \(d_{k}\) prefers \(c_{m}\), the matching \(\varphi\) is unstable due to the CU \(c_{i}\) or D2D user \(d_{k}\) would like to match with each other than to interming the matching._  
To solve the proposed problem, we regard the CUs and the D2D users as the two sides to maximize their data rates. Preference lists in GS algorithm will determine the matching project among all members. In our method, preference list for each member is created by sorting the SINR value in descending order which derived from (4) and (6). Graphical expressions of the full-scale preference list establishment and a two-dimension matching derived from the matching-based user access and resource allocation algorithm are shown in Figure 3.  
The matching-based algorithm is shown in Algorithm 1 which mainly includes the following steps:  
1. The data set of the CUs and D2D users are used for input. The data set of the CUs including the transmit power and the distance between D2D users and the BS, and the data set of the D2D users consists of the transmit power, the distance between the D2D transmitter and the BS, the distance between the D2D receiver and the CUs and the distance between the transmitter and receiver of each D2D pair. 2. **Line 1 to 4:** Calculate the SINR of each CU and each D2D user, and create the preference lists by sorting the SINR in descending order. The preference lists are the basis of the proposed iterative algorithm. 3. **Line 5 to 22:** Each D2D user proposes to the most preferred CU. Some CUs may not get any proposal; some CUs may get a proposal from a D2D user; and the others may get several proposals from different D2D users. The CUs which do not get any proposals will maintain unpaired. The CUs which get a proposal from a D2D user will match with the D2D user. On the other hand, the CUs that get multiple proposals from multiple D2D users will be assigned to the most two D2D users with higher precedence in their preference lists. The D2D users which are rejected by their most preferred CU will propose to their sub-preferred CUs. If a D2D user \(D_{i}\) propose to the CU \(C_{k}\) which has paired with a  
Figure 3: Graphical expressions of the stable matching.