Class: Graphsrb::Graph

Inherits:
BaseGraph show all
Defined in:
lib/graphsrb/graph.rb

Instance Method Summary collapse

Methods inherited from BaseGraph

#add_edge, #add_vertex, #clear, #copy, #edge, #edge_count, #edges, #has_edge?, #has_vertex?, #increase_weight, #initialize, #remove_edge, #remove_vertex, #update_weight, #vertex_count, #vertices

Constructor Details

This class inherits a constructor from Graphsrb::BaseGraph

Instance Method Details

#adjacent_vertices(vertex) ⇒ Object Also known as: neighborhood

Retrieves adjacent vertices of a vertex



4
5
6
7
8
9
10
11
12
13
14
15
# 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

#degree(v) ⇒ Object

Returns degree of a vertex



26
27
28
# File 'lib/graphsrb/graph.rb', line 26

def degree(v)
  _incident_nodes(v.id).size
end

#incident_edges(v) ⇒ Object

Retrieves incident edges of a vertex



20
21
22
23
# 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_degreeObject

Returns maximum degree of all graph vertices



31
32
33
# File 'lib/graphsrb/graph.rb', line 31

def max_degree
  self.vertices.map{|v| self.degree(v)}.max
end