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.

Parameters:

  • node_1 (Abuelo::Node)

    start-node

  • node_2 (Abuelo::Node)

    end-node

  • weight (Numeric) (defaults to: 1)

    of the edge



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)

Returns start-node.

Returns:



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

def node_1
  @node_1
end

#node_2Abuelo::Node (readonly)

Returns end-node.

Returns:



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

def node_2
  @node_2
end

#weightNumeric (readonly)

Returns weight.

Returns:

  • (Numeric)

    weight



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

def weight
  @weight
end

Instance Method Details

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

Comparison based on weight.

Parameters:

Returns:

  • (-1, 0, +1)


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

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

#==(other) ⇒ Boolean

Equality check.

Parameters:

Returns:

  • (Boolean)

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



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

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.



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

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

#to_sString

Returns human readable representation of edge.

Returns:

  • (String)

    human readable representation of edge



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

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