title: Generating BFS forest of graphs
body: <p>I want to generate a BFS forest of of a DAG (Direct Acyclic Graph). This means my Tree class needs to be a general tree and not a binary tree (in other words, I can't know the number of children a node will have ahead of time when I am generating a forest). Most of the code is written and shown below, however I lack one line that, for the life of me, escapes me!</p>
  
  <pre><code>public Tree BFS(V start)
  {
      reset();
      LinkedList&lt;GraphMatrixVertex&lt;V&gt;&gt; list = new LinkedList&lt;GraphMatrixVertex&lt;V&gt;&gt;();
      GraphMatrixVertex&lt;V&gt; vert = dict.get(start);
      Tree root = new Tree(vert); 
      list.add(vert);
      do
      {
          vert = list.getFirst();
          Iterator&lt;V&gt; ni = neighbors(start);
          while(ni.hasNext())
          {
              V v = ni.next();
              GraphMatrixVertex&lt;V&gt; vtx = dict.get(v);
              if(!vtx.isVisited())
              {
                  list.add(vtx);
                              vtx.visit();
                  root.addChild(new Tree(vtx));
              }
          }
      //code goes here
      }
      while(!list.isEmpty());
  
      return root;
  }
  </code></pre>
  
  <p>My Tree class stores a value parameter, a parent reference, and a list of children.
  My problem is referencing the next tree node. Once I have added all the unvisited neighbors as childs of the current node, how do I get to the next node?</p>
  
  <p>EDIT:</p>
  
  <p>So it would look something like this?</p>
  
  <pre><code>public void bfs(Tree parent)
  {   
      Iterator&lt;V&gt; ni = neighbors((V) parent.value());
      if(ni.hasNext())
      {
              while(ni.hasNext())
              {
              V next = ni.next();
              GraphMatrixVertex&lt;V&gt; vert = dict.get(next);
              if(!vert.isVisited())
                  parent.addChild(new Tree(next));
          }   
      }
  }
  </code></pre>
  
  <p>where does the recursive call go?</p>