# A Biologically Inspired Self-Organizing Underwater Sensor Network
## 5 Forwarding Guide
### Core Algorithm for Forwarding Guide  
The forwarding guide can be described with Algorithm 4. The output of the algorithm is the content that will be packed into the communication packet sent by the current slot as:  
\[(id_{self},c_{self},id_{next},S_{msg})\]  
The inputs of the algorithm include communication table \(L_{f}\), which is provided by the slot distributor, and the key content contained in the message packet received as  
\[(id_{self}^{sender},_{self}^{sender},id_{next}^{sender},_{msg}^{sender})\]  
We initialize the algorithm in Line 1 and 2, where we define a set \(S_{1\text{-}neighbor}\) to save the IDs of the current node's first-order neighbors, and \(S_{cost}=\{c_{i}|i_{1\text{-}neighbors}\}\) to save the costs for each neighbor to send a message to the base station. Initially, the two sets are all empty. Then, the while loop in Lines 3-27 defines the current node's behavior in relation to relaying messages in each slot. Basically, the behavior is composed of three parts, where Line 4 and Lines 12-14 maintain the neighbors, Lines 5-9 and Lines 15-17 deal with the message content to be sent, and Lines 18-24 search for the best path to relay the message packet. ```
0: Communication table \(L_{f}\), \((id_{self}^{sender},c_{self}^{sender},id_{next}^{sender},S_{msg}^{sender})\) contained in communication packet received
0:\((id_{self},c_{self},id_{next},S_{msg})\), as information that should be contained in the message packet to send
1:\(S_{1\_neighbor}\leftarrow\phi\), \(S_{cost}\leftarrow\phi\)
2:\(id_{next}=-1\), \(S_{msg}\leftarrow\phi_{cself}\gets 0\) if current node is base station and \(c_{self}\leftarrow\infty\) otherwise
3:while New slot with address=\(id_{slot}\)do
4: Update \(S_{1\_neighbor}\) and \(S_{cost}\) by deleting neighbors that fail to speak for a whole frame
5:if\(L_{f}(id_{slot})\)=1 then
6: Send message packet containing \((id_{self},c_{self},id_{next},S_{msg})\)
7:if\(id_{next}\
eq-1\)then
8:\(S_{msg}\leftarrow\phi\)
9:endif
10:else
11:if receive message packet successfully then
12: message packet contains \((id_{self}^{sender},c_{self}^{sender},id_{next}^{sender},S_{msg}^{sender})\)
13:\(S_{1\_neighbor}\gets S_{1\_neighbor}\cup id_{self}^{sender}\)
14: Update \(S_{cost}\) with \(c_{self}^{sender}\)
15:if\(id_{next}^{sender}=id_{self}\)then
16:\(S_{msg}\gets S_{msg}\cup S_{msg}^{sender}\)
17:endif
18: Calculate expected wait with

\[t_{exp}\leftarrow\frac{t_{slot}}{N}\sum_{i=1}^{N}n_{slotsWait}^{i}\]
19:for\(i\in S_{1\_neighbor}\)do
20:\(t_{cost}^{i}\gets t_{exp}+c(i),c(i)\in S_{cost}\)
21:if\(t_{cost}^{i}<c_{self}\)then
22:\(c_{self}\gets t_{cost}^{i}id_{next}\gets i\)
23:endif
24:endfor
25:endif
26:endif
27:endwhile
```  
**Algorithm 4** Forwarding Guide  
At the beginning of a new slot in Line 3, the current node deletes the lost neighbor. As in Section 4, one node should speak at least once during a frame, so if a neighbor fails to speak for a whole frame, it is lost and will be deleted. Then, the current node checks if it should speak in Line 5 and then broadcasts a message packet containing self ID \(id_{self}\), the current cost to relay the message packet \(c_{self}\), its successor node to relay the message packet \(id_{next}\), and all data needing to be sent \(S_{msg}\). If \(id_{next}=-1\), the current node has not established a path to the base station, and no successor exists and \(S_{msg}\) cannot be relayed. Therefore, the node will clear its storage. Otherwise, \(S_{msg}\) will be handed over to another node, so \(S_{msg}\) will be set empty. On receiving a message in Line 11, the node updates the information about its neighbor by either adding new information or by replacing old information with new information, as in Lines 13-14. If the current node is the successor of the sender, then \(S_{msg}^{sender}\) is stored and awaiting being forwarded, as in Lines 15-17. Then, in Lines 18-24, the current node tries to update its successor, finding a better way to relay the message.