Class: Graphos::Weighted::Node
- Inherits:
-
Object
- Object
- Graphos::Weighted::Node
- Defined in:
- lib/graphos/weighted/node.rb
Overview
This class represents a node in a weighted graph
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
- #add_edge(to, weight) ⇒ Object
- #degree ⇒ Object
- #edge(to) ⇒ Object
-
#initialize(index) ⇒ Node
constructor
A new instance of Node.
- #neighbor_of?(index) ⇒ Boolean
- #neighbors ⇒ Object
Constructor Details
#initialize(index) ⇒ Node
Returns a new instance of Node.
9 10 11 12 |
# File 'lib/graphos/weighted/node.rb', line 9 def initialize index @index = index @edges = [] end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
8 9 10 |
# File 'lib/graphos/weighted/node.rb', line 8 def edges @edges end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
8 9 10 |
# File 'lib/graphos/weighted/node.rb', line 8 def index @index end |
Instance Method Details
#add_edge(to, weight) ⇒ Object
14 15 16 17 18 |
# File 'lib/graphos/weighted/node.rb', line 14 def add_edge to, weight # Does a O(n) check deleting existing edges @edges.delete_if{|n| n.to == to} @edges << Edge.new(self, to, weight) end |
#degree ⇒ Object
20 21 22 |
# File 'lib/graphos/weighted/node.rb', line 20 def degree @edges.inject(0){|sum,e| sum+e.weight } end |
#edge(to) ⇒ Object
32 33 34 |
# File 'lib/graphos/weighted/node.rb', line 32 def edge to @edges.lazy.select{|e| e.to.index == to}.first end |
#neighbor_of?(index) ⇒ Boolean
24 25 26 |
# File 'lib/graphos/weighted/node.rb', line 24 def neighbor_of? index @edges.any? {|node| node.to.index == index } end |
#neighbors ⇒ Object
28 29 30 |
# File 'lib/graphos/weighted/node.rb', line 28 def neighbors @edges.map{|edge| edge.to} end |