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 = {}
  @edge_number = 0
  initializer.each do |v|
    add_vertex!(v[:v])
    v[:i].each do |iv|
      add_vertex!(v[:v])
      add_vertex!(iv)
      add_edge!(v[:v], iv)
    end
  end
end

Public Instance Methods

add_edge!(from, to) click to toggle source
# File lib/graph.rb, line 35
def add_edge!(from, to)
  protected_add_edge!(from, to)
  @edge_number += 1
end
add_vertex!(vertex_id) click to toggle source
# File lib/graph.rb, line 28
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 40
def adjacted_with(vertex)
  raise GraphError.new("Graph does not contain vertex #{vertex}") unless @vertices.has_key?(vertex)
  @vertices[vertex].clone
end
delete_edge!(from, to) click to toggle source
# File lib/graph.rb, line 115
def delete_edge!(from, to)
  protected_delete_edge!(from, to)
  @edge_number -= 1
end
delete_vertex!(vertex) click to toggle source
# File lib/graph.rb, line 101
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 89
def edge_label_number
  @edge_labels.count
end
edge_number() click to toggle source
# File lib/graph.rb, line 81
def edge_number
  @edge_number
end
get_edge_label(from, to) click to toggle source
# File lib/graph.rb, line 61
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 69
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 97
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 93
def has_vertex?(vertex)
  @vertices.has_key?(vertex)
end
label_edge!(from, to, label) click to toggle source
# File lib/graph.rb, line 45
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 53
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
reverse!() click to toggle source
# File lib/graph.rb, line 120
def reverse!
  v = Hash.new()
  l = {}
  @vertices.keys.each do |from|
    v[from] = [].to_set
  end

  @vertices.keys.each do |from|
    @vertices[from].each do |to|
      v[to] << from
      if @edge_labels.include?(Pair.new(from, to))
        l[Pair.new(to, from)] = @edge_labels[Pair.new(from, to)]
      end
    end
  end
  @vertices = v
  @edge_labels = l
end
vertex_label_number() click to toggle source
# File lib/graph.rb, line 85
def vertex_label_number
  @vertex_labels.count
end
vertex_number() click to toggle source
# File lib/graph.rb, line 77
def vertex_number
  @vertices.count
end
vertices() click to toggle source
# File lib/graph.rb, line 139
def vertices
  @vertices
end

Protected Instance Methods

protected_add_edge!(from, to) click to toggle source
# File lib/graph.rb, line 145
def protected_add_edge!(from, to)
  if @vertices.has_key?(from) && @vertices.has_key?(to)
    @vertices[from] << to
  end
end
protected_delete_edge!(from, to) click to toggle source
# File lib/graph.rb, line 151
def protected_delete_edge!(from, to)
  if has_edge?(from, to)
    @vertices[from].delete(to)
    @edge_labels.delete(Pair.new(from, to))
  end
end