Class: Graphos::Weighted::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/graphos/weighted/node.rb

Overview

This class represents a node in a weighted graph

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#edgesObject (readonly)

Returns the value of attribute edges.



8
9
10
# File 'lib/graphos/weighted/node.rb', line 8

def edges
  @edges
end

#indexObject (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

#degreeObject



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

Returns:

  • (Boolean)


24
25
26
# File 'lib/graphos/weighted/node.rb', line 24

def neighbor_of? index
  @edges.any? {|node| node.to.index == index }
end

#neighborsObject



28
29
30
# File 'lib/graphos/weighted/node.rb', line 28

def neighbors
  @edges.map{|edge| edge.to}
end