title: Breadth First Search using HashMap<Vertex, List<Vertex>>
body: <p>I am trying to perform a breadth first search on a large graph. I have a list of the vertex's with each of its neighbors. I have put the data into a hashmap with the Key being the Vertex and the Value being a List of Vertex that neighbor the Key. I have tried the following but the second "For Loop" is running indefinitely because of the way I am using the Key/Value pairs to loop. But I can't think of another way to loop properly and hit each node. </p>
  
  <pre><code>public static void BFS(HashMap&lt;Vertex, List&lt;Vertex&gt;&gt; map) {
          Queue&lt;Vertex&gt; myQ = new LinkedList&lt;Vertex&gt;();
          for(HashMap.Entry&lt;Vertex, List&lt;Vertex&gt;&gt; entry : map.entrySet()) {
              if(entry.getKey().getCount() == 0){
                  count++;
                  entry.getKey().setCount(count);
                  myQ.add(entry.getKey());
                  while(!myQ.isEmpty()) {
                      for(Vertex neighbor : entry.getValue()){
                          if(neighbor.getCount() == 0) {
                              count++;
                              neighbor.setCount(count);
                              myQ.add(neighbor);
                          }
                      }
                      myQ.remove(entry.getKey());
                  }                   
              }       
          }
      }
  </code></pre>
  
  <p>the count attribute is used to track if a Vertex has already been visited.</p>
  
  <p>Any help would be appreciated, just looking for a new way of thinking about how to loop through the HashMap. Or if I am doing something completely wrong :)</p>