title: Implementation of Dijkstra`s Algorithm - Stuck in an infinite loop
body: <p>Can someone, please, help me with my implementation? I am stuck in an infinite loop, but I don't know why. I think the problem is in the step where I look for the node with the minimum distance... I would really appreciate some help in this.</p>
  
  <pre><code>import java.util.*;
  
  public class Dijkstra {
  private Map&lt;String, Integer&gt; dist;
  private Set&lt;Vertex&gt; unvisited;
  //private Set&lt;Vertex&gt; processed;
  private Vertex source;
  private Graph g;
  
  public Dijkstra(Graph g, Vertex source) {
      this.g = g;
      this.source = source;
      //dist = new int[g.numOfVertices()];
      dist = new HashMap&lt;String, Integer&gt;();
      for(Vertex v: g.getVertices()) {
          if (v == this.source)
              dist.put(v.getId(), 0);
          else
              dist.put(v.getId(), Integer.MAX_VALUE);
      }
      unvisited = new HashSet&lt;Vertex&gt;(); 
      for(int i = 1; i &lt; g.numOfVertices(); i++) {
          unvisited.add(g.getVertices().get(i));
      }       
  }
  
  public ArrayList&lt;Integer&gt; getShortestPaths() {
      while (!unvisited.isEmpty()) {
          Vertex current = this.getMinimum();
          System.out.println("Hello1");
          unvisited.remove(current);
          System.out.println("Hello2: "+ current.getId());
          if (dist.get(current.getId()) == Integer.MAX_VALUE)
              break;
          Map&lt;Vertex,Integer &gt; neighbors = new HashMap&lt;Vertex,Integer&gt;();
          for (Edge e : g.getEdges()) {
              if (e.getSource().getId() == current.getId() &amp;&amp; unvisited.contains(e.getDestination())) {
                  neighbors.put(e.getDestination(), e.getWeight());
              }
          }
          for (Vertex v : neighbors.keySet()) {
              int alt = dist.get(current.getId()) + neighbors.get(v);
              if (alt &lt; dist.get(v.getId())) {
                  dist.put(v.getId(), alt);
              }
          }           
      }
  
  
  
  
  
      return new ArrayList&lt;Integer&gt; (dist.values());//(ArrayList&lt;Integer&gt;) dist.values();
  }
  
  public Vertex getMinimum() {
      int indexOfMinimum = -1;
      //String indexOfMinimum = "";
      int minimum = Integer.MAX_VALUE;
      for (String i : dist.keySet() ) {
          if (dist.get(i) &lt; minimum) {
              minimum = dist.get(i);
              System.out.println(minimum);
              indexOfMinimum = Integer.parseInt(i);
          }
      }
      return g.getVertices().get(indexOfMinimum);
  }
  
  
  
  
  public static void main(String[] args) {
      System.out.println("Hello World!!!");
      List&lt;Vertex&gt; v = new ArrayList&lt;Vertex&gt;();
      List&lt;Edge&gt; e = new ArrayList&lt;Edge&gt;();
      v.add(new Vertex("0"));
      v.add(new Vertex("1"));
      v.add(new Vertex("2"));
      v.add(new Vertex("3"));
      Graph g = new Graph(v ,e);
      g.addEdge(v.get(0), v.get(3), 1);
      g.addEdge(v.get(0), v.get(2), 4);
      g.addEdge(v.get(3), v.get(2), 2);
      g.addEdge(v.get(3), v.get(1), 6);
      g.addEdge(v.get(2), v.get(1), 3);
      Dijkstra sp = new Dijkstra(g, v.get(0));
      ArrayList&lt;Integer&gt; dist1 = sp.getShortestPaths();
  
      for (int i: dist1) { 
          System.out.println("Hello");
          System.out.println(dist1.get(i));
      }
      //v.add(new Vertex("5"));
      //v.add(new Vertex("6"));
      //v.add(new Vertex("7"));
  
  }
  
  }
  
  
  public class Graph {
  private final List&lt;Vertex&gt; vertices;
  private final List&lt;Edge&gt; edges;
  
  public Graph(List&lt;Vertex&gt; vertices, List&lt;Edge&gt; edges) {
      this.vertices = vertices;
      this.edges = edges;
  }
  
  public List&lt;Vertex&gt; getVertices() {
      return vertices;
  }
  
  public List&lt;Edge&gt; getEdges() {
      return edges;
  }
  
  public void addEdge(Vertex from, Vertex to, int weight) {
      edges.add(new Edge(from, to, weight));
  }
  
  public void addVertex(Vertex v) {
      vertices.add(v);
  }
  
  public int numOfVertices() {
      return this.vertices.size();
  }
  
  public int numOfEdges() {
      return this.edges.size();
  }
  }
  
  class Vertex {
  final private String id;
  
  public Vertex(String id) {
      this.id = id;
  }
  
  public String getId() {
      return this.id;
  }
  }
  
  class Edge {
  //private final String id;
  private final Vertex source;
  private final Vertex destination;
  private final int weight;
  
  public Edge(Vertex source, Vertex destination, int weight) {
      this.source = source;
      this.destination = destination;
      this.weight = weight;
  }
  
  public Vertex getSource() {
      return this.source;
  }
  
  public Vertex getDestination() {
      return this.destination;
  }
  
  public int getWeight() {
      return this.weight;
  }
  
  }
  </code></pre>