class Silicium::Graphs::OrientedGraph

Public Class Methods

new(initializer = []) click to toggle source
# File lib/graph.rb, line 13
def initialize(initializer = [])
  @vertices = {}
  @edge_labels = {}
  @vertex_labels = {}
  initializer.each do |v|
    add_vertex!(v[:v])
    v[:i].each { |iv| add_edge_force!(v[:v], iv)}
  end
end

Public Instance Methods

add_edge!(from, to) click to toggle source
# File lib/graph.rb, line 30
def add_edge!(from, to)
  if @vertices.has_key?(from) && @vertices.has_key?(to)
    @vertices[from] << to
  end
end
add_edge_force!(from, to) click to toggle source

should only be used in constructor

# File lib/graph.rb, line 37
def add_edge_force!(from, to)
  add_vertex!(from)
  add_vertex!(to)
  add_edge!(from, to)
end
add_vertex!(vertex_id) click to toggle source
# File lib/graph.rb, line 23
def add_vertex!(vertex_id)
  if @vertices.has_key?(vertex_id)
    return
  end
  @vertices[vertex_id] = [].to_set
end
adjacted_with(vertex) click to toggle source
# File lib/graph.rb, line 43
def adjacted_with(vertex)
  unless @vertices.has_key?(vertex)
    raise GraphError.new("Graph does not contain vertex #{vertex}")
  end

  @vertices[vertex].clone
end
delete_edge!(from, to) click to toggle source
# File lib/graph.rb, line 125
def delete_edge!(from, to)
  if has_edge?(from, to)
    @vertices[from].delete(to)
    @edge_labels.delete(Pair.new(from, to))
  end
end
delete_vertex!(vertex) click to toggle source
# File lib/graph.rb, line 111
def delete_vertex!(vertex)
  if has_vertex?(vertex)
    @vertices.keys.each do |key|
      delete_edge!(key, vertex)
    end
    @vertices.delete(vertex)
    @vertex_labels.delete(vertex)

    @vertices.keys.each do |key|
      @edge_labels.delete(Pair.new(vertex, key))
    end
  end
end
edge_label_number() click to toggle source
# File lib/graph.rb, line 99
def edge_label_number
  @edge_labels.count
end
edge_number() click to toggle source
# File lib/graph.rb, line 87
def edge_number
  res = 0
  @vertices.values.each do |item|
    res += item.count
  end
  res
end
get_edge_label(from, to) click to toggle source
# File lib/graph.rb, line 67
def get_edge_label(from, to)
  if !@vertices.has_key?(from) || ! @vertices[from].include?(to)
    raise GraphError.new("Graph does not contain edge (#{from}, #{to})")
  end

  @edge_labels[Pair.new(from, to)]
end
get_vertex_label(vertex) click to toggle source
# File lib/graph.rb, line 75
def get_vertex_label(vertex)
  unless @vertices.has_key?(vertex)
    raise GraphError.new("Graph does not contain vertex #{vertex}")
  end

  @vertex_labels[vertex]
end
has_edge?(from, to) click to toggle source
# File lib/graph.rb, line 107
def has_edge?(from, to)
  @vertices.has_key?(from) && @vertices[from].include?(to)
end
has_vertex?(vertex) click to toggle source
# File lib/graph.rb, line 103
def has_vertex?(vertex)
  @vertices.has_key?(vertex)
end
label_edge!(from, to, label) click to toggle source
# File lib/graph.rb, line 51
def label_edge!(from, to, label)
  unless @vertices.has_key?(from) && @vertices[from].include?(to)
    raise GraphError.new("Graph does not contain edge (#{from}, #{to})")
  end

  @edge_labels[Pair.new(from, to)] = label
end
label_vertex!(vertex, label) click to toggle source
# File lib/graph.rb, line 59
def label_vertex!(vertex, label)
  unless @vertices.has_key?(vertex)
    raise GraphError.new("Graph does not contain vertex #{vertex}")
  end

  @vertex_labels[vertex] = label
end
vertex_label_number() click to toggle source
# File lib/graph.rb, line 95
def vertex_label_number
  @vertex_labels.count
end
vertex_number() click to toggle source
# File lib/graph.rb, line 83
def vertex_number
  @vertices.count
end