3 # tsort.rb - provides a module for topological sorting and strongly connected components.
8 # TSort implements topological sorting using Tarjan's algorithm for
9 # strongly connected components.
11 # TSort is designed to be able to be used with any object which can be
12 # interpreted as a directed graph.
14 # TSort requires two methods to interpret an object as a graph,
15 # tsort_each_node and tsort_each_child.
17 # * tsort_each_node is used to iterate for all nodes over a graph.
18 # * tsort_each_child is used to iterate for child nodes of a given node.
20 # The equality of nodes are defined by eql? and hash since
21 # TSort uses Hash internally.
25 # The following example demonstrates how to mix the TSort module into an
26 # existing class (in this case, Hash). Here, we're treating each key in
27 # the hash as a node in the graph, and so we simply alias the required
28 # #tsort_each_node method to Hash's #each_key method. For each key in the
29 # hash, the associated value is an array of the node's child nodes. This
30 # choice in turn leads to our implementation of the required #tsort_each_child
31 # method, which fetches the array of child nodes and then iterates over that
32 # array using the user-supplied block.
38 # alias tsort_each_node each_key
39 # def tsort_each_child(node, &block)
40 # fetch(node).each(&block)
44 # {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
47 # {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
48 # #=> [[4], [2, 3], [1]]
50 # == A More Realistic Example
52 # A very simple `make' like tool can be implemented as follows:
62 # def rule(outputs, inputs=[], &block)
63 # triple = [outputs, inputs, block]
64 # outputs.each {|f| @dep[f] = [triple]}
65 # @dep[triple] = inputs
69 # each_strongly_connected_component_from(target) {|ns|
71 # fs = ns.delete_if {|n| Array === n}
72 # raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
76 # outputs, inputs, block = n
77 # inputs_time = inputs.map {|f| File.mtime f}.max
79 # outputs_time = outputs.map {|f| File.mtime f}.min
80 # rescue Errno::ENOENT
83 # if outputs_time == nil ||
84 # inputs_time != nil && outputs_time <= inputs_time
85 # sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
92 # def tsort_each_child(node, &block)
93 # @dep[node].each(&block)
104 # m.rule(%w[t1]) { command 'date > t1' }
105 # m.rule(%w[t2]) { command 'date > t2' }
106 # m.rule(%w[t3]) { command 'date > t3' }
107 # m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
108 # m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
113 # * 'tsort.rb' is wrong name because this library uses
114 # Tarjan's algorithm for strongly connected components.
115 # Although 'strongly_connected_components.rb' is correct but too long.
119 # R. E. Tarjan, "Depth First Search and Linear Graph Algorithms",
120 # <em>SIAM Journal on Computing</em>, Vol. 1, No. 2, pp. 146-160, June 1972.
124 class Cyclic < StandardError
128 # Returns a topologically sorted array of nodes.
129 # The array is sorted from children to parents, i.e.
130 # the first element has no child and the last node has no parent.
132 # If there is a cycle, TSort::Cyclic is raised.
136 tsort_each {|element| result << element}
141 # The iterator version of the #tsort method.
142 # <tt><em>obj</em>.tsort_each</tt> is similar to <tt><em>obj</em>.tsort.each</tt>, but
143 # modification of _obj_ during the iteration may lead to unexpected results.
145 # #tsort_each returns +nil+.
146 # If there is a cycle, TSort::Cyclic is raised.
148 def tsort_each # :yields: node
149 each_strongly_connected_component {|component|
150 if component.size == 1
151 yield component.first
153 raise Cyclic.new("topological sort failed: #{component.inspect}")
159 # Returns strongly connected components as an array of arrays of nodes.
160 # The array is sorted from children to parents.
161 # Each elements of the array represents a strongly connected component.
163 def strongly_connected_components
165 each_strongly_connected_component {|component| result << component}
170 # The iterator version of the #strongly_connected_components method.
171 # <tt><em>obj</em>.each_strongly_connected_component</tt> is similar to
172 # <tt><em>obj</em>.strongly_connected_components.each</tt>, but
173 # modification of _obj_ during the iteration may lead to unexpected results.
176 # #each_strongly_connected_component returns +nil+.
178 def each_strongly_connected_component # :yields: nodes
181 tsort_each_node {|node|
182 unless id_map.include? node
183 each_strongly_connected_component_from(node, id_map, stack) {|c|
192 # Iterates over strongly connected component in the subgraph reachable from
195 # Return value is unspecified.
197 # #each_strongly_connected_component_from doesn't call #tsort_each_node.
199 def each_strongly_connected_component_from(node, id_map={}, stack=[]) # :yields: nodes
200 minimum_id = node_id = id_map[node] = id_map.size
201 stack_length = stack.length
204 tsort_each_child(node) {|child|
205 if id_map.include? child
206 child_id = id_map[child]
207 minimum_id = child_id if child_id && child_id < minimum_id
210 each_strongly_connected_component_from(child, id_map, stack) {|c|
213 minimum_id = sub_minimum_id if sub_minimum_id < minimum_id
217 if node_id == minimum_id
218 component = stack.slice!(stack_length .. -1)
219 component.each {|n| id_map[n] = nil}
227 # Should be implemented by a extended class.
229 # #tsort_each_node is used to iterate for all nodes over a graph.
231 def tsort_each_node # :yields: node
232 raise NotImplementedError.new
236 # Should be implemented by a extended class.
238 # #tsort_each_child is used to iterate for child nodes of _node_.
240 def tsort_each_child(node) # :yields: child
241 raise NotImplementedError.new
248 class TSortHash < Hash # :nodoc:
250 alias tsort_each_node each_key
251 def tsort_each_child(node, &block)
252 fetch(node).each(&block)
256 class TSortArray < Array # :nodoc:
258 alias tsort_each_node each_index
259 def tsort_each_child(node, &block)
260 fetch(node).each(&block)
264 class TSortTest < Test::Unit::TestCase # :nodoc:
266 h = TSortHash[{1=>[2, 3], 2=>[3], 3=>[]}]
267 assert_equal([3, 2, 1], h.tsort)
268 assert_equal([[3], [2], [1]], h.strongly_connected_components)
272 h = TSortHash[{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}]
273 assert_equal([[4], [2, 3], [1]],
274 h.strongly_connected_components.map {|nodes| nodes.sort})
275 assert_raise(TSort::Cyclic) { h.tsort }
279 a = TSortArray[[1], [0], [0], [2]]
280 assert_equal([[0, 1], [2], [3]],
281 a.strongly_connected_components.map {|nodes| nodes.sort})
283 a = TSortArray[[], [0]]
284 assert_equal([[0], [1]],
285 a.strongly_connected_components.map {|nodes| nodes.sort})