class Graphsrb::Graph

Public Instance Methods

adjacent_vertices(vertex) click to toggle source

Retrieves adjacent vertices of a vertex

# File lib/graphsrb/graph.rb, line 4
def adjacent_vertices(vertex)
  nodes = []
  id = vertex.id
  nodes = adj_table[id].nodes unless adj_table[id].nil?
  vertices.each do |v|
    next if v.id == id
    node = adj_table[v.id].find(_create_node(id))
    nodes << _create_node(v.id, weight:node.weight) unless node.nil?
  end
  #Convert nodes into vertices
  nodes.map{|node| _create_vertex(node.vertex.id)}
end
Also aliased as: neighborhood
degree(v) click to toggle source

Returns degree of a vertex

# File lib/graphsrb/graph.rb, line 26
def degree(v)
  _incident_nodes(v.id).size
end
incident_edges(v) click to toggle source

Retrieves incident edges of a vertex

# File lib/graphsrb/graph.rb, line 20
def incident_edges(v)
  #Convert nodes into edges with weights
  _incident_nodes(v.id).map{|node| _create_edge(v.id, node.vertex.id, weight:node.weight)}
end
max_degree() click to toggle source

Returns maximum degree of all graph vertices

# File lib/graphsrb/graph.rb, line 31
def max_degree
  self.vertices.map{|v| self.degree(v)}.max
end
neighborhood(vertex)
Alias for: adjacent_vertices

Private Instance Methods

_incident_nodes(id) click to toggle source
# File lib/graphsrb/graph.rb, line 36
def _incident_nodes(id)
  nodes = []
  nodes = adj_table[id].nodes unless adj_table[id].nil?
  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