title: Using Dijkstra's algorithm with an unordered_map graph
body: <p>So this my current code, I will post the header declarations below...</p>
  
  <pre><code>// Using Dijkstra's
  int Graph::closeness(string v1, string v2){
  int edgesTaken = 0;
  unordered_map&lt;string, bool&gt; visited;
  unordered_map&lt;string, int&gt; distances;
  string source = v1; // Starting node
  while(source != v2 &amp;&amp; !visited[source]){
      // The node has been visited
      visited[source] = 1;
      // Set all initial distances to infinity
      for(auto i = vertices.begin(); i != vertices.end(); i++){
          distances[i-&gt;first] = INT_MAX;
      }
      // Consider all neighbors and calculate distances from the current node
      // &amp; store them in the distances map
      for(int i = 0; i &lt; vertices[source].edges.size(); i++){
          string neighbor = vertices[source].edges[i].name;           
          distances[neighbor] = vertices[source].edges[i].weight; 
      }   
      // Find the neighbor with the least distance
      int minDistance = INT_MAX;
      string nodeWithMin;
      for(auto i = distances.begin(); i != distances.end(); i++){
          int currDistance = i-&gt;second;
          if(currDistance &lt; minDistance){
              minDistance = currDistance;
              nodeWithMin = i-&gt;first;
          }       
      }
      // There are no neighbors and the node hasn't been found yet
      // then terminate the function and return -1. The nodes aren't connected
      if(minDistance == INT_MAX) 
          return -1;
      // Set source to the neighbor that has the shortest distance
      source = nodeWithMin;
      // Increment edgesTaken
      edgesTaken++;
      // clear the distances map to prepare for the next iteration
      distances.clear();
  }
  return edgesTaken;
  }
  </code></pre>
  
  <p>Declarations (This is an undirected graph) :</p>
  
  <pre><code>class Graph{
      private:
              // This holds the connected name and the corresponding we
              struct EdgeInfo{
                      std::string name;
                      int weight;
                      EdgeInfo() { }
                      EdgeInfo(std::string n, int w) : name(n), weight(
              };
  
              // This will hold the data members of the vertices, inclu
              struct VertexInfo{
                      float value;
                      std::vector&lt;EdgeInfo&gt; edges;
                      VertexInfo() { }
                      VertexInfo(float v) : value(v) { }
              };
  
              // A map is used so that the name is used as the index
              std::unordered_map&lt;std::string, VertexInfo&gt; vertices;
  </code></pre>
  
  <p><b> NOTE: Please do not suggest that I change the header declarations, I am contributing to a project that has already had 8 other functions written and it's definitely too late to go back and change anything since every other function would then have to be rewritten </b><br><br>
  I'm currently getting incorrect output.  The function is handling a 0 distance situation correctly however (If two vertices aren't connected then the function should return a -1).  If the two nodes are the same vertex ex closeness("Boston", "Boston") then the function should return a 0.  </p>
  
  <p><b> Example graph </b>
  <a href="https://i.stack.imgur.com/fQLpT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fQLpT.jpg" alt="Example Graph"></a>
  </p>
  
  <p>the closeness of the following two vertices on the left will be on the right:</p>
  
  <pre><code>Correct:
  Trenton -&gt; Philadelphia: 2
  Binghamton -&gt; San Francisco: -1
  Boston -&gt; Boston: 0
  Palo Alto -&gt; Boston: -1
  
  Output of my function:
  Trenton -&gt; Philadelphia: 3
  Binghamton -&gt; San Francisco: -1
  Boston -&gt; Boston: 0
  Palo Alto -&gt; Boston: 3
  </code></pre>
  
  <p>I've tried to copy dijkstra's exactly how it is described, but I'm getting incorrect readings, I've been trying to figure this out for a while now -> Can anyone point me in the right direction?</p>