Dijkstra's algorithm for finding the shortest path?
<p>So I have been trying to implement the Dijkstra Algorithm to find the shortest path within a graph, but am running into several errors as I go along. First off, here's the actual question and the components I was given with it.</p>

<p>"Write a c++ function to implement Dijkstra's algorithm on a graph. The graph implementation is on Moodle, called IntroToGraphs.zip.The function takes two parameters: starting vertex and destination vertex. Print the shortest path and the shortest distance from the starting vertex to the destination vertex."</p>

<p>void Graph::Dijkstra(string starting, string destination);</p>

<p>The vertex structure has been changed to include "visited", "distance" and "previous".</p>

<pre><code>struct adjVertex{
    vertex *v;
    int weight;
};
struct vertex{
    std::string name;
    bool visited;
    int distance;
    vertex *previous;
    std::vector&lt;adjVertex&gt; adj;
};
</code></pre>

<p>Here is the definition of the Graph class:</p>

<pre><code>class Graph
{
    public:
        Graph();
        ~Graph();
        void addEdge(std::string v1, std::string v2, int weight);
        void addVertex(std::string name);
        void displayEdges();
    void Dijkstra(string sourceVertex, string destinationVertex);
    protected:
    private:
        std::vector&lt;vertex&gt; vertices;

};
</code></pre>

<p>Here's the code I've written:</p>

<pre><code>void Graph::Dijkstra(string starting, string destination){
vertex * start;
vertex * ending;
for(int i=0;i&lt;vertices.size();i++){
    vertices[i].visited=false;
    vertices[i].distance=INT_MAX;
    vertices[i].previous=NULL;
    if(vertices[i].name==starting){
        start=&amp;vertices[i];
    }
    if(vertices[i].name==destination){
        ending=&amp;vertices[i];
    }
}
start-&gt;visited=true;
start-&gt;distance=0;
vector&lt;vertex *&gt; solved;
vector&lt;vertex *&gt; path;
vertex *tmp;
vertex *parent;
while(!ending-&gt;visited){ //pseudo territory
    int minDistance=INT_MAX;
    tmp=NULL;
    for(int i=0;i&lt;solved.size();i++){
        vertex * s=solved[i];
        for(int y=0;y&lt;s-&gt;adj.size();y++){
            if(!s-&gt;adj[y].v-&gt;visited){
                s-&gt;distance=s-&gt;distance+s-&gt;adj[y].v-&gt;distance;
                if(s-&gt;distance&lt;minDistance){
                    tmp=s-&gt;adj[y].v;
                    minDistance=s-&gt;distance;
                    parent=s-&gt;previous;
                }
            }
        }
    }
}
tmp-&gt;distance=minDistance;
tmp-&gt;previous=parent;
tmp-&gt;visited=true;}
</code></pre>

<p>The error I'm hitting with this code is that minDistance is an unidentified variable at the very bottom, when I set tmp->distance to it. Any clues about what I need to fix?</p>