title: Counting levels in Breadth First Search (Distance between start node and goal node)
body: <p>Can anyone help me how to count the visited levels of a graph using Breadth First Search in Java?</p>
  
  <p>Here is my method, I have start node (str) and end node (goal), when the loop reach the goal node it should be stopped.</p>
  
  <p>What I want now is counting the levels from start node to end node.</p>
  
  <pre><code>public void bfs(String str,String goal) {
      int strInx = findIndex(str);
      vertexList[strInx].wasVisited = true;
      theQueue.insert(strInx);
      int v2;
      boolean bre = false;
      while (!theQueue.isEmpty()) {
          System.out.println(vertexList[theQueue.getFront()].label);
          int v1 = theQueue.remove();
          while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {
              vertexList[v2].wasVisited = true;
              System.out.println("--V2---&gt;"+vertexList[v2].label);
              theQueue.insert(v2);
  
              if(goal.equals(vertexList[v2].label)) {
                  bre=true;
                  break;
              }
          }
          if(bre) 
              break;   
      }                
      for (int j = 0; j &lt; nVerts; j++) {
          vertexList[j].wasVisited = false;
      }
  }
  </code></pre>