Class: Doodl::DirectedGraph

Inherits:
Graph
  • Object
show all
Defined in:
lib/graph.rb

Direct Known Subclasses

SimpleBidirectionalGraph

Instance Attribute Summary

Attributes inherited from Graph

#nodes

Instance Method Summary collapse

Methods inherited from Graph

#add_node, #attach_edge_data, #attach_node_data, #contains_edge?, #contains_node?, #detach_edge_data, #detach_node_data, #each_node_passing, #initialize, #num_edges, #source, #target, #to_s

Methods included from GraphGenerator

#gen_binary_tree, #gen_connected_graph, #gen_linear_graph, #gen_mesh_graph, #gen_random_graph, #gen_ring_graph

Constructor Details

This class inherits a constructor from Doodl::Graph

Instance Method Details

#add_edge(source, target, update = false) ⇒ Object

Raises:

  • (ArgumentError)


200
201
202
203
204
205
206
207
208
# File 'lib/graph.rb', line 200

def add_edge(source, target, update = false)
  raise ArgumentError, "Source/Target must be member of graph" unless (@nodes.include?(source) and @nodes.include?(target))
  edge = DirectedEdge.new(source, target)
  raise ArgumentError if source.includes_out_edge?(edge)
  source.add_out_edge(edge)
  changed if update
  notify_observers(self)
  return edge
end

#del_edge(edge, update = false) ⇒ Object



210
211
212
213
214
# File 'lib/graph.rb', line 210

def del_edge(edge, update = false)
  edge.source.del_out_edge(edge)
  changed if update
  notify_observers(self)
end

#del_node(node, update = false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/graph.rb', line 188

def del_node(node, update = false)
  each_edge do |edge|
    if (edge.source == node or edge.target == node)
      source = edge.source
      source.del_out_edge(edge)
    end
  end
  @nodes.delete(node)
  changed if update
  notify_observers(self)
end

#directed?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/graph.rb', line 165

def directed?
  true
end

#each_adjacent(node) ⇒ Object



235
236
237
# File 'lib/graph.rb', line 235

def each_adjacent(node)
  each_adjacent_edge(node) { |edge| yield(target(edge), edge) }
end

#each_adjacent_edge(node) ⇒ Object



227
228
229
# File 'lib/graph.rb', line 227

def each_adjacent_edge(node)
  node.each_out_edge { |edge| yield(edge) }
end

#each_adjacent_node(node) ⇒ Object



231
232
233
# File 'lib/graph.rb', line 231

def each_adjacent_node(node)
  each_adjacent_edge(node) { |edge| yield(target(edge)) }
end

#each_edgeObject



216
217
218
219
220
# File 'lib/graph.rb', line 216

def each_edge
  each_node do |source|
    source.each_out_edge { |edge| yield(edge) }
  end
end

#gen_edge(source, target) ⇒ Object



179
180
181
# File 'lib/graph.rb', line 179

def gen_edge(source, target)
  DirectedEdge.new(source, target)
end

#gen_nodeObject



175
176
177
# File 'lib/graph.rb', line 175

def gen_node
  DirectedNode.new
end

#get_edge(source, target) ⇒ Object

Raises:

  • (ArgumentError)


183
184
185
186
# File 'lib/graph.rb', line 183

def get_edge(source, target)
  raise ArgumentError, "Source and Target must be member of Graph" unless (@nodes.include?(source) and @nodes.include?(target))
  source.get_edge(target)
end

#includes_edge?(edge) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


169
170
171
172
# File 'lib/graph.rb', line 169

def includes_edge?(edge)
  raise ArgumentError unless edge.respond_to?(:source) and edge.respond_to?(:target)
  contains_node?(edge.source) and contains_node?(edge.target) and edge.source.includes_out_edge?(edge)
end

#out_degree(node) ⇒ Object

Incidence



223
224
225
# File 'lib/graph.rb', line 223

def out_degree(node)
  node.out_degree
end