title: Traversing level order for the graph in networkx
body: <p>I am trying to convert a <code>DiGraph</code> into n-ary tree and displaying the nodes in level order or BFS. My tree is similar to this, but much larger, for simplicity using this example: </p>
  
  <pre><code>G = networkx.DiGraph()
  G.add_edges_from([('n', 'n1'), ('n', 'n2'), ('n', 'n3')])
  G.add_edges_from([('n4', 'n41'), ('n1', 'n11'), ('n1', 'n12'), ('n1', 'n13')])
  G.add_edges_from([('n2', 'n21'), ('n2', 'n22')])
  G.add_edges_from([('n13', 'n131'), ('n22', 'n221')])
  </code></pre>
  
  <p>Tree: borrowed the data from this <a href="https://stackoverflow.com/questions/21866902/networkx-graph-searches-dfs-successors-vs-dfs-predecessors">question</a>:</p>
  
  <pre><code>n----&gt;n1---&gt;n11
   |     |---&gt;n12
   |     |---&gt;n13
   |           |---&gt;n131 
   |---&gt;n2              
   |     |----&gt;n21     
   |     |----&gt;n22     
   |            |---&gt;n221 
   |---&gt;n3
  </code></pre>
  
  <p>I am using <code>networkx.DiGraph</code> for this purpose and created the graph successfully. Here is my code for creating a DiGraph: </p>
  
  <pre><code>G = nx.DiGraph()
  roots = set()
  for l in raw.splitlines():
      if len(l):
          target, prereq = regex1.split(l)
          deps = tuple(regex2.split(prereq))
          print("Add node:") + target
          roots.add(target)
          G.add_node(target)
          for d in deps:
              if d:
                  G.add_edge(target, d)
  </code></pre>
  
  <p>I am reading the all the data from a file with about 200 lines in the following format and trying to get a dependency tree. My graph is around 100 nodes with 600 edges.</p>
  
  <pre><code>AAA: BBB,CCC,DDD,
  BBB:
  DDD: EEE,FFF,GGG,KKK
  GGG: AAA,BBB,III,LLL
  ....
  ...
  ..
  .
  </code></pre>
  
  <p>After looking into the networkx docs online, now I can achieve the the level order output doing a topological sort on the dependency tree, with the below code.</p>
  
  <pre><code>order =  nx.topological_sort(G)
  print "topological sort"
  print order 
  </code></pre>
  
  <p>output: </p>
  
  <pre><code>['n2', 'n3', 'n1', 'n21', 'n22', 'n11', 'n13', 'n12', 'n221', 'n131']
  </code></pre>
  
  <p>The order seems correct, but since I need to process the jobs in a batch (which saves time) and not sequentially, I want the output in level ordered output batches or using BFS. What is the best way to achieve this ?<br>
  ex: level[0:n], ex: </p>
  
  <pre><code>0. ['n'] 
  1. ['n2', 'n3', 'n1',] 
  2. ['n21', 'n22', 'n11',] 
  3. ['n13', 'n12', 'n221', 'n131'] 
  </code></pre>