# An Opportunistic Network Routing Algorithm Based on Cosine Similarity of Data Packets between Nodes
## 3 An Efficient Forwarding Strategy Based on Cosine Similarity between Nodes
### Algorithm Design  
According to the traversal process of nodes in the sub-network topological structure in the previous section, the routing algorithm based on cosine similarity of data packets between nodes is deduced. The execution process of the algorithm is as follows:  
**Step 1:** Initialize a directed tree T. Each node maintains a set of data packets \(D_{i}\) and a data packet vector \(\mathbf{C}_{i}\). The node S currently transmitting information is taken as the root node of the tree T.  
**Step 2:** Create a set of neighbor nodes for the current node S, denoted as \(U_{s}\). If there is a parent node P of the current node in the tree T, create a set of neighbor nodes for the node P, denoted as \(U_{p}\), and perform the following operation: \(U_{s}=U_{s}-(U_{s}\cap U_{p})\). **Step 3:** Determine whether the number of nodes in the collection \(U_{s}\) is greater than K. If the number of nodes in the set \(U_{s}\) is greater than the access control number K, the nodes in the set are sorted from near to far according to the transmission distance from the current node, and the top K nodes are inserted into the tree T in turn as children of the current node S.  
**Step 4:** Calculate the similarity between the current node s and each child node in turn. First, merge the sets of data packets between node s and child node j, denoted as \(D_{sj}=D_{s}\cup D_{j}\). According to the Equation (6), Equation (7) and the set of data packets after merging, the data packet vectors \(\mathbf{C}^{\prime}_{s}\) and \(\mathbf{C}^{\prime}_{j}\) of node s and node j are recalculated. Then, based on the similarity calculation Equation (11), the similarity between nodes s and j is calculated, which is denoted as \(SIM_{sj}\). **Step 5:** Determine the similarity between the current node and each neighbor node. All nodes with the value of similarity between the lower threshold \(\alpha\) and the upper threshold \(\beta\) are selected as the next hop, and the remaining nodes are deleted from the tree T.  
Figure 5: Transmission paths. If the similarity with all the neighbor nodes is less than the lower threshold, that is \(\max(SIM_{sj})<\alpha\), the neighbor node with the greatest similarity is selected as the next hop, and the remaining child nodes are deleted from the tree T.  
If \(\min(SIM_{sj})>\beta\), the neighbor node with the smallest similarity is selected as the next hop, and the remaining child nodes are deleted from the tree T.  
If \(all(SIM_{sj})\
otin[\alpha,\beta]\). Then, among all the neighbor nodes whose similarity is less than the lower threshold \(\alpha\), the neighbor node with the greatest similarity is selected as the next hop. And in all neighbor nodes whose similarity is greater than the upper threshold \(\beta\), the neighbor node with the smallest similarity is selected as the next hop, and the remaining child nodes are deleted from the tree T.  
**Step 6:** All children of the current node in the tree T are successively regarded as the current node, and steps 2, 3, 4, 5, 6 are repeated until the sub-network topology is accessed. **Step 7:** According to the above process, a directed tree T can be finally obtained. In the light of the structure of the tree T, we can get one or more transmission paths, and the node that is currently sending information is forwarded on these paths through a replication forwarding strategy. The cosSim routing algorithm is shown in Algorithm 1. ```
1.Input: A graph G(V, E), a source S, D\({}_{x}\): Data packet aggregation of node z, C\({}_{z}\): Data packet
2.Output: A or more paths
3.Init:InitTree(T), CurrentNode(i,s); /*set the node s as the current node i*/
4.Set:TsetRootNode(i); U\({}_{i}\); /*A set of neighbor nodes of node i*/
5.If(Node p=T.getParentNode(i)) then
6.U\({}_{p}\);U\({}_{i}\)=U\({}_{i}\)-(U\({}_{i}\cap\)U\({}_{p}\));
7.SorothyDistance(U\({}_{s}\)); U\({}_{s}\).Delete(K); /*Sorting neighbor nodes in U\({}_{i}\) according to the transmission distance,
8.and delete the neighbor nodes after K*/
9.While(!