class RGL::ImplicitGraph
An ImplicitGraph
provides a handy way to define graphs on the fly, using two blocks for the two iterators defining a graph. Other examples are given by the methods {#vertices_filtered_by} and {#edges_filtered_by}, which can be applied to any graph.
@example
# A directed cyclic graph, with five vertices can be created as follows: g = RGL::ImplicitGraph.new do |g| g.vertex_iterator { |b| 0.upto(4,&b) } g.adjacent_iterator { |x, b| b.call((x+1)%5) } g.directed = true end g.to_s # => "(0-1)(1-2)(2-3)(3-4)(4-0)"
Constants
- EMPTY_NEIGHBOR_ITERATOR
- EMPTY_VERTEX_ITERATOR
Attributes
Public Class Methods
Create a new {ImplicitGraph}, which is empty by default. The caller should configure the graph using vertex and neighbor iterators. If the graph is directed, the client should set +@directed+ to true. The default value for +@directed+ is false.
# File lib/rgl/implicit.rb 40 def initialize 41 @directed = false 42 @vertex_iterator = EMPTY_VERTEX_ITERATOR 43 @adjacent_iterator = EMPTY_NEIGHBOR_ITERATOR 44 yield self if block_given? # Let client overwrite defaults. 45 end
Public Instance Methods
Sets the adjacent_iterator
to block, which must be a block of two parameters:
The first parameter is the vertex the neighbors of which are to be traversed. The second is the block which will be called for each neighbor of this vertex.
# File lib/rgl/implicit.rb 86 def adjacent_iterator(&block) 87 @adjacent_iterator = block 88 end
Returns the value of +@directed+.
# File lib/rgl/implicit.rb 49 def directed? 50 @directed 51 end
# File lib/rgl/implicit.rb 57 def each_adjacent(v, &block) 58 @adjacent_iterator.call(v, block) 59 end
RGL::Graph#each_edge
# File lib/rgl/implicit.rb 61 def each_edge(&block) 62 if defined? @edge_iterator 63 @edge_iterator.call(block) 64 else 65 super # use default implementation 66 end 67 end
# File lib/rgl/implicit.rb 53 def each_vertex(&block) 54 @vertex_iterator.call(block) 55 end
Sets the edge_iterator
to block, which must be a block of two parameters: The first parameter is the source of the edges; the second is the target of the edge.
# File lib/rgl/implicit.rb 94 def edge_iterator(&block) 95 @edge_iterator = block 96 end
Sets the vertex_iterator
to block, which must be a block of one parameter which again is the block called by {#each_vertex}.
# File lib/rgl/implicit.rb 73 def vertex_iterator(&block) 74 @vertex_iterator = block 75 end