Method: RGL::Graph#vertices_filtered_by

Defined in:
lib/rgl/implicit.rb

#vertices_filtered_by(&filter) ⇒ ImplicitGraph

Returns a new ImplicitGraph which has as vertices all vertices of the receiver which satisfy the predicate filter.

The methods provides similar functionality as BGLs Filtered Graph

Examples:


def complete (n)
  set = n.integer? ? (1..n) : n
  RGL::ImplicitGraph.new do |g|
    g.vertex_iterator { |b| set.each(&b) }
    g.adjacent_iterator do |x, b|
      set.each { |y| b.call(y) unless x == y }
    end
  end
end

complete(4).to_s # => "(1=2)(1=3)(1=4)(2=3)(2=4)(3=4)"
complete(4).vertices_filtered_by { |v| v != 4 }.to_s # => "(1=2)(1=3)(2=3)"

Returns:



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rgl/implicit.rb', line 125

def vertices_filtered_by(&filter)
  implicit_graph do |g|
    g.vertex_iterator do |b|
      self.each_vertex { |v| b.call(v) if filter.call(v) }
    end

    g.adjacent_iterator do |v, b|
      self.each_adjacent(v) { |u| b.call(u) if filter.call(u) }
    end
  end
end