Advertisement
Tatantyler

BFS v2 (use with CRF v2)

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