Advertisement
Tatantyler

Breadth-First Search Routing Function (for use with CRF)

Jan 30th, 2013
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. local routing_scheme_args = {...}
  2.  
  3. local initalNode = os.computerID()
  4. local destinationNode = routing_scheme_args[1]
  5. local nodeGraph = routing_scheme_args[2]
  6. local upperParams = routing_scheme_args[3]
  7.  
  8. if upperParams["r"] or upperParams["debug_routing_scheme"] then
  9.     print("[ROUTEGEN_BFS] Entering Breadth-First Search routing function...")
  10.     print("[ROUTEGEN_BFS] Routing from "..initalNode.." to "..destinationNode..".")
  11. end
  12.  
  13. local routes = {}
  14. local searchQueue = {}
  15. local searched = {} -- to make sure we don't end up recursing
  16. local enqueue = function(value) table.insert(searchQueue, 1, value) end
  17. local dequeue = function() return table.remove(searchQueue) end
  18.  
  19. -- BFS:
  20. enqueue(initalNode)
  21. while true do
  22.     if #searchQueue == 0 then
  23.         if upperParams["r"] or upperParams["debug_routing_scheme"] then
  24.             print("[ROUTEGEN_BFS] Function exiting early: exhausted node list.")
  25.         end
  26.         return nil -- couldn't find a route
  27.     end
  28.     local currentNode = dequeue()
  29.     if upperParams["r"] or upperParams["debug_routing_scheme"] then
  30.         print("[ROUTEGEN_BFS] Examining node: "..currentNode)
  31.     end
  32.     if currentNode == destinationNode then
  33.         break -- got a route
  34.     end
  35.     if upperParams["r"] or upperParams["debug_routing_scheme"] then
  36.         print("[ROUTEGEN_BFS] Adding "..currentNode.."\'s neighbors...")
  37.     end
  38.     for i=1, #nodeGraph[currentNode] do
  39.         if not searched[ nodeGraph[currentNode][i] ] then
  40.             enqueue( nodeGraph[currentNode][i] )
  41.             routes[ nodeGraph[currentNode][i] ] = currentNode
  42.             searched[ nodeGraph[currentNode][i] ] = true
  43.         end
  44.     end
  45. end
  46.  
  47. if upperParams["r"] or upperParams["debug_routing_scheme"] then
  48.     print("[ROUTEGEN_BFS] Route found to "..destinationNode..".")
  49.     print("[ROUTEGEN_BFS] Constructing route...")
  50. end
  51.  
  52. local links = {}
  53. local stack = {}
  54. local current = destinationNode
  55. while true do
  56.     if upperParams["r"] or upperParams["debug_routing_scheme"] then
  57.         print("Adding node: "..current.." to final route.")
  58.     end
  59.     table.insert(stack, 1, current) -- Insert the current node at the beginning of the stack. (it's a reverse stack)
  60.     if current == os.computerID() then -- If we've reached the end (or beginning; remember, it's in reverse)...
  61.         break -- ...then just exit.
  62.     elseif routes[current] then -- If there's another node to go to...
  63.         if upperParams["r"] or upperParams["debug_routing_scheme"] then
  64.             print("Moving to node "..routes[current]..".")
  65.         end
  66.         current = routes[current] -- ... then go to it.
  67.     else -- If for some reason this node doesn't have a parent...
  68.         break -- ...then just exit.
  69.     end
  70. end
  71. if upperParams["r"] or upperParams["debug_routing_scheme"] then
  72.     print("[ROUTEGEN_BFS] Final route generation complete, returning...")
  73. end
  74. return stack -- Just in case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement