class GraphNjae::Edge

An edge (or multiedge) in a graph. The edge can have arbitrary attributes, treated as method names.

Each connection is handled by a Graph::Connection object, so that each end of the Edge can have it’s own attributes.

Public Class Methods

new(values = {}) click to toggle source
Calls superclass method
# File lib/graph.njae/edge.rb, line 11
def initialize(values = {})
  super(values)
  self.connections = []
  self
end

Public Instance Methods

<<(other) click to toggle source

Connect this edge to a vertex

# File lib/graph.njae/edge.rb, line 18
def <<(other)
  c = Connection.new
  c.end = other
  other.edges << self unless other.edges.include? self
  self.connections << c
  self
end
connection_at(vertex) click to toggle source

Return the connection object that joins this Edge to the specified Vertex

# File lib/graph.njae/edge.rb, line 32
def connection_at(vertex)
  self.connections.find {|c| c.end.equal?  vertex}
end
other_end(vertex) click to toggle source

Return the vertex at the other end of the one given. Self-loops should still return the vertex

# File lib/graph.njae/edge.rb, line 38
def other_end(vertex)
  if self.vertices[0] == vertex
    self.vertices[1]
  else
    self.vertices[0]
  end
end
to_dot(opts = {}) { |self| ... } click to toggle source
# File lib/graph.njae/edge.rb, line 50
def to_dot(opts = {})
  if block_given?
    yield self
  else
    dot = self.connections[0].end.object_id.to_s + " -- " + self.connections[1].end.object_id.to_s
    if opts.size > 0
      dot << ' {'
      dot << opts.keys.map { |k|
        (k.to_s + ' = "' + self.instance_eval(opts[k].to_s).to_s) + '"'
                    }.join(', ')
      dot << '}'
    end
    dot << ';'
  end
end
to_s() click to toggle source
# File lib/graph.njae/edge.rb, line 46
def to_s
  '<E: ' + self.type.to_s + ' [' + self.vertices.map {|n| n.to_s}.join(', ') + '] >'
end
vertices() click to toggle source

Return the set of vertices this edge connects.

# File lib/graph.njae/edge.rb, line 27
def vertices
  self.connections.map {|c| c.end}
end