Module: Connected::Vertex

Included in:
GenericNode
Defined in:
lib/connected/vertex.rb

Overview

Vertices are based on a mixin

Instance Method Summary collapse

Instance Method Details

#connection_to(other) ⇒ Object

Retrieves the Connection object responsible for connecting to a Node



35
36
37
# File 'lib/connected/vertex.rb', line 35

def connection_to(other)
  connections.select { |c| c.to == other }.min_by(&:metric)
end

#connectionsObject



6
7
8
9
# File 'lib/connected/vertex.rb', line 6

def connections
  # Expect classes to describe how to find connections
  raise "#connections() MUST be implemented on #{self.class.name}"
end

#eccentricityObject



39
40
41
42
43
# File 'lib/connected/vertex.rb', line 39

def eccentricity
  reachable_vertices.map do |r|
    self == r ? nil : Path.find(from: self, to: r).cost
  end.compact.max
end

#edgesObject



11
12
13
# File 'lib/connected/vertex.rb', line 11

def edges
  connections
end

#graphObject



15
16
17
# File 'lib/connected/vertex.rb', line 15

def graph
  @graph || subtree
end

#neighborhood(closed: false) ⇒ Object

The neighborhood of a Vertex is a Graph of all neighbors and any connections they have to each other.



27
28
29
30
31
32
# File 'lib/connected/vertex.rb', line 27

def neighborhood(closed: false)
  vertices = neighbors
  vertices << self if closed
  n_edges = vertices.map(&:connections).flatten.uniq.select { |c| vertices.include?(c.to) }
  GenericGraph.new(vertices:, edges: n_edges)
end

#neighborsObject

A shortcut for retrieving this node’s neighbors



20
21
22
# File 'lib/connected/vertex.rb', line 20

def neighbors
  connections.map(&:to).uniq
end

#reachable_from?(other) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/connected/vertex.rb', line 49

def reachable_from?(other)
  other.reachable_vertices.include?(self)
end

#reachable_verticesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/connected/vertex.rb', line 53

def reachable_vertices
  rverts = neighbors + [self]

  queue = (neighbors.map(&:neighbors).flatten.uniq - neighbors)

  until queue.empty?
    this_vert = queue.pop
    next if this_vert == self

    rverts << this_vert
    this_vert.neighbors.each do |v|
      queue << v unless rverts.include?(v)
    end
  end

  rverts.uniq
end

#reaches?(other) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/connected/vertex.rb', line 45

def reaches?(other)
  reachable_vertices.include?(other)
end

#subtreeObject

A subtree is a subgraph including all edges and vertices reachable from a vertex



72
73
74
# File 'lib/connected/vertex.rb', line 72

def subtree
  GenericGraph.new(vertices: reachable_vertices)
end