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 = 0) ⇒ Edge

Initialiazises the edge with its two nodes and its weight.

Parameters:

  • node_1 (Abuelo::Node)

    start-node

  • node_2 (Abuelo::Node)

    end-node

  • weight (Numeric) (defaults to: 0)

    of the edge



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_1Abuelo::Node (readonly)

Returns start-node.

Returns:



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

def node_1
  @node_1
end

#node_2Abuelo::Node (readonly)

Returns end-node.

Returns:



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

def node_2
  @node_2
end

#weightNumeric (readonly)

Returns weight.

Returns:

  • (Numeric)

    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.

Parameters:

Returns:

  • (-1, 0, +1 or ni)


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

def <=>(other_edge)
  self.weight <=> other_edge.weight
end

#==(other_edge) ⇒ Boolean

Equality check.

Parameters:

Returns:

  • (Boolean)

    true if start-, end-node and weight of both edges are equal



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

#oppositeAbuelo::Edge

Returns a new edge with same weight but reversed start- and end-node.

Returns:

  • (Abuelo::Edge)

    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_sString

Returns human readable representation of edge.

Returns:

  • (String)

    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