class Graphos::Weighted::Node

This class represents a node in a weighted graph

Attributes

index[R]

Public Class Methods

new(index, size=0) click to toggle source
# File lib/graphos/weighted/node.rb, line 9
def initialize index, size=0
  @index = index
  @edges = Array.new(size||0)
  @edge_count = 0
end

Public Instance Methods

add_edge(to, weight) click to toggle source
# File lib/graphos/weighted/node.rb, line 20
def add_edge to, weight
  # Does a O(n) check deleting existing edges
  current_idx = edges.index{|n| n.to == to}
  if current_idx
    @edges[current_idx] = Edge.new(self,to,weight)
  else
    edge = Edge.new(self, to, weight)
    if(@edges.size > @edge_count)
      @edges[@edge_count] = edge
    else
      @edges << edge
    end
    @edge_count += 1
  end
end
degree() click to toggle source
# File lib/graphos/weighted/node.rb, line 36
def degree
  edges.inject(0){|sum,e| sum+e.weight }
end
edge(to) click to toggle source
# File lib/graphos/weighted/node.rb, line 48
def edge to
  edges.lazy.select{|e| e.to.index == to}.first
end
edges() click to toggle source
# File lib/graphos/weighted/node.rb, line 15
def edges
  return [] if @edge_count == 0
  @edges[0..@edge_count-1]
end
neighbor_of?(index) click to toggle source
# File lib/graphos/weighted/node.rb, line 40
def neighbor_of? index
  edges.any? {|node| node.to.index == index }
end
neighbors() click to toggle source
# File lib/graphos/weighted/node.rb, line 44
def neighbors
  edges.map{|edge| edge.to}
end