class MGraph::Graph

Public Class Methods

new(graph_store = VertexEdgeTable.new) click to toggle source
# File lib/mgraph/graph.rb, line 8
def initialize graph_store = VertexEdgeTable.new
  @graph_store = graph_store
end

Public Instance Methods

add_edge(v1, v2) click to toggle source
# File lib/mgraph/graph.rb, line 12
def add_edge v1, v2
  edge = Edge.new v1, v2
  @graph_store.add_edge edge
  edge
end
breadth_first_traverse(starting_vertex) { |vertex| ... } click to toggle source
# 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
each_vertex() { |vertex| ... } click to toggle source
# File lib/mgraph/graph.rb, line 31
def each_vertex
  return vertices.each unless block_given?
  vertices.each { |vertex| yield vertex }
end
edges() click to toggle source
# File lib/mgraph/graph.rb, line 36
def edges
  @graph_store.edges
end
has_edge?(v1, v2) click to toggle source
# File lib/mgraph/graph.rb, line 40
def has_edge? v1, v2
  edge = Edge.new v1, v2
  edges.include? edge
end
has_vertex?(vertex) click to toggle source
# File lib/mgraph/graph.rb, line 45
def has_vertex? vertex
  vertices.include? vertex
end
neighbors(vertex) click to toggle source
# File lib/mgraph/graph.rb, line 49
def neighbors vertex
  @graph_store.neighbors vertex
end
vertices() click to toggle source
# File lib/mgraph/graph.rb, line 53
def vertices
  @graph_store.vertices
end