class Algorithmable::Graphs::Undirected

Attributes

edges[R]
vertices[R]

Public Class Methods

new(vertices = 0) click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 6
def initialize(vertices = 0)
  @vertices = vertices
  @edges = 0
  @adj = []
  @vertices.times { |i| @adj[i] = [] }
end

Public Instance Methods

add_edge(left_vertex, right_vertex) click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 13
def add_edge(left_vertex, right_vertex)
  @adj[left_vertex] ||= []
  @adj[right_vertex] ||= []
  @adj[left_vertex].push right_vertex
  @adj[right_vertex].push left_vertex
  @edges = @edges.next
end
adjacency(vertex) click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 21
def adjacency(vertex)
  @adj[vertex]
end
degree(vertex) click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 29
def degree(vertex)
  fail "Vertex #{vertex} is not valid." unless valid_vertex?(vertex)
  adjacency(vertex).size
end
to_s() click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 34
def to_s
  data = ''
  @vertices.times do |vertex|
    data += "( #{vertex} => "
    @adj[vertex].each do |neighbor|
      data += "#{neighbor} "
    end
    data += ') '
  end
  "#<#{self.class} @vertices=#{@vertices} @edges=#{@edges} @data=#{data}>"
end
valid_vertex?(vertex) click to toggle source
# File lib/algorithmable/graphs/undirected.rb, line 25
def valid_vertex?(vertex)
  !(0 > vertex || vertex >= @vertices)
end