class Graphsrb::Digraph

Directed graph

Public Instance Methods

adjacent_vertices(vertex) click to toggle source

Retrieves adjacent vertices of a vertex (takes only outgoing edeges)

# File lib/graphsrb/digraph.rb, line 5
def adjacent_vertices(vertex)
  nodes = []
  id = vertex.id
  nodes = adj_table[id].nodes unless adj_table[id].nil?
  #Convert nodes into vertices
  nodes.map{|node| _create_vertex(node.vertex.id)}
end
Also aliased as: neighborhood
edge(v, u) click to toggle source

Retrieves an edge

# File lib/graphsrb/digraph.rb, line 22
def edge(v, u)
  id1, id2 = v.id, u.id
  if has_vertex?(id1)
    node = adj_table[id1].find(_create_node(id2))
    return _create_edge(id1, id2, weight:node.weight) if node
  end
end
edge?(id1, id2)
Alias for: has_edge?
has_edge?(id1, id2) click to toggle source

Checks whether the digraph has an edge

# File lib/graphsrb/digraph.rb, line 16
def has_edge?(id1, id2)
  has_vertex?(id1) && adj_table[id1].has_node?(_create_node(id2))
end
Also aliased as: edge?
incoming_edges(v) click to toggle source

Retrieves incoming edges of a vertex

# File lib/graphsrb/digraph.rb, line 56
def incoming_edges(v)
  #Convert nodes into edges
  _incoming_nodes(v.id).map{|node| _create_edge(node.vertex.id, v.id, weight:node.weight)}
end
increase_weight(v, u, dw) click to toggle source

Increses edge weight by w

# File lib/graphsrb/digraph.rb, line 37
def increase_weight(v, u, dw)
  id1, id2 = v.id, u.id
  adj_table[id1].increase_weight(_create_node(id2), dw) if has_vertex?(id1)
end
indegree(v) click to toggle source

Returns in-degree of a vertex

# File lib/graphsrb/digraph.rb, line 67
def indegree(v)
  _incoming_nodes(v.id).size
end
neighborhood(vertex)
Alias for: adjacent_vertices
outdegree(v) click to toggle source

Returns out-degree of a vertex

# File lib/graphsrb/digraph.rb, line 62
def outdegree(v)
  _outgoing_nodes(v.id).size
end
outgoing_edges(v) click to toggle source

Retrieves outgoing edges of a vertex

# File lib/graphsrb/digraph.rb, line 50
def outgoing_edges(v)
  #Convert nodes into edges
  _outgoing_nodes(v.id).map{|node| _create_edge(v.id, node.vertex.id, weight:node.weight)}
end
remove_edge(id1, id2) click to toggle source

Remove an edge from the graph

# File lib/graphsrb/digraph.rb, line 44
def remove_edge(id1, id2)
  adj_table[id1].delete(_create_node(id2)) if has_vertex?(id1)
  true
end
update_weight(v, u, w) click to toggle source

Updates edge weight

# File lib/graphsrb/digraph.rb, line 31
def update_weight(v, u, w)
  id1, id2 = v.id, u.id
  adj_table[id1].update_weight(_create_node(id2), w) if has_vertex?(id1)
end

Protected Instance Methods

_create_edge(id1, id2, args={}) click to toggle source
# File lib/graphsrb/digraph.rb, line 90
def _create_edge(id1, id2, args={})
  Graphsrb::DiEdge.new(id1, id2, args)
end

Private Instance Methods

_incoming_nodes(id) click to toggle source
# File lib/graphsrb/digraph.rb, line 79
def _incoming_nodes(id)
  nodes = []
  vertices.each do |vertex|
    next if vertex.id == id
    node = adj_table[vertex.id].find(_create_node(id))
    nodes << _create_node(vertex.id, weight:node.weight) unless node.nil?
  end
  nodes
end
_outgoing_nodes(id) click to toggle source
# File lib/graphsrb/digraph.rb, line 73
def _outgoing_nodes(id)
  nodes = []
  nodes = adj_table[id].nodes unless adj_table[id].nil?
  nodes
end