Class: Abuelo::Edge
- Inherits:
-
Object
- Object
- Abuelo::Edge
- Defined in:
- lib/abuelo/edge.rb
Overview
Class Edge provides a representation of an edge. An edge connects two nodes(start-node and end-node) and has a weight.
Instance Attribute Summary collapse
-
#node_1 ⇒ Abuelo::Node
readonly
Start-node.
-
#node_2 ⇒ Abuelo::Node
readonly
End-node.
-
#weight ⇒ Numeric
readonly
Weight.
Instance Method Summary collapse
-
#<=>(other_edge) ⇒ -1, ...
Comparison based on weight.
-
#==(other_edge) ⇒ Boolean
Equality check.
-
#initialize(node_1, node_2, weight = 0) ⇒ Edge
constructor
Initialiazises the edge with its two nodes and its weight.
-
#opposite ⇒ Abuelo::Edge
A new edge with same weight but reversed start- and end-node.
-
#to_s ⇒ String
Human readable representation of edge.
Constructor Details
#initialize(node_1, node_2, weight = 0) ⇒ Edge
Initialiazises the edge with its two nodes and its weight.
26 27 28 29 30 |
# File 'lib/abuelo/edge.rb', line 26 def initialize(node_1, node_2, weight = 0) @node_1 = node_1 @node_2 = node_2 @weight = weight end |
Instance Attribute Details
#node_1 ⇒ Abuelo::Node (readonly)
Returns start-node.
11 12 13 |
# File 'lib/abuelo/edge.rb', line 11 def node_1 @node_1 end |
#node_2 ⇒ Abuelo::Node (readonly)
Returns end-node.
14 15 16 |
# File 'lib/abuelo/edge.rb', line 14 def node_2 @node_2 end |
#weight ⇒ Numeric (readonly)
Returns weight.
17 18 19 |
# File 'lib/abuelo/edge.rb', line 17 def weight @weight end |
Instance Method Details
#<=>(other_edge) ⇒ -1, ...
Comparison based on weight.
46 47 48 |
# File 'lib/abuelo/edge.rb', line 46 def <=>(other_edge) self.weight <=> other_edge.weight end |
#==(other_edge) ⇒ Boolean
Equality check.
57 58 59 60 61 |
# File 'lib/abuelo/edge.rb', line 57 def ==(other_edge) self.node_1 == other_edge.node_1 && self.node_2 == other_edge.node_2 && self.weight == other_edge.weight end |
#opposite ⇒ Abuelo::Edge
Returns a new edge with same weight but reversed start- and end-node.
35 36 37 |
# File 'lib/abuelo/edge.rb', line 35 def opposite Abuelo::Edge.new(node_2, node_1, weight) end |
#to_s ⇒ String
Returns human readable representation of edge.
66 67 68 |
# File 'lib/abuelo/edge.rb', line 66 def to_s "#{node_1.to_s} -> #{node_2.to_s} with weight #{weight}" end |