code: def test_bfs(idtype, n=100):
      def _bfs_nx(g_nx, src):
          edges = nx.bfs_edges(g_nx, src)
          layers_nx = [set([src])]
          edges_nx = []
          frontier = set()
          edge_frontier = set()
          for u, v in edges:
              if u in layers_nx[-1]:
                  frontier.add(v)
                  edge_frontier.add(g.edge_ids(int(u), int(v)))
              else:
                  layers_nx.append(frontier)
                  edges_nx.append(edge_frontier)
                  frontier = set([v])
                  edge_frontier = set([g.edge_ids(u, v)])
          # avoids empty successors
          if len(frontier) > 0 and len(edge_frontier) > 0:
              layers_nx.append(frontier)
              edges_nx.append(edge_frontier)
          return layers_nx, edges_nx
  
      a = sp.random(n, n, 3 / n, data_rvs=lambda n: np.ones(n))
      g = dgl.from_scipy(a).astype(idtype)
  
      g_nx = g.to_networkx()
      src = random.choice(range(n))
      layers_nx, _ = _bfs_nx(g_nx, src)
      layers_dgl = dgl.bfs_nodes_generator(g, src)
      assert len(layers_dgl) == len(layers_nx)
      assert all(toset(x) == y for x, y in zip(layers_dgl, layers_nx))
  
      g_nx = nx.random_tree(n, seed=42)
      g = dgl.from_networkx(g_nx).astype(idtype)
      src = 0
      _, edges_nx = _bfs_nx(g_nx, src)
      edges_dgl = dgl.bfs_edges_generator(g, src)
      assert len(edges_dgl) == len(edges_nx)
      assert all(toset(x) == y for x, y in zip(edges_dgl, edges_nx))
  
file_path: dgl/tests/python/common/test_traversal.py
imports: import itertools
  import random
  import sys
  import time
  import unittest
  import backend
  import dgl
  import networkx
  import numpy
  import scipy.sparse
  from utils import parametrize_idtype
label_desc: Use image node_id 2 for calling a global function with example usage: test_bfs(idtype, n) and returns: layers_nx,  edges_nx