class Shortest::Path::Graph

Attributes

edges[RW]

Public Class Methods

new() click to toggle source
# File lib/shortest/path/general.rb, line 16
def initialize
  @edges=[]
end

Public Instance Methods

connect(src, dst, length) click to toggle source
# File lib/shortest/path/general.rb, line 20
def connect(src, dst, length)
  raise "No such vertex: #{src}" unless self.include?(src)
  raise "No such vertex: #{dst}" unless self.include?(dst)
  @edges.push Edge.new(src, dst, length)
end
connect_mutually(vertex1, vertex2, length) click to toggle source
# File lib/shortest/path/general.rb, line 26
def connect_mutually(vertex1, vertex2, length)
  self.connect(vertex1, vertex2, length)
  self.connect(vertex2, vertex1, length)
end
neighbors(vertex) click to toggle source
# File lib/shortest/path/general.rb, line 38
def neighbors(vertex)
  neighbors = []
  @edges.each{|edge| neighbors.push edge.dst if edge.src==vertex }
  neighbors.uniq
end
remove_edge(vertex1, vertex2) click to toggle source
# File lib/shortest/path/general.rb, line 31
def remove_edge(vertex1, vertex2)
  for_remove = @edges.select{ |e| (e.src == vertex1 && e.dst == vertex2) || (e.src == vertex2 && e.dst == vertex1) }
  for_remove.each do |edge|
    @edges.delete_at(@edges.index(edge))
  end
end