1. Candidate CH 𝑠 𝑖 broadcasts a competition message
 COMPETE_CH( 𝑠 𝑖 . I D , 𝑠 𝑖 . 𝑅 , 𝑠 𝑖 . R E );
2. On receiving a COMPETE_CH from 𝑠 𝑗 ;
3. if ( d i s t ( 𝑠 𝑖 , 𝑠 𝑗 ) < 𝑅 ) then
4.   𝑠 𝑖 . 𝑁 C H 𝑠 𝑖 . 𝑁 C H 𝑠 𝑗 ;
5. 𝑠 𝑖 generates test sequence number 𝑖 and the test task 𝑇 𝑖
6. 𝑠 𝑖 broadcasts a TEST_REQ( 𝑠 𝑖 . I D , 𝑖 , 𝑇 𝑖 );
7. On receiving a TEST_REQ( 𝑠 𝑗 . I D , 𝑗 , 𝑇 𝑗 ) from 𝑠 𝑗 ;
8.   𝑠 𝑖 generates the test result 𝑅 ( 𝑠 𝑖 , 𝑗 ) for task 𝑇 𝑗 ;
9.   𝑠 𝑖 broadcasts the Test_RES( 𝑠 𝑗 , 𝑗 , 𝑅 s i ) to 𝑠 𝑖 . 𝑁 C H ;
10. On receiving a TEST_RES from 𝑠 𝑗 initiated by 𝑠 𝑘
11.  if ( 𝑠 𝑘 = 𝑠 𝑖 ) then
12.  if ( 𝑅 ( 𝑠 𝑗 , 𝑗 ) = 𝑅 ( 𝑠 𝑖 , 𝑗 )) then
13.     𝑠 𝑖 . 𝑁 F F _ C H 𝑠 𝑖 . 𝑁 F F _ C H { 𝑠 𝑗 } ;
14.    𝐞 𝐥 𝐬 𝐞 𝐢 𝐟 𝑠 𝑖 . 𝑁 𝐹 _ C H 𝑠 𝑖 . 𝑁 𝐹 _ C H { 𝑠 𝑗 } ;
15.  else if ( 𝑠 𝑘 𝑠 𝑖 . 𝑁 C H ) then
16.   if ( 𝑅 ( 𝑠 𝑗 , 𝑗 ) = 𝑅 ( 𝑠 𝑖 , 𝑗 )) then
17.      𝑠 𝑖 . 𝑁 F F _ C H 𝑠 𝑖 . 𝑁 F F _ C H { 𝑠 𝑗 } ;
18.    𝐞 𝐥 𝐬 𝐞 𝑠 𝑖 . 𝑁 𝐹 _ C H 𝑠 𝑖 . 𝑁 𝐹 _ C H { 𝑠 𝑗 } ;
19.   𝐞 𝐥 𝐬 𝐞 𝐢 𝐟 ( 𝑠 𝑘 𝑠 𝑖 . 𝑁 C H )
20.   if ( 𝑠 𝑖 has received a TEST_RES from 𝑠 𝑧 𝑠 𝑖 . 𝑁 C H
    and 𝑅 ( 𝑠 𝑗 , 𝑗 ) = 𝑅 ( 𝑠 𝑧 , 𝑗 )) then
21.     𝑠 𝑖 . 𝑁 F F _ C H 𝑠 𝑖 . 𝑁 F F _ C H { 𝑠 𝑗 , 𝑠 𝑧 } ;
22.   else if ( 𝑠 𝑖 received a TEST_RES from 𝑠 𝑧 𝑠 𝑖 . 𝑁 C H
    and 𝑅 ( 𝑠 𝑗 , 𝑗 ) 𝑅 ( 𝑠 𝑧 , 𝑗 ) and 𝑠 𝑧 𝑠 𝑖 . 𝑁 F F _ C H ) then
23.     𝑠 𝑖 . 𝑁 𝐹 _ C H 𝑠 𝑖 . 𝑁 𝐹 _ C H { 𝑠 𝑗 } ;
24.   else store the test response;
25. if ( 𝑠 𝑖 . 𝑁 C H = N U L L ) then 𝑠 𝑖 .state ←finalCH;
26. while ( 𝑠 𝑖 .state = 𝑐 𝑎 𝑛 𝑑 𝑖 𝑑 𝑎 𝑡 𝑒 𝐶 𝐻 )
27.  if ( 𝑠 𝑗 𝑠 𝑖 . 𝑁 F F _ C H and 𝑠 𝑖 . 𝐸 R E > 𝑠 𝑗 . 𝐸 R E ) then
28.    broadcast FINAL_CH( 𝑠 𝑖 . I D );
29.     𝑠 𝑖 . s t a t e ←finalCH;
30.  On receiving a FINAL_CH from 𝑠 𝑗 ;
31.  if ( 𝑠 𝑗 𝑠 𝑖 . 𝑁 C H ) then
32.    broadcast QUIT_ELECT( 𝑠 𝑖 . I D );
33.     𝑠 𝑖 . s t a t e ←RegularNode;
34.  On receiving a QUIT_ELECT from 𝑠 𝑗
35.  if ( 𝑠 𝑗 𝑠 𝑖 . 𝑁 C H ) then
36.    remove 𝑠 𝑗 from 𝑠 𝑖 . 𝑁 C H ;
37. end while
Algorithm 1: The distributed CH election algorithm based on fault diagnosis executed by candidate CH 𝑠 𝑖 .