title: Shortest path algorithm in unweighted, undirected graph
body: <pre><code>public void shortestPath(String searched,String destination) {
      Queue &lt;Vertex&gt; queue=new LinkedList&lt;&gt;();
      Stack &lt;Vertex&gt; path=new Stack&lt;&gt;();
      Vertex root=vertices.get(searched);
      Vertex end=vertices.get(destination);
      
      queue.add(root);
      root.setVisited(true);
      
      while(!queue.isEmpty())
      {
          Vertex current=queue.poll();
          Vertex[] neighbors=getNeighbor(current);
          
          for(int i=0;i&lt;neighbors.length;i++)
          {
              if(!neighbors[i].getVisited())
              {
                  queue.add(neighbors[i]);
                  neighbors[i].setVisited(true);
                  neighbors[i].setPrevious(current);
                  
                  if(neighbors[i]==end)
                  {
                      queue.clear();
                      break;
                  }
              }
          }
      }
      
      while(end!=null)
      {
          path.add(end);
          end.setVisited(false);
          end=end.getPrevious();
      }
      
      while(!path.isEmpty())
      {
          System.out.print(path.pop().getName()+&quot; &quot;);
      }
      System.out.println();
  }
  </code></pre>
  <p>I tried to write shortest path algorithm for unweighted and undirected graph. Algorithm works for some cases, but in other cases stucks in while(end!=null) part and loops infinitely. I can't find the mistake I made in the code.(vertices is hashmap and vertex is a class which I created.)</p>