Class: Vertex
- Inherits:
-
Object
- Object
- Vertex
- Defined in:
- lib/zadt/AbstractDataTypes/Graph/vertex.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
Connected with.
-
#edges ⇒ Object
Connected by.
-
#value ⇒ Object
Contains.
Instance Method Summary collapse
-
#connect(other_vertex, edge) ⇒ Object
Make an edge between this vertex and another.
-
#initialize(value = Hash.new) ⇒ Vertex
constructor
A new instance of Vertex.
- #inspect ⇒ Object
-
#is_connected?(other_vertex) ⇒ Boolean
Returns if another vertex is “connected” to this one.
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
#connections ⇒ Object
Connected with
7 8 9 |
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 7 def connections @connections end |
#edges ⇒ Object
Connected by
4 5 6 |
# File 'lib/zadt/AbstractDataTypes/Graph/vertex.rb', line 4 def edges @edges end |
#value ⇒ Object
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 |
#inspect ⇒ Object
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
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 |