## 4 Centralized and Distributed Algorithms
### Distributed Has\({}^{4}\)
```
while\(\exists N_{p}\in S_{C}\)do  Set timers; ifChangeFlag\(=true\)then  Receiving self-elect-inquiry messages \(\{M_{i}\}\);  On triggering timer\({}_{1}\); if\(M_{i}.lif\:time>\).lif\(\forall M_{i}\in\{M_{i}\}\)OR\(\{M_{i}\}=\emptyset\)then  Broadcast a self-elect-affirm message;  ChangeFlag\(\leftarrow\) false;  end if else \(U\gets U\bigcup\{M_{i}\}\);  Descending sort\(U\);  Choose the first nodes from \(U\)\(u\) as root;  Broadcast the root-inquiry message ;  Remove \(u\) from \(U\);  Change itself to \(S_{C}\);  end if  end for  Receiving root-inquiry message;  On triggering timer\({}_{l+2}\);  Solve \(x_{ij}^{\pi\prime}(t)\) to maximum the leaf number;  Broadcast the decision message;  On triggering timer\({}_{2l+2}\);  Broadcast the channel-occupation message;  Receiving channel-occupation message;  Update available channel;  Receiving channel-collision messages;  On triggering timer\({}_{3l+3}\);  Update available channel and leaf nodes;  end for
```  
**Algorithm 4**Distributed HAS\({}^{4}\)\(S_{U}\). ```
denote \(s\) to be the hops to root; while\(\exists N_{p}\in S_{C}\)do  Receiving root-inquiry messages;  On triggering timer\({}_{2+l-s}\);  Transmit root-inquiry messages to its father node;  Receiving the decision broadcast;  On triggering timer\({}_{2+l+s}\); ifinvolvedthen  Forward the decision to its child;  update available channel;  On triggering timer\({}_{2+2l}\);  Broadcast a channel-occupation message;  end if  Receiving channel-occupation messages;  On triggering timer\({}_{3+2l}\);  Update available channel;  Receiving channel-collision messages;  On triggering timer\({}_{3L+3-s}\);  Transmit channel-collision messages to its father node;  end if
```  
**Algorithm 5**Distributed HAS\({}^{4}\)\(S_{U}\). ```
denote \(s\) to be the hops to root; while\(\exists N_{p}\in S_{C}\)do  Receiving root-inquiry messages;  On triggering timer\({}_{2+l-s}\);  Transmit root-inquiry messages to its father node;  Receiving the decision broadcast;  On triggering timer\({}_{2+l+s}\); ifinvolvedthen  Forward the decision to its child;  update available channel;  On triggering timer\({}_{2+2l}\);  Broadcast a channel-occupation message;  end if  Receiving channel-occupation messages;  On triggering timer\({}_{3+2l}\);  Update available channel;  Receiving channel-collision messages;  On triggering timer\({}_{3L+3-s}\);  Transmit channel-collision messages to its father node;  end if
```  
**Algorithm 6**Distributed HAS\({}^{4}\)\(S_{U}\). ```
denote \(s\) to be the hops to root; while\(\exists N_{p}\in S_{C}\)do  Receiving root-inquiry messages;  On triggering timer\({}_{2+l-s}\);  Transmit root-inquiry messages to its father node;  Receiving the decision broadcast;  On triggering timer\({}_{2+l+s}\); ifinvolvedthen  Forward the decision to its child;  update available channel;  On triggering timer\({}_{2+2l}\);  Broadcast a channel-occupation message;  end if  Receiving channel-occupation messages;  On triggering timer\({}_{3+2l}\);  Update available channel;  Receiving channel-collision messages;  On triggering timer\({}_{3L+3-s}\);  Transmit channel-collision messages to its father node;  end if
```  
**Algorithm 7**Distributed HAS\({}^{4}\)\(S_{U}\). The execution of distributed HAS4 on \(S_{A}\) nodes is as shown in Algorithm 3. For each \(S_{A}\) node, it receives root-inquire messages, takes some appropriate \(S_{C}\) nodes into its group according to all the constraints in Section 3.2, and broadcasts this decision. Once an \(S_{A}\) node receives a channel-occupation message or channel-collision message, it will update the available channels. The execution of distributed HAS4 on \(S_{U}\) nodes is as shown in Algorithm 4. In distributed HAS4, \(S_{U}\) nodes are in charge of forwarding the messages between \(S_{A}\) and \(S_{C}\). Since in root select, root affirm and conflict handler stages, messages from \(S_{C}\) (\(S_{A}\)) should be forwarded hop-by-hop to \(S_{A}\) (\(S_{C}\)) sensors, while, in the other stages, sensors just independently broadcast messages.