title: Dijkstra's algorithm in C++
body: <p>I'm required to implement the Dijkstra's algorithm via ADT graph using the adjacency matrix representation for finding a shortest path by enhancing the pseudo code below using either C/C++ language.</p>
  
  <pre><code>procedure Dijkstra(G, w, r, Parent[0:n-1], Dist)
    for v← 0 to n-1 do
      Dist[v] ← ∞
      InTheTree[v] ← .false.
    endfor
    Parent[r] ←-1
    Dist[r] ←0
    for Stage ←1 to n-1 do 
      Select vertex u that minimises Dist[u] over all u such that InTheTree[u] = .false. 
      InTheTree[u] = .true.                                       // add u to T
      for each vertex v such that uv ∈ E do             // update Dist[v] and 
        if .not. InTheTree[v]  then                // Parent[v] arrays
          if Dist[u] + w(uv) &lt; Dist[v]
            Dist[v] = Dist[u] + w(uv)
            Nearest[v] ←w(uv) 
            Parent[v] ← u
          endif
        endif
      endfor
    endfor
  end Dijkstra
  </code></pre>
  
  <p>Here is my solution of code which is being coded in C++. My lecturer claim that the code does not meet the pseudocode requirements and I'm not sure 
  where it went wrong so can anyone help me to spot what doesn't match between the code and the pseudocode?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;limits.h&gt;
  #define N 9
  
  int minDistance(int dist[], bool sptSet[])
  {
     int min = INT_MAX, min_index;
  
     for (int n = 0; n &lt; N; n++)
       if (sptSet[v] == false &amp;&amp; dist[n] &lt;= min)
           min = dist[n], min_index = n;
  
     return min_index;
  }
  int printSolution(int dist[], int v)
  {
     printf("Vertex   Distance from Source\n");
     for (int i = 0; i &lt; N; i++)
        printf("%d \t\t %d\n", i, dist[i]);
  }
  void dijkstra(int graph[N][N], int src)
  {
       int dist[N];     
  
       bool sptSet[N];                         
  
       for (int i = 0; i &lt; N; i++) {
          dist[i] = INT_MAX;
          sptSet[i] = false;
       }
  
       dist[src] = 0;
  
  
       for (int count = 0; count &lt; N-1; count++)
       {
  
         int u = minDistance(dist, sptSet);
              sptSet[u] = true;
         for (int n = 0; n &lt; N; n++)
           if (!sptSet[n] &amp;&amp; graph[u][n] &amp;&amp; dist[u] != INT_MAX 
                                         &amp;&amp; dist[u]+graph[u][n] &lt; dist[n])
              dist[n] = dist[u] + graph[u][n];
       }
       printSolution(dist, N);
  }
  
  int main()
  {
     int graph[N][N] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
                        {4, 0, 8, 0, 0, 0, 0, 11, 0},
                        {0, 8, 0, 7, 0, 4, 0, 0, 2},
                        {0, 0, 7, 0, 9, 14, 0, 0, 0},
                        {0, 0, 0, 9, 0, 10, 0, 0, 0},
                        {0, 0, 4, 0, 10, 0, 2, 0, 0},
                        {0, 0, 0, 14, 0, 2, 0, 1, 6},
                        {8, 11, 0, 0, 0, 0, 1, 0, 7},
                        {0, 0, 2, 0, 0, 0, 6, 7, 0}
                       };
  
      dijkstra(graph, 0);
  
      return 0;
  }
  </code></pre>