Class: Zadt::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/zadt/AbstractDataTypes/Graph/graph.rb,
lib/zadt/HelpModules/Functionality/Graph/graph.rb

Direct Known Subclasses

DiGraph, FaceGraph

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

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

#edgesObject (readonly)

Which are connected by



9
10
11
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 9

def edges
  @edges
end

#valueObject

Contains



12
13
14
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 12

def value
  @value
end

#verticesObject (readonly)

Made up of



6
7
8
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 6

def vertices
  @vertices
end

Class Method Details

.helpObject



7
8
9
# File 'lib/zadt/HelpModules/Functionality/Graph/graph.rb', line 7

def self.help
  Graph.show_help_message
end

Instance Method Details

#add_vertexObject

Add a vertex



24
25
26
27
28
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 24

def add_vertex
  vertex = Vertex.new
  @vertices << vertex
  vertex
end

#break_connection(v1, v2) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 75

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



61
62
63
64
65
66
67
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 61

def find_connection(v1, v2)
  raise "not a vertex" unless v1.is_a?(Vertex) && v2.is_a?(Vertex)
  raise "Vertices not connected" if !is_connected?(v1, v2)
  connection = v1.edges.select {|edge| edge.connection.include?(v2)}
  raise "Error finding connection" if connection.length > 1
  connection.first
end

#helpObject



3
4
5
# File 'lib/zadt/HelpModules/Functionality/Graph/graph.rb', line 3

def help
  Graph.help
end

#is_connected?(v1, v2) ⇒ Boolean

Returns whether two vertices are connected

Returns:

  • (Boolean)


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

def is_connected?(v1, v2)
  raise "not a vertex" unless v1.is_a?(Vertex) && v2.is_a?(Vertex)
  v1.connections.include?(v2)
end

#make_connection(v1, v2) ⇒ Object

Make an edge between two vertices



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 46

def make_connection(v1, v2)
  raise "not a vertex" unless v1.is_a?(Vertex) && v2.is_a?(Vertex)
  raise "already connected" if is_connected?(v1, v2)
  # Make new edge
  edge = Edge.new(v1, v2)
  # Connect the two using the vertex method "connect"
  v1.connect(v2, edge)
  v2.connect(v1, edge)

  # Add to edge catalog
  @edges << edge
  edge
end

#remove_vertex(vertex) ⇒ Object

Remove a vertex



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zadt/AbstractDataTypes/Graph/graph.rb', line 31

def remove_vertex(vertex)
  # The vertex must exist
  raise "not a vertex" unless vertex.is_a?(Vertex)
  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