Advertisement
Tatantyler

Search Algorithms

Nov 26th, 2012
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. function dijkstra(initalNode, nodeList, neighborList, destinationNode)
  2.     local unvisited = {}
  3.     local visited = {}
  4.     local previous = {}
  5.     local currentNode = initalNode
  6.     for i,v in ipairs(nodeList) do
  7.         unvisited[v] = 999999999
  8.     end
  9.     unvisited[initalNode] = 0
  10.     while true do
  11.         local distance = unvisited[currentNode]
  12.         for i,v in pairs(neighborList[currentNode]) do
  13.             if unvisited[i] then
  14.                 if (distance + v) < unvisited[i] then
  15.                     unvisited[i] = distance+v
  16.                     previous[i] = currentNode
  17.                 end
  18.             end
  19.         end
  20.         visited[currentNode] = unvisited[currentNode]
  21.         unvisited[currentNode] = nil
  22.         if destinationNode then
  23.             if visited[destinationNode] then
  24.                 break
  25.             end
  26.         end
  27.         local smallest = {999999999, -1}
  28.         for i,v in pairs(unvisited) do
  29.             if v < smallest[1] then
  30.                 smallest = {v, i}
  31.             end
  32.         end
  33.         if smallest == {999999999, -1} then
  34.             break
  35.         end
  36.         currentNode = smallest[2]
  37.        
  38.     end
  39.     return visited, previous
  40. end
  41.  
  42. function BFS(initalNode, nodeList, neighborList, destinationNode)
  43.     local function splitTable(input) -- splits a table into two, one with indexes, one with values.
  44.         local indexes = {}
  45.         local values = {}
  46.         for i,v in pairs(input) do
  47.             table.insert(indexes, i)
  48.             table.insert(values, v)
  49.         end
  50.         return indexes, values
  51.     end
  52.     local parents = {}
  53.     local queue = {}
  54.     table.insert(queue, initalNode)
  55.     while true do
  56.         if #queue == 0 then
  57.             break
  58.         end
  59.         local node = table.remove(queue, 1)
  60.         if node == destinationNode then
  61.             break
  62.         end
  63.         local nodeNeighbors = splitTable(neighborList[node])
  64.         for i,v in ipairs(nodeNeighbors) do
  65.             table.insert(queue, v)
  66.             parents[v] = node
  67.         end
  68.     end
  69.     return parents
  70. end
  71.  
  72. function generatePath(initalNode, nodeList, neighborList, destinationNode, useBFS)
  73.     local distances = {}
  74.     local links = {}
  75.     if useBFS then
  76.         links = BFS(initalNode, nodeList, neighborList, destinationNode)
  77.     else
  78.         distances, links = dijkstra(initalNode, nodeList, neighborList, destinationNode)
  79.     end
  80.     local stack = {}
  81.     local current = destinationNode
  82.     while true do
  83.         table.insert(stack, 1, current)
  84.         if links[current] then
  85.             current = links[current]
  86.         else
  87.             break
  88.         end
  89.     end
  90.     return stack
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement