module Bundler::TSort
def strongly_connected_components
p graph.strongly_connected_components #=> [[4], [2, 3], [1]]
graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
p graph.strongly_connected_components #=> [[4], [2], [3], [1]]
graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
end
def tsort_each_node(&b) @g.each_key(&b) end
def tsort_each_child(n, &b) @g[n].each(&b) end
end
@g = g
def initialize(g)
include Bundler::TSort
class G
Each elements of the array represents a strongly connected component.
The array is sorted from children to parents.
Returns strongly connected components as an array of arrays of nodes.
def strongly_connected_components each_node = method(:tsort_each_node) each_child = method(:tsort_each_child) Bundler::TSort.strongly_connected_components(each_node, each_child) end