Class: Djikstra::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/djikstra/edge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, start, stop, weight, line = 0) ⇒ Edge

Returns a new instance of Edge.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/djikstra/edge.rb', line 5

def initialize(graph,start,stop,weight,line=0)
  raise ParseError, "Invalid edge on line #{line}" unless start && stop && weight
  
  @graph  = graph
  @start  = graph.find_node(start.to_s.upcase)
  @stop   = graph.find_node(stop.to_s.upcase)
  @weight = Integer(weight)
  
  @start.neighbor_of(@stop, @weight)
  @stop.neighbor_of(@start, @weight)
rescue TypeError, ArgumentError
  # Integer may raise this, so reraise as a ParseError
  raise ParseError, "Invalid weight on line #{line}"
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



3
4
5
# File 'lib/djikstra/edge.rb', line 3

def graph
  @graph
end

#startObject

Returns the value of attribute start.



3
4
5
# File 'lib/djikstra/edge.rb', line 3

def start
  @start
end

#stopObject

Returns the value of attribute stop.



3
4
5
# File 'lib/djikstra/edge.rb', line 3

def stop
  @stop
end

#weightObject

Returns the value of attribute weight.



3
4
5
# File 'lib/djikstra/edge.rb', line 3

def weight
  @weight
end