Class: Abuelo::Edge

Inherits:
Object
  • Object
show all
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.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_1, node_2, weight = 1) ⇒ Edge

Initialiazises the edge with its two nodes and its weight.



25
26
27
28
29
# File 'lib/abuelo/edge.rb', line 25

def initialize(node_1, node_2, weight = 1)
  @node_1 = node_1
  @node_2 = node_2
  @weight = weight
end

Instance Attribute Details

#node_1Abuelo::Node (readonly)



10
11
12
# File 'lib/abuelo/edge.rb', line 10

def node_1
  @node_1
end

#node_2Abuelo::Node (readonly)



13
14
15
# File 'lib/abuelo/edge.rb', line 13

def node_2
  @node_2
end

#weightNumeric (readonly)



16
17
18
# File 'lib/abuelo/edge.rb', line 16

def weight
  @weight
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Comparison based on weight.



45
46
47
# File 'lib/abuelo/edge.rb', line 45

def <=>(other)
  weight <=> other.weight
end

#==(other) ⇒ Boolean

Equality check.



56
57
58
59
60
# File 'lib/abuelo/edge.rb', line 56

def ==(other)
  node_1 == other.node_1 &&
    node_2 == other.node_2 &&
    weight == other.weight
end

#symmetricAbuelo::Edge



34
35
36
# File 'lib/abuelo/edge.rb', line 34

def symmetric
  Abuelo::Edge.new(node_2, node_1, weight)
end

#to_sString



65
66
67
# File 'lib/abuelo/edge.rb', line 65

def to_s
  "#{node_1} -> #{node_2} with weight #{weight}"
end