lib/math/discrete/graph/algorithms.rb
# Breadth-first search (BFS) is an algorithm for traversing a graph
# It starts at the givenroot and explores the neighbor nodes first, before moving to the next level neighbours.
# Read more:
# https://en.wikipedia.org/wiki/Breadth-first_search
Breadth-first search (BFS) is an algorithm for traversing a graph
It starts at the givenroot and explores the neighbor nodes first, before moving to the next level neighbours.
Breadth - first search ( BFS ) is an algorithm for traversing a graph It starts at the givenroot and explores the neighbor nodes first before moving to the next level neighbours .
def breadth_first_search(root: vertex_set.first)
    return {} if @vertex_map.empty?
    raise TypeError, 'root must be of the type Math::Discrete::Graph::Vertex' unless root.is_a? Vertex
    raise Graph::VertexNotFound, "could not find vertex with label=#{root.label}" unless vertex_labels.include? root.label
    search_queue = Containers::Queue.new [root]
    search_tree = { root.label => { distance: 0, parent: nil } }
    until search_queue.empty?
      vertex = search_queue.pop
      vertex.adjacent_vertices.each do |child|
        if search_tree[child.label].nil?
          search_tree[child.label] = {
            distance: search_tree[vertex.label][:distance] + 1,
            parent: vertex.label
          }
          search_queue.push child
        end
      end
    end
    search_tree
  end
def breadth_first_search ( root : vertex_set . first ) return { } if @vertex_map . empty? raise TypeError , 'root must be of the type Math::Discrete::Graph::Vertex' unless root . is_a? Vertex raise Graph :: VertexNotFound , "could not find vertex with label=#{root.label}" unless vertex_labels . include? root . label search_queue = Containers :: Queue . new [ root ] search_tree = { root . label => { distance : 0 , parent : nil } } until search_queue . empty? vertex = search_queue . pop vertex . adjacent_vertices . each do | child | if search_tree [ child . label ] . nil? search_tree [ child . label ] = { distance : search_tree [ vertex . label ] [ :distance ] + 1 , parent : vertex . label } search_queue . push child end end end search_tree end
Breadth-first search (BFS) is an algorithm for traversing a graph
It starts at the givenroot and explores the neighbor nodes first, before moving to the next level neighbours.
Breadth - first search ( BFS ) is an algorithm for traversing a graph It starts at the givenroot and explores the neighbor nodes first before moving to the next level neighbours .
root
<not_specific>
vertex_set
<not_specific>
first
<not_specific>