# A Novel Routing Scheme for Creating Opportunistic Context-Virtual Networks in IoT Scenarios +
## 5 SACAR Algorithm Description  
### SACAR Base Algorithm  
In this section, the SACAR base algorithm is explained. Let us consider a generic IoT scenario at time \(t\), \(\mathcal{G}=(\mathcal{D},t)\), composed of a set of \(d_{i}^{x_{i}y_{i}}\in\mathcal{D}\) heterogeneous IoT nodes, where the \(i\)th node is located at coordinates \((x_{i},y_{i})\) on a particular geographical area. Moreover, each node \(d_{i}^{x_{i}y_{i}}\in\mathcal{D}\) is a four-tuple of type \(d_{i}^{x_{i}y_{i}}=\{BP_{i},SP_{i},G_{i},S_{i}\}\), where \(BP_{i}\) and \(SP_{i}\) are the _basic_ and _social profiles_ of the node, \(G_{i}=\{g_{1}^{i},g_{2}^{i},...,g_{k}^{i}\}\) represents the set of _goals_, and \(S_{i}=\{s_{1}^{i},s_{2}^{i},...,s_{p}^{i}\}\) is the set of _skills_ the node is able to perform. Note that, although each node \(d_{i}^{x_{i}y_{i}}\in\mathcal{D}\) has a \(BP_{i}\) and a \(SP_{i}\), the two remaining sets are not required of every IoT device, i.e., \(G_{i}=\emptyset\) or \(S_{i}=\emptyset\). The behavior of SACAR is described by Algorithms 1 and 2. In particular, Algorithm 1 describes the steps required to find neighbor nodes with similar interests, while the actions to be performed upon the reception of a message are depicted by Algorithm 2. Therefore, the former case is a proactive process, whilst the latter one is reactive. Four input parameters are required by Algorithm 1: (i) the IoT scenario \(\mathcal{D}\); (ii) the simulation time \(T\); (iii) the frequency \(\omega_{t}\) by which the messages are exchanged between nodes; and (iv) the update time interval \(\delta_{t}\) to execute the functions by the nodes with skills, i.e., with \(S_{i}\
eq\emptyset\). The main procedure is described as follows. Each IoT node \(d_{i}^{x_{i}y_{i}}\) in the scenario, which is located at position \((x_{i},y_{i})\), is continuously listening for incoming messages (Line 4). Every \(\omega_{t}\) seconds, each node  
Figure 2: Example of three Opportunistic Context-Virtual Networks (OCVNs) in a seven-node environment. obtains the list of neighbors \(\mathcal{N}_{i}\) in \(\mathcal{G}\) that are inside its coverage range, i.e., an interaction using short-range communication technologies can be performed (Line 6). The next step to be carried out by a node \(d_{i}^{x_{i}y_{i}}\) is to send basic information about its goals \(G_{i}\) and skills \(S_{i}\) to each neighbor (Lines 7-9). At this point, it is not necessary to send full information for each goal and skill, i.e., pairs \(<id,value>\), since the main purpose is to identify other reachable nodes with the same interests. The last process to be proactively performed by nodes is the execution of their associated skill functions. Clearly, these functions are only executed by those nodes with skills, i.e., with \(S_{i}\
eq\emptyset\). Therefore, every \(\delta_{t}\) seconds, one function \(f(s_{p}^{i})\) is invoked for each skill \(s_{p}^{i}\in S_{i}\) (Lines 11-17). Now that the processes carried out by nodes in a proactive way have been described, it is necessary to explain the actions required upon the reception of a message. For this purpose, Algorithm 2 is proposed. Two input parameters are now required: (i) the node \(d_{i}^{x_{i}y_{i}}\) that received the message; and (ii) the message \(m_{j}\) received from neighbor \(d_{j}^{x_{j}y_{j}}\). The procedure now is simple. The receiving node is able to extract the ids of the goals \(G_{j}\) and skills \(S_{j}\), as well as the id \(d_{j}\) of the node (e.g., hardware address) that created the message (Line 1). If there is a match between the interests of both nodes (i.e., between both pairs of goals and skills), the full information of matching goals/skills is now sent to the neighbor identified by \(d_{j}\) by means of pairs of type \(<id,value>\) (Lines 2-4).