Skip to main content
. 2026 Jun 20;26(12):3915. doi: 10.3390/s26123915
Algorithm 4. On-Demand Route Validation and Loop Avoidance
Function RecomputeRoute(Node C)
    If C can directly reach the base station within range Rc2
       Set C.Upstream = BS
       Set C.HOP level = 1
       Set Sup(C) = 1
       Propagate local route-update information to direct downstream child CHs
       Exit
    Else
       C broadcasts a Route-Request using range Rc2
       Initialize CandidateSet = empty
       For each candidate node U within Rc2
           If U is alive
              Initialize CurrentUpstream = U
              Initialize VisitedSet = empty
              Initialize Steps = 0
              Initialize IsValid = false
              While Steps < Dmax
                 If CurrentUpstream = BS
                     Set IsValid = true
                     Break
                 End if
                 If CurrentUpstream is undefined OR CurrentUpstream is not alive
                     Mark U as invalid
                     Break
                 End if
                 If CurrentUpstream is already in VisitedSet
                     Loop detected; mark U as invalid
                     Break
                 End if
                 Add CurrentUpstream to VisitedSet
                 Let NextUpstream = CurrentUpstream.Upstream
                 If NextUpstream is undefined
                     Mark U as invalid
                     Break
                 End if
                 If distance(CurrentUpstream, NextUpstream) > Rc2
                     Mark U as invalid
                     Break
                 End if
                 Set CurrentUpstream = NextUpstream
                 Set Steps = Steps + 1
             End while
             If Steps reaches Dmax before reaching a base station, mark U as invalid.
             If IsValid = true
                 Calculate Score(C, U)
                 Add U to CandidateSet
             End if
           End if
         End for
         If CandidateSet is not empty
             Select Ubest with the maximum score
             Set C.Upstream = Ubest
             Set C.HOP level = Ubest.HOP level + 1
             Set Sup(C) = 1
             Propagate local route-update information to direct downstream child CHs
         Else
             Set C.Upstream = undefined
             Set Sup(C) = 0
             Recovery failed; mark C as Disconnected
         End if
   End if
End Function