Created
May 1, 2016 16:29
-
-
Save anonymous/bbf1bc9c77f222e3718b599323514782 to your computer and use it in GitHub Desktop.
tsort_example_2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "tsort" | |
$dep={ | |
:M=>[:S,:B], | |
:R=>[:M,:S,:A], | |
:S=>[:I,:J,:M2,:T,:T3], | |
:A=>[], #later change to :A=>[:A] | |
:B=>[], | |
:C=>[:T2], | |
:C2=>[:M2,:R2], | |
:I=>[], | |
:J=>[], | |
:M2=>[], | |
:R3=>[:S,:M,:R], | |
:R2=>[], | |
:R4=>[], | |
:R5=>[:R4], | |
:S2=>[:C2,:C,:J,:R4,:R5], | |
:T=>[], | |
:T2=>[], | |
:T3=>[:T], | |
} | |
def top_dep | |
yield :S2 | |
yield :R3 | |
end | |
top_dep {|i| puts i} | |
top_dep {|i| puts i} | |
puts "-------------" | |
def get_dep(nd,&block) | |
$dep[nd].each(&block) | |
end | |
def dep_each_node(&block) | |
$dep.each_key(&block) | |
end | |
def dep_each_child(node, &block) | |
$dep[node].each(&block) | |
end | |
puts TSort.tsort(method(:top_dep),method(:get_dep)) | |
puts "......" | |
TSort.strongly_connected_components(method(:dep_each_node ),method(:dep_each_child)).each{|c| | |
puts c.inspect} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment