Class: Zadt::Graph
- Inherits:
-
Object
- Object
- Zadt::Graph
- Defined in:
- lib/zadt/AbstractDataTypes/Graph/graph.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Which are connected by.
-
#value ⇒ Object
Contains.
-
#vertices ⇒ Object
readonly
Made up of.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_vertex ⇒ Object
Add a vertex.
- #break_connection(v1, v2) ⇒ Object
-
#find_connection(v1, v2) ⇒ Object
Find the edge connecting two vertices.
- #help ⇒ Object
-
#initialize ⇒ Graph
constructor
init_v allows for initial vertices (not generally used).
-
#is_connected?(v1, v2) ⇒ Boolean
Returns whether two vertices are connected.
-
#make_connection(v1, v2) ⇒ Object
Make an edge between two vertices.
- #methods ⇒ Object
-
#remove_vertex(vertex) ⇒ Object
Remove a vertex.
Constructor Details
#initialize ⇒ Graph
init_v allows for initial vertices (not generally used)
15 16 17 18 19 20 21 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 15 def initialize #@vertices is ALL vertices on the graph @vertices = [] #@edges is ALL edges on the graph @edges = [] @value = Hash.new end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Which are connected by
9 10 11 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 9 def edges @edges end |
#value ⇒ Object
Contains
12 13 14 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 12 def value @value end |
#vertices ⇒ Object (readonly)
Made up of
6 7 8 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 6 def vertices @vertices end |
Class Method Details
.help ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 97 def self.help puts "Here are the functions for Graph:" puts "#add_vertex" puts "#remove_vertex(vertex)" puts "#make_connection(v1,v2), adds an edge between two vertices" puts "#break_connection(v1,v2)" puts "#find_connection(v1,v2), returns edge connecting two given vertices" puts "#is_connected?(v1,v2)" end |
.methods ⇒ Object
23 24 25 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 23 def self.methods self.help end |
Instance Method Details
#add_vertex ⇒ Object
Add a vertex
28 29 30 31 32 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 28 def add_vertex vertex = Vertex.new @vertices << vertex vertex end |
#break_connection(v1, v2) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 70 def break_connection(v1, v2) raise "First vertex does not exist" if !v1 raise "Second vertex does not exist" if !v2 if is_connected?(v1, v2) # Find edge edge = find_connection(v1, v2) # Remove edge from edges catalog @edges.delete(edge) #Remove edge from vertices v1.edges.delete(edge) v2.edges.delete(edge) v1.connections.delete(v2) v2.connections.delete(v1) else raise "Vertices are not connected" end end |
#find_connection(v1, v2) ⇒ Object
Find the edge connecting two vertices
60 61 62 63 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 60 def find_connection(v1, v2) connection = v1.edges.select {|edge| edge.connection.include?(v2)} connection.first end |
#help ⇒ Object
89 90 91 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 89 def help Graph.help end |
#is_connected?(v1, v2) ⇒ Boolean
Returns whether two vertices are connected
66 67 68 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 66 def is_connected?(v1, v2) v1.connections.include?(v2) end |
#make_connection(v1, v2) ⇒ Object
Make an edge between two vertices
49 50 51 52 53 54 55 56 57 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 49 def make_connection(v1, v2) raise "already connected" if is_connected?(v1, v2) # Connect the two using the vertex method "connect" edge = v1.connect(v2) # Add to edge catalog @edges << edge edge end |
#methods ⇒ Object
93 94 95 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 93 def methods help end |
#remove_vertex(vertex) ⇒ Object
Remove a vertex
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 35 def remove_vertex(vertex) # The vertex must exist if !vertex raise "Vertex does not exist" # The vertex must not be connected to anything elsif !vertex.connections.empty? raise "Vertex has edges. Break them first." # If it exists and isn't connected, delete it else @vertices.delete(vertex) end end |