# Research on Secure Community Opportunity Network Based on Trust Model
## 3 Trust Community Routing Algorithm
### Routing and forwarding Strategy  
Nodes within the security community exhibit a high level of trust. To enhance the efficiency of message transmission while ensuring the security of node communication, it becomes imperative to designate nodes with stronger forwarding capabilities from the security community as the subsequent relay nodes. This study opts for nodes with elevated centrality, determined through both global and local centrality concepts, as the subsequent relay nodes. Definition 4: Node centrality. This paper utilizes the node's neighbor count to compute its global centrality (Algorithm 1). A higher global centrality implies increased likelihood of interaction with other nodes, thereby providing more opportunities for message forwarding. Local centrality, on the other hand, is determined by the number of neighbor nodes belonging to the local community. Greater local centrality indicates a heightened chance of encountering nodes within the local community,facilitating easier message forwarding to the destination node within that community. The calculation formula for node centrality is articulated as follows:  
\[\left\{ \begin{array}{l}GloCenter_{A}=\frac{\sum{Node_{eni}}}{\sum{Node_{ali}}} \\ LocCenter_{A}=\frac{\sum{Node_{eni}}}{\sum{Node_{eni}}}\end{array} \right. \tag{8}\]  
where \(GloCenter_{A}\) represents the global centrality of node \(A\), \(LocCenter_{A}\) denotes the local centrality of node \(A\), \(Node_{n}ei\) signifies the neighboring node of node \(A\), \(Node_{n}l\) encompasses all nodes in the social opportunistic network, and \(Node_{n}ei\) denotes the neighboring nodes belonging to the local community of node \(A\). ```
1 Input: Source_node S, Neighbor_node list V
2 Output: S.local
3 foreachNode \(N\in V\)do
4\(Trust_{(S,N)}=adTrust_{(S,N)}+(1-\alpha)iTrust_{(S,N)}\)
5
6 end for
7 Compute Trust_Threshold as \(Threshold=\frac{1}{m}\sum_{j\in\{S\text{\tiny{commedications}}\}}Trust_{(S,j)}\)
8 forall\(LN\in S\).localdo
9\(Trust_{(S,LN)}=Trust_{(S,LN)old}\times(1-\frac{Time_{n}-Time_{l}}{Time_{n}})\gamma\)
10 if\(Trust_{(S,LN)}\) < Trust_Thresholdthen
11 S.local removes LN ;
12
13 end if
14
15 end for
16
17 forall\(FN\in S\).familiardo
18\(Trust_{(S,FN)}=Trust_{(S,FN)old}\times(1-\frac{Time_{n}-Time_{l}}{Time_{n}})\gamma\)
19 if\(Trust_{(S,FN)}\) < Trust_Thresholdthen
20 S.familiar removes FN ;
21
22 end if
23
24 end for
25 foreachNode \(N\in V\)do
26 if\(Trust_{(S,N)}\) >= Trust_Thresholdthen
27 S.familiar addes N ;
28
29 forall\(NL\in N\).localdo
30 Compute Trust\({}_{(S,NL)}\) as Trust\({}_{(S,NL)}=Trust_{(S,N)}\times Trust_{(N,NL)}\)
31 if\(Trust_{(S,NL)}\) >= Trust_Thresholdthen
32 S.local addes NL ;
33
34 end if
35 end for
36
37 end for
38 Output: S.local
```  
**Algorithm 1**Community division  
However, over-reliance on high-centrality nodes for message transmission may lead to node buffer overflow and message transmission failure. Simultaneously, due to the small size and low-energy characteristics of wireless devices, depending excessively on high-centrality nodes for message transmission may rapidly deplete node energy, eventually resulting in node failure and network collapse. Therefore, in addition to considering node centrality, this paper comprehensively incorporates node residual energy and node residual cache to select the next hop relay node. The selection formula for the next hop relay node is as follows:\[Score_{rel} =\alpha{GloCenter_{A}}+\beta\frac{Node_{resb}}{Node_{buf}}+\gamma\frac{ Node_{resn}}{Node_{ene}},{Message_{des}}\
otin\mathit{Relay}_{local} \tag{9}\] \[Score_{rel} =\alpha{LocCenter_{A}}+\beta\frac{Node_{resb}}{Node_{buf}}+\gamma\frac{ Node_{resn}}{Node_{ene}},{Message_{des}}\in\mathit{Relay}_{local} \tag{10}\]  
where \(Score_{rel}\) represents the score of the relay node. A higher score indicates a greater likelihood for the node to be selected as the next-hop relay node. \(GloCenter_{A}\) denotes the global centrality, while \(LocCenter_{A}\) signifies the local centrality of node \(A\).