title: Dijkstra's Algorithm in Javascript Maze Implementation
body: <p>I'm trying to use Dijkstra's Algorithm to path find every position of a maze back to a specific point. </p>
  
  <p>I found a JavaScript version of Dijkstra's Algorithm here: </p>
  
  <p><a href="https://github.com/bm1362/dijkstra-js/blob/master/nodes.js" rel="nofollow">https://github.com/bm1362/dijkstra-js/blob/master/nodes.js</a></p>
  
  <p>(Thank you bm1362!)</p>
  
  <p>The way I've approached creating the maze graph is as follows:</p>
  
  <pre><code>// the maze
  var maze = 
  [[1, 1, 1, 1],
   [1, 0, 0, 0],
   [1, 0, 0, 0],
   [1, 1, 1, 1]];
  
  
  maze2 = [];
  var i = 0;
  var mazeSize = 30;
  for (var x = 0; x &lt; maze[0].length; x++) {
  
      for (var y = 0; y &lt; maze[0].length; y++) {
  
          if (maze[x][y] == 1) {
              nodes.push(new node);
              nodes[i].setPos((x) * (maze[0].length + 100), (y) * ((maze[0].length + 100)))
              nodes[i].tmppos = [x, y];
              nodes[i].id = i;
              maze2[x + ',' + y] = nodes[i];
              i++;
          }
  
      }
  }
  
  //sort nodes in order of x position
  var sortedNodes = quickSort(nodes);
  //var sortedNodes = nodes;
  for (var i = 0; i &lt; sortedNodes.length; i++) {
      sortedNodes[i].number = i;
  
  
      var n1 = sortedNodes[i];
      var up = {}, down = {}, left = {}, right = {};
      var pos = [];
      up.x = n1.tmppos[0];
      up.y = n1.tmppos[1] - 1;
      pos.push(up);
      down.x = n1.tmppos[0];
      down.y = n1.tmppos[1] + 1;
      pos.push(down);
      left.x = n1.tmppos[0] - 1;
      left.y = n1.tmppos[1];
      pos.push(left);
      right.x = n1.tmppos[0] + 1;
      right.y = n1.tmppos[1];
      pos.push(right);
  
      for (var m = 0; m &lt; pos.length; m++) {
  
          if (maze[pos[m].x] != undefined &amp;&amp; maze[pos[m].x][pos[m].y] != undefined &amp;&amp; maze[pos[m].x][pos[m].y] == 1) {
              var n2 = maze2[pos[m].x + ',' + pos[m].y];
              var dist = calcDist(n1, n2);
              sortedNodes[i].addConnection(n2);
              connections.push({
                  a: n1,
                  b: n2,
                  dist: dist
              })
          }
      }
  
  
  }
  </code></pre>
  
  <p>The issue that I'm having is even though all the nodes do seem to have a connection, when the algorithm runs, some of the nodes do not get a parent set. But I've tried many different ways and I still cannot get it to set all the parents.</p>
  
  <p>I've created a JS fiddle here: <a href="http://jsfiddle.net/XcdCr/3/" rel="nofollow">http://jsfiddle.net/XcdCr/3/</a></p>
  
  <p>Any help would be greatly appreciated. </p>