title: How to solve the nearest neighbor heuristic
body: <p>I am trying to solve the nearest neighbor heuristic for the tsp on a small example with a 2d-array in C++. This is my code by now. I want to create tours with the start Nodes from 0 to 6, so I can compare them afterwards (which one has the lowest distance). 
  However, after debugging I kind of know where my problem is, but I cannot understand why it is not working. My <code>visited[i+1]</code> is not saving the smallestIndex on the next Index. </p>
  
  <pre><code>void getNearestNeighbor(float distanceMatrix[][6], int startNode, int visited[6]){
  
      bool unvisited[6]{ 1,1,1,1,1,1 };
      visited[0] = { startNode };
      unvisited[startNode] = false;
      for (int i = 0; i &lt; 5; i++)
      {
          float smallestDistance = 100.0;
          int smallestIndex =visited[i];
  
          for (int j = 0; j &lt; 6; j++)
          {
  
              if (visited[i] != j &amp;&amp; unvisited[j] == true)
              {
                  if (smallestDistance &gt; distanceMatrix[visited[i]][j])
                  {
                      smallestDistance = distanceMatrix[visited[i]][j];
                      smallestIndex = j;
                  }
              }
          }
  
          visited[i+1] = smallestIndex;
          unvisited[smallestIndex] = false;
          //cout &lt;&lt; visited[i] &lt;&lt; endl;
      }
  }   
  
  int main()
  {
      int visited[6]; 
      float distanceMatrix[6][6]{
          { 0, 17, 11, 5.39, 10.8, 8.6 },
          { 17, 0, 10.2, 12.5, 19.1, 25.6 },
          { 11, 10.2, 0, 9.85, 18.8, 19 },
          { 5.39, 12.5, 9.85, 0, 8.94, 13.5 },
          { 10.8, 19.1, 18.8, 8.94, 0, 14 },
          { 8.6, 25.6, 19, 13.5, 14, 0 } };
  
  
      for (int srtNode = 0; srtNode &lt; 6; srtNode++)
      {
          getNearestNeighbor(distanceMatrix, srtNode, visited);
          cout &lt;&lt; visited[srtNode];
          //calcDistance(distanceMatrix, visited);
      }
  
      system("pause");
  
      return EXIT_SUCCESS;
  }
  </code></pre>