Class: Zadt::Vertex

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = Hash.new) ⇒ 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

Class Method Details

.helpObject



56
57
58
59
60
61
62
63
64
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 56

def self.help
  puts "Here are the functions for Vertex:"
  puts "#connect(other_vertex)"
  puts "#is_connected?(other_vertex)"
  puts "#make_connection(v1,v2)"
  puts "#break_connection(v1,v2)"
  puts "#find_connection(v1,v2)"
  puts "#is_connected?(v1,v2)"
end

.methodsObject



20
21
22
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 20

def self.methods
  self.help
end

Instance Method Details

#connect(other_vertex) ⇒ Object

Make an edge between this vertex and another



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 25

def connect(other_vertex)
  return nil if !other_vertex.is_a?(Vertex) || other_vertex == self
  raise "already connected" if is_connected?(other_vertex)

  edge = Edge.new(self, other_vertex)
  # Store connection info in this vertex
  @edges << edge
  @connections << other_vertex
  # Store connection info in other vertex
  other_vertex.store_connection_info(self, edge)
  edge
end

#helpObject



66
67
68
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 66

def help
  Vertex.methods
end

#inspectObject



43
44
45
46
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 43

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

#is_connected?(other_vertex) ⇒ Boolean

Returns if another vertex is “connected” to this one



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

def is_connected?(other_vertex)
  @connections.include?(other_vertex)
end

#methodsObject



70
71
72
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 70

def methods
  help
end

#store_connection_info(vertex, edge) ⇒ Object

Used to store connection info in the second vertex

involved in a connection

Must needs be public, since it’s called by a different vertex



51
52
53
54
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 51

def store_connection_info(vertex, edge)
  @edges << edge
  @connections << vertex
end