Class: Vertex

Inherits:
Object
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/Graph/vertex.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = Hash.new) ⇒ Vertex

Returns a new instance of Vertex.



12
13
14
15
16
17
18
# 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

Instance Attribute Details

#connectionsObject

Connected with



7
8
9
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 7

def connections
  @connections
end

#edgesObject

Connected by



4
5
6
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 4

def edges
  @edges
end

#valueObject

Contains



10
11
12
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 10

def value
  @value
end

Instance Method Details

#connect(other_vertex, edge) ⇒ Object

Make an edge between this vertex and another



21
22
23
24
25
26
27
28
29
30
# 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

#inspectObject



38
39
40
41
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 38

def inspect
  description = "Vertex"
  description += ": empty" if @value.empty?
end

#is_connected?(other_vertex) ⇒ Boolean

Returns if another vertex is “connected” to this one

Returns:

  • (Boolean)


33
34
35
36
# 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