module Bundler::TSort

def tsort


p graph.tsort # raises Bundler::TSort::Cyclic
graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})

p graph.tsort #=> [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

If there is a cycle, Bundler::TSort::Cyclic is raised.

the first element has no child and the last node has no parent.
The array is sorted from children to parents, i.e.
Returns a topologically sorted array of nodes.
def tsort
  each_node = method(:tsort_each_node)
  each_child = method(:tsort_each_child)
  Bundler::TSort.tsort(each_node, each_child)
end