title: Why is this lua dijkstra's algorithm not working in some cases?
body: <p>Im using Dijkstra algorythm code from this site: <a href="https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Lua" rel="nofollow noreferrer">https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Lua</a></p>
  
  <p>Unfortunately It doesnt work for current edges table.</p>
  
  <p>I have determined that the problem disappears when I delete the connection from 35 -> 36, but It doesnt solve the problem.</p>
  
  <pre class="lang-lua prettyprint-override"><code>-- Graph definition
  local edges = {
    [34] = {[35] = 1,[37] = 1,},
    [35] = {[34] = 1,[36] = 1,[46] = 1,},
    [36] = {[35] = 1,[37] = 1,},
    [37] = {[34] = 1,[36] = 1,},
    [38] = {[46] = 1,},
    [46] = {[35] = 1,[38] = 1,},
  }
  
  -- Fill in paths in the opposite direction to the stated edges
  function complete (graph)
      for node, edges in pairs(graph) do
          for edge, distance in pairs(edges) do
              if not graph[edge] then graph[edge] = {} end
              graph[edge][node] = distance
          end
      end
  end
  
  -- Create path string from table of previous nodes
  function follow (trail, destination)
      local path, nextStep = destination, trail[destination]
      while nextStep do
          path = nextStep .. " " .. path
          nextStep = trail[nextStep]
      end
      return path
  end
  
  -- Find the shortest path between the current and destination nodes
  function dijkstra (graph, current, destination, directed)
      if not directed then complete(graph) end
      local unvisited, distanceTo, trail = {}, {}, {}
      local nearest, nextNode, tentative
      for node, edgeDists in pairs(graph) do
          if node == current then
              distanceTo[node] = 0
              trail[current] = false
          else
              distanceTo[node] = math.huge
              unvisited[node] = true
          end
      end
      repeat
          nearest = math.huge
          for neighbour, pathDist in pairs(graph[current]) do
              if unvisited[neighbour] then
                  tentative = distanceTo[current] + pathDist
                  if tentative &lt; distanceTo[neighbour] then
                      distanceTo[neighbour] = tentative
                      trail[neighbour] = current
                  end
                  if tentative &lt; nearest then
                      nearest = tentative
                      nextNode = neighbour
                  end
              end
          end
          unvisited[current] = false
          current = nextNode
      until unvisited[destination] == false or nearest == math.huge
      return distanceTo[destination], follow(trail, destination)
  end
  
  -- Main procedure
  print("Directed:", dijkstra(edges, 34, 38, true))
  print("Undirected:", dijkstra(edges, 34, 38, false))
  </code></pre>
  
  <p>I recieve the output of <code>inf, 38</code> with current egdes table content but when I delete the connection between 35 -> 36 it gives an good output - <code>3, 34 35 46 38</code></p>
  
  <p>For easier understand im uploading the graphic representation of edges table: <a href="https://i.imgur.com/FFF22C1.png" rel="nofollow noreferrer">https://i.imgur.com/FFF22C1.png</a></p>
  
  <p>As you can see the route is corrent when we start from 34 -> 35 -> 46 -> 38 but as I sad It works only when connection from 35 to 36 is not existing.</p>
  
  <p><b>Why it is not working in the case showed in my code?</b></p>