class Vertex

Attributes

connections[RW]

Connected with

edges[RW]

Connected by

value[RW]

Contains

Public Class Methods

new(value = Hash.new) click to toggle source
# File lib/zadt/AbstractDataTypes/Graph/vertex.rb, line 12
def initialize(value = Hash.new)
  # List of edges attached to vertex
  @edges = []
  # List of vertices "connected" to this one
  @connections = []
  @value = value
end

Public Instance Methods

connect(other_vertex, edge) click to toggle source

Make an edge between this vertex and another

# File lib/zadt/AbstractDataTypes/Graph/vertex.rb, line 21
def connect(other_vertex, edge)
  raise "not a vertex" unless other_vertex.is_a?(Vertex)
  raise "cannot connect vertex to self" if other_vertex == self
  raise "not an edge" unless edge.is_a?(Edge)
  raise "already connected" if is_connected?(other_vertex)
  # Store connection info in this vertex
  @edges << edge
  @connections << other_vertex
  edge
end
inspect() click to toggle source
# File lib/zadt/AbstractDataTypes/Graph/vertex.rb, line 38
def inspect
  description = "Vertex"
  description += ": empty" if @value.empty?
end
is_connected?(other_vertex) click to toggle source

Returns if another vertex is “connected” to this one

# File lib/zadt/AbstractDataTypes/Graph/vertex.rb, line 33
def is_connected?(other_vertex)
  raise "not a vertex" unless other_vertex.is_a?(Vertex)
  @connections.include?(other_vertex)
end