# A Power Planning Algorithm Based on RPL for AMI Wireless Sensor Networks
## 4 DODAG-Oriented Power Planning Algorithm
```
1 BuildDODAG (\(n\));
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
4 Set the power of nodes in **U** as \(\min(\textbf{W})\) ;
5 Set the jumps count of all nodes \(i\) in **U** as \(j_{i}=0\);
6\(A\leftarrow\text{InitDODAG (n)}\) ;
7 SetPowers (\(root\), \(A\));
8\(\textbf{C}\leftarrow A\cup\{root\}\) and \(\textbf{U}\leftarrow\textbf{U}\setminus A\);
9 repeat
10\(\textbf{J}\leftarrow\varnothing\);
11 while\(\textbf{U}\
eq\varnothing\)do
12\(i\leftarrow\)node in **U** nearest to _root_ (with respect to the Euclidean distance);
13\(\textbf{U}\leftarrow\textbf{U}\setminus\{i\}\);
14\(B\leftarrow\)SelectParents (\(i\), **C**);
15 if\(|B|=k\vee(B\
eq\varnothing\wedge j_{i}=\alpha)\lor root\in B\)then
16 SetPowers (\(i,B\));
17\(\textbf{C}\leftarrow\textbf{C}\cup\{i\}\);
18 elseif\(j_{i}<\alpha\)then
19\(j_{i}=j_{i}+1\);
20\(\textbf{J}\leftarrow\textbf{J}\cup\{i\}\);
21 else
22\(i\)node \(i\) can not be connected;
23
24 end if
25
26 end if
27\(\textbf{U}\leftarrow\textbf{J}\);
28 until\(\textbf{U}=\varnothing\);
29
30 end while
```  
**Algorithm 1**Power Planning Algorithm  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
4\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
5\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
6\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
7 end if
8
9 end for
10 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
11 end for
```  
**Algorithm 2**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
4\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
5\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
6\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
7 end if
8
9 end for
10 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
11 end for
```  
**Algorithm 3**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
5\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
6\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
7\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
8 end if
9
10 end for
11 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
12 end for
```  
**Algorithm 4**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
5\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
6\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
7\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
8 end if
9
10 end for
11 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
12 end for
```  
**Algorithm 5**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
7\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
8\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
9 end if
10
11 end for
12 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
13 end for
```  
**Algorithm 6**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
8\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
9\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
10 end if
11
12 end for
13 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
14 end for
```  
**Algorithm 7**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
8\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
9\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
11 end if
12
13 end for
14 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
15 end for
```  
**Algorithm 8**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
9\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
10\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
11 end if
12
13 end for
14 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
15 end for
```  
**Algorithm 8**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
16\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
17\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
18 end if
19
20 end for
21 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
22 end for
```  
**Algorithm 9**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
19\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
20\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
21 end if
22
23 end for
24
25 end for
26 return\(A\) /*cardinalityof\(A\)maybe\(\leq n\) */
27 end for
```  
**Algorithm 10**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
28\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
30\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
31 end if
32
33 end for
34
35 end for
36
37 end for
```  
**Algorithm 11**Select Nodes to Connect Directly to the Root  
```
1 InitDODAG (\(n\))
2/*\(n\)numberofnodestoconnecttoroot*/
3 begin
6\(A\leftarrow\varnothing\)  Define a disk with center at \(root\) and radius \(d_{\max(\textbf{W})}^{Q}\) Divide the disk into \(n\) equal sectors for each sectordo
38\(D\leftarrow\)nodes in the sector if\(D\
eq\varnothing\)then
39\(i\leftarrow\)node in \(D\) nearest to \(root\) (with respect to the Euclidean distance) \(A\gets A\cup\{i\}\)
40 end if
71
8 end for
82
90 end for
```  
**Algorithm 12**Select Nodes to Connect Directly to the Root The SelectParents procedure (Algorithm 3) tries to find a suitable parent set for the new node considering the nodes already connected to the DODAG (the set **C**).