title: Dijkstra’s Algorithm problems with backtracing vertexes
body: <p>I'm trying to implement backtracing of the vertices visited for the lowest costing route to a vertex. I'm getting incorrect results and don't understand why. The only correct output was the last one in the image. What is causing the incorrect</p>
  
  <p>Note: driverMap is a 2D 14x14 integer vector that holds the distances it takes to get to each vertex and path is a int array to hold the past path vertex taken. The beginning is a section of the code from my Main function to help out.</p>
  
  <p>It's different as my previous questions were different, this time i'm asking for help on my current output against expected when attempting to backtrace.</p>
  
  <p><img src="https://i.stack.imgur.com/xOYOa.png" alt="enter image description here"></p>
  
  <pre><code>for(int i = 0; i &lt; allCars.size(); i++) //allCars.size()
  
  {
  
      int startInt = allCars[i].getStart(), loopEnder = 0;
  
  
  
      for(int k = 0; k &lt; driverMap.size(); k++)
      {
          path[k] = 0;
          Distances[k] = 0;
      }
  
  
      for(int j = 0; j &lt; driverMap.size(); j++) 
  
      {
  
          Distances[j] = driverMap[startInt][j];
  
      }
  
      cout &lt;&lt; "\nSTART INTERSECTION: '" &lt;&lt; startInt &lt;&lt; "' END INTERSECTION: '" &lt;&lt; allCars[i].getEnd() &lt;&lt; "'" &lt;&lt;  endl;
  
  
      Dijkstra(driverMap, Distances, path, startInt);
  
      int endInt = allCars[i].getEnd(), atInt = path[endInt];
  
      cout &lt;&lt; "END = " &lt;&lt; endInt;
  
      //allCars[i].addPath(endInt);
      do
      {
          cout &lt;&lt; "AT = " &lt;&lt; atInt;
          allCars[i].addPath(atInt);
          atInt = path[atInt];
          loopEnder++;
      }while(atInt != endInt &amp;&amp; loopEnder &lt; 5);
      cout &lt;&lt; endl;
  
      //allCars[i].addPath(startInt);
  
  
      allCars[i].displayCar();
  
  }
  
  void Dijkstra(const vector&lt; vector&lt;int&gt; &gt; &amp; driverMap, int Distances[], int path[], int startInt) 
  {
      int Intersections[driverMap.size()];
      for(int a = 0; a &lt; driverMap.size(); a++)
      {
          Intersections[a] = a;
      }
      Intersections[startInt] = -1;
  
      for(int l = 0; l &lt; driverMap.size(); l++)
      {
          int minValue = 99999;
  
          int minNode = 0;
  
  
  
  
          for (int i = 0; i &lt; driverMap.size(); i++) 
  
          {
  
              if(Intersections[i] == -1) 
  
              {
  
                  continue;
  
              }
  
              if(Distances[i] &gt; 0 &amp;&amp; Distances[i] &lt; minValue)
  
              {
  
                  minValue = Distances[i];
  
                  minNode = i;
  
              }
  
          }
  
  
          Intersections[minNode] = -1;
  
  
  
  
  
          for(int i = 0; i &lt; driverMap.size(); i++) 
  
          {
  
              if(driverMap[minNode][i] &lt; 0)
  
              {
  
                  continue;
  
              }
  
              if(Distances[i] &lt; 0) 
  
              {
  
                  Distances[i] = minValue + driverMap[minNode][i];
                  path[i] = minNode;
  
                  continue;
  
              }
  
              if((Distances[minNode] + driverMap[minNode][i]) &lt; Distances[i]) 
  
              {
  
                  Distances[i] = minValue + driverMap[minNode][i];
                  path[i] = minNode;
  
              }
  
          }
      }
  
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2iDDS.png" alt="Output result"></p>