# 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
### Node Definition and Similarity Calculation  
**Definition 1**. : _The data packet set \(D_{i}=\{d_{i1},d_{i2},\cdots,d_{ij}\}\) represents the \(j\) data packets carried by any node \(i\). The data packets carried by the node \(i\) are expressed as a vector \(\mathbf{C}_{i}=(w_{i1},w_{i2},\cdots,w_{ij})\), where \(w_{ij}\) is the weight of the \(j\)-th data packet in the set \(D_{i}\), that is the frequency of the data packet appearing in the node \(i\), and the initial value is 1._  
**Definition 2**. : _The merge operation of the data packet sets of any two nodes \(a\) and \(b\) is denoted as \(D^{\prime}=D_{a}\cup D_{b}\), and the respective data packet vectors are recalculated on the basis of the set \(D^{\prime}\)._  
Figure 2: The topology structure of sub-network. Assume that node a has a total of n data packets. Its set is  
\[D_{a}=\left\{d_{a1},d_{a2},\cdots,d_{ai},\cdots,d_{an}\right\},1\leq i\leq n \tag{1}\]  
The data packets of node a can be expressed as vector \(\mathbf{C}_{a}\):  
\[\mathbf{C}_{a}=\left(w_{a1},w_{a2},\cdots,w_{ai},\cdots,w_{an}\right),\;1\leq i\leq n \tag{2}\]  
Assume that node b has a total of m data packets. Its set is  
\[D_{b}=\left\{d_{b1},d_{b2},\cdots,d_{bj},\cdots,d_{bm}\right\},\;1\leq j\leq m \tag{3}\]  
The data packets of node b can be expressed as vector \(\mathbf{C}_{b}\):  
\[\mathbf{C}_{b}=\left(w_{b1},w_{b2},\cdots,w_{bj},\cdots,w_{bm}\right),\;1\leq j\leq m \tag{4}\]  
The merge operation of the data packet sets of node a and b is recorded as  
\[D_{ab}=D_{a}\cup D_{b}=\left\{d^{\prime}{}_{1},d^{\prime}{}_{2},\cdots,d^{ \prime}{}_{k},\cdots,d^{\prime}{}_{z}\right\},\;1\leq k\leq z,\;\max(m,n)\leq z \leq(m+n) \tag{5}\]  
The data packets vector of node a corresponds to the data packet set after merging is \(\mathbf{C^{\prime}}_{a}\):  
\[\mathbf{C^{\prime}}_{a}=\left(w^{\prime}{}_{a1},w^{\prime}{}_{a2},\cdots,w^{ \prime}{}_{ak},\cdots,w^{\prime}{}_{az}\right),\;1\leq k\leq z \tag{6}\]  
\(w^{\prime}{}_{ak}\) is calculated as follows:  
\[w^{\prime}{}_{ak}=\left\{ \begin{array}{ccc}w_{ai},&if&d^{\prime}{}_{k}=d_{ ai}\\ 0,&if&d^{\prime}{}_{k}\
eq d_{ai}\end{array} \right. \tag{7}\]  
The data packets vector of node b corresponds to the data packet set after merging is \(\mathbf{C^{\prime}}_{b}\):  
\[\mathbf{C^{\prime}}_{b}=\left(w^{\prime}{}_{b1},w^{\prime}{}_{b2},\cdots,w^{ \prime}{}_{bk},\cdots,w^{\prime}{}_{b2}\right),\;1\leq k\leq z \tag{8}\]  
\(w^{\prime}{}_{bk}\) is calculated as follows:  
\[w^{\prime}{}_{bk}=\left\{ \begin{array}{ccc}w_{bj}\;,&if&d^{\prime}{}_{k}=d_ {bj}\\ 0\;,&if&d^{\prime}{}_{k}\
eq d_{bj}\end{array} \right. \tag{9}\]  
**Definition 3**. : _The node similarity \(SIM_{ab}\) indicates the degree of similarity between nodes a and b._  
The cosine similarity between vector \(\mathbf{Q}\) and vector \(\mathbf{D}\) is as follows:  
\[\cos(\mathbf{Q},\mathbf{D})=\frac{\sum\left(\mathbf{Q}T_{i}\times\mathbf{D}T_ {i}\right)}{\sqrt{\sum\left(\mathbf{Q}T_{i}\right)^{2}}\times\sqrt{\sum\left( \mathbf{D}T_{i}\right)^{2}}}\]  
**Definition 4**. : _Access control number K, which is used to control the number of the current node to access its neighbor nodes. When the current node has a large number of neighbor nodes, it may take a long time to attempt to access all the neighbor nodes, which is not conducive to the transmission of information in the network._  
Therefore, when the number of neighbors of a node is larger than the access control number K, the neighbor nodes are sorted according to the transmission distance with the current node, and the first K neighbor nodes with closer transmission distance are preferentially accessed. The access control number K effectively reduces the computation time of the cosSim algorithm. **Definition 5**. : _The lower threshold of node similarity (\(\alpha\)), which is the screening criteria for candidate nodes in the next hop.