Stopping BFS traversal after reachin a certain depth in RGL


I am using RGL for some of the graph implementations. My requirements is
such that I need to find out all nodes that are at distance less than
3 from root.

For this, I thought of traversing the graph using BFS and stop the
traversal when i reach 4th level. But how do I do this?

Sandeep G

Take the example graph from the README

  require 'rgl/adjacency'
  require 'rgl/traversal'

  dg = RGL::DirectedAdjacencyGraph[1,2 ,2,3 ,2,4, 4,5, 6,4, 1,6]
  bfs = dg.bfs_iterator(1)
  bfs.each do |v|
  dist = bfs.distance_to_root(v)
  break if dist > 3
  print v, ': ', dist, "\n"

Should produce this result:

1: 1
6: 2
2: 2
4: 3
3: 3