Skip to content
instance method

TSort#each_strongly_connected_component

() { (::Array[Node]) -> void } -> void
() -> ::Enumerator[::Array[Node], void]
each_strongly_connected_component() { |nodes| ... }

The iterator version of the strongly_connected_components method. <em>obj</em>.each_strongly_connected_component is similar to <em>obj</em>.strongly_connected_components.each, but modification of obj during the iteration may lead to unexpected results.

each_strongly_connected_component returns nil.

Example
class G
  include TSort
  def initialize(g)
    @g = g
  end
  def tsort_each_child(n, &b) @g[n].each(&b) end
  def tsort_each_node(&b) @g.each_key(&b) end
end

graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
graph.each_strongly_connected_component {|scc| p scc }
#=> [4]
#   [2]
#   [3]
#   [1]

graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
graph.each_strongly_connected_component {|scc| p scc }
#=> [4]
#   [2, 3]
#   [1]