Method: MGraph::Graph#breadth_first_traverse

Defined in:
lib/mgraph/graph.rb

#breadth_first_traverse(starting_vertex) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mgraph/graph.rb', line 18

def breadth_first_traverse starting_vertex
  return enum_for(:breadth_first_traverse, starting_vertex) unless block_given?

  queue = [starting_vertex, *neighbors(starting_vertex)]
  visited = []
  while vertex = queue.shift
    yield vertex
    visited << vertex
    queue += neighbors(vertex).to_a - visited
    queue = queue.uniq
  end
end