Debian Codegraph @

vendor/mruby/mrbgems/mruby-tsort/mrblib/tsort.rb

raw
1 # Copyright (C) 2017 Kouhei Sutou <[email protected]>
2 # Copyright (C) 1993-2013 Yukihiro Matsumoto <[email protected]>
3
4 # This file is based on tsort.rb in CRuby.
5
6 #--
7 # tsort.rb - provides a module for topological sorting and strongly connected components.
8 #++
9 #
10
11 #
12 # TSort implements topological sorting using Tarjan's algorithm for
13 # strongly connected components.
14 #
15 # TSort is designed to be able to be used with any object which can be
16 # interpreted as a directed graph.
17 #
18 # TSort requires two methods to interpret an object as a graph,
19 # tsort_each_node and tsort_each_child.
20 #
21 # * tsort_each_node is used to iterate for all nodes over a graph.
22 # * tsort_each_child is used to iterate for child nodes of a given node.
23 #
24 # The equality of nodes are defined by eql? and hash since
25 # TSort uses Hash internally.
26 #
27 # == A Simple Example
28 #
29 # The following example demonstrates how to mix the TSort module into an
30 # existing class (in this case, Hash). Here, we're treating each key in
31 # the hash as a node in the graph, and so we simply alias the required
32 # #tsort_each_node method to Hash's #each_key method. For each key in the
33 # hash, the associated value is an array of the node's child nodes. This
34 # choice in turn leads to our implementation of the required #tsort_each_child
35 # method, which fetches the array of child nodes and then iterates over that
36 # array using the user-supplied block.
37 #
38 # require 'tsort'
39 #
40 # class Hash
41 # include TSort
42 # alias tsort_each_node each_key
43 # def tsort_each_child(node, &block)
44 # fetch(node).each(&block)
45 # end
46 # end
47 #
48 # {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
49 # #=> [3, 2, 1, 4]
50 #
51 # {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
52 # #=> [[4], [2, 3], [1]]
53 #
54 # == A More Realistic Example
55 #
56 # A very simple `make' like tool can be implemented as follows:
57 #
58 # require 'tsort'
59 #
60 # class Make
61 # def initialize
62 # @dep = {}
63 # @dep.default = []
64 # end
65 #
66 # def rule(outputs, inputs=[], &block)
67 # triple = [outputs, inputs, block]
68 # outputs.each {|f| @dep[f] = [triple]}
69 # @dep[triple] = inputs
70 # end
71 #
72 # def build(target)
73 # each_strongly_connected_component_from(target) {|ns|
74 # if ns.length != 1
75 # fs = ns.delete_if {|n| Array === n}
76 # raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
77 # end
78 # n = ns.first
79 # if Array === n
80 # outputs, inputs, block = n
81 # inputs_time = inputs.map {|f| File.mtime f}.max
82 # begin
83 # outputs_time = outputs.map {|f| File.mtime f}.min
84 # rescue Errno::ENOENT
85 # outputs_time = nil
86 # end
87 # if outputs_time == nil ||
88 # inputs_time != nil && outputs_time <= inputs_time
89 # sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
90 # block.call
91 # end
92 # end
93 # }
94 # end
95 #
96 # def tsort_each_child(node, &block)
97 # @dep[node].each(&block)
98 # end
99 # include TSort
100 # end
101 #
102 # def command(arg)
103 # print arg, "\n"
104 # system arg
105 # end
106 #
107 # m = Make.new
108 # m.rule(%w[t1]) { command 'date > t1' }
109 # m.rule(%w[t2]) { command 'date > t2' }
110 # m.rule(%w[t3]) { command 'date > t3' }
111 # m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
112 # m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
113 # m.build('t5')
114 #
115 # == Bugs
116 #
117 # * 'tsort.rb' is wrong name because this library uses
118 # Tarjan's algorithm for strongly connected components.
119 # Although 'strongly_connected_components.rb' is correct but too long.
120 #
121 # == References
122 #
123 # R. E. Tarjan, "Depth First Search and Linear Graph Algorithms",
124 # <em>SIAM Journal on Computing</em>, Vol. 1, No. 2, pp. 146-160, June 1972.
125 #
126
127 module TSort
128 class Cyclic < StandardError
129 end
130
131 # Returns a topologically sorted array of nodes.
132 # The array is sorted from children to parents, i.e.
133 # the first element has no child and the last node has no parent.
134 #
135 # If there is a cycle, TSort::Cyclic is raised.
136 #
137 # class G
138 # include TSort
139 # def initialize(g)
140 # @g = g
141 # end
142 # def tsort_each_child(n, &b) @g[n].each(&b) end
143 # def tsort_each_node(&b) @g.each_key(&b) end
144 # end
145 #
146 # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
147 # p graph.tsort #=> [4, 2, 3, 1]
148 #
149 # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
150 # p graph.tsort # raises TSort::Cyclic
151 #
152 def tsort
153 each_node = method(:tsort_each_node)
154 each_child = method(:tsort_each_child)
155 TSort.tsort(each_node, each_child)
156 end
157
158 # Returns a topologically sorted array of nodes.
159 # The array is sorted from children to parents, i.e.
160 # the first element has no child and the last node has no parent.
161 #
162 # The graph is represented by _each_node_ and _each_child_.
163 # _each_node_ should have +call+ method which yields for each node in the graph.
164 # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
165 #
166 # If there is a cycle, TSort::Cyclic is raised.
167 #
168 # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
169 # each_node = lambda {|&b| g.each_key(&b) }
170 # each_child = lambda {|n, &b| g[n].each(&b) }
171 # p TSort.tsort(each_node, each_child) #=> [4, 2, 3, 1]
172 #
173 # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
174 # each_node = lambda {|&b| g.each_key(&b) }
175 # each_child = lambda {|n, &b| g[n].each(&b) }
176 # p TSort.tsort(each_node, each_child) # raises TSort::Cyclic
177 #
178 def TSort.tsort(each_node, each_child)
179 TSort.tsort_each(each_node, each_child).to_a
180 end
181
182 # The iterator version of the #tsort method.
183 # <tt><em>obj</em>.tsort_each</tt> is similar to <tt><em>obj</em>.tsort.each</tt>, but
184 # modification of _obj_ during the iteration may lead to unexpected results.
185 #
186 # #tsort_each returns +nil+.
187 # If there is a cycle, TSort::Cyclic is raised.
188 #
189 # class G
190 # include TSort
191 # def initialize(g)
192 # @g = g
193 # end
194 # def tsort_each_child(n, &b) @g[n].each(&b) end
195 # def tsort_each_node(&b) @g.each_key(&b) end
196 # end
197 #
198 # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
199 # graph.tsort_each {|n| p n }
200 # #=> 4
201 # # 2
202 # # 3
203 # # 1
204 #
205 def tsort_each(&block) # :yields: node
206 each_node = method(:tsort_each_node)
207 each_child = method(:tsort_each_child)
208 TSort.tsort_each(each_node, each_child, &block)
209 end
210
211 # The iterator version of the TSort.tsort method.
212 #
213 # The graph is represented by _each_node_ and _each_child_.
214 # _each_node_ should have +call+ method which yields for each node in the graph.
215 # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
216 #
217 # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
218 # each_node = lambda {|&b| g.each_key(&b) }
219 # each_child = lambda {|n, &b| g[n].each(&b) }
220 # TSort.tsort_each(each_node, each_child) {|n| p n }
221 # #=> 4
222 # # 2
223 # # 3
224 # # 1
225 #
226 def TSort.tsort_each(each_node, each_child) # :yields: node
227 return to_enum(__method__, each_node, each_child) unless block_given?
228
229 TSort.each_strongly_connected_component(each_node, each_child) {|component|
230 if component.size == 1
231 yield component.first
232 else
233 raise Cyclic.new("topological sort failed: #{component.inspect}")
234 end
235 }
236 end
237
238 # Returns strongly connected components as an array of arrays of nodes.
239 # The array is sorted from children to parents.
240 # Each elements of the array represents a strongly connected component.
241 #
242 # class G
243 # include TSort
244 # def initialize(g)
245 # @g = g
246 # end
247 # def tsort_each_child(n, &b) @g[n].each(&b) end
248 # def tsort_each_node(&b) @g.each_key(&b) end
249 # end
250 #
251 # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
252 # p graph.strongly_connected_components #=> [[4], [2], [3], [1]]
253 #
254 # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
255 # p graph.strongly_connected_components #=> [[4], [2, 3], [1]]
256 #
257 def strongly_connected_components
258 each_node = method(:tsort_each_node)
259 each_child = method(:tsort_each_child)
260 TSort.strongly_connected_components(each_node, each_child)
261 end
262
263 # Returns strongly connected components as an array of arrays of nodes.
264 # The array is sorted from children to parents.
265 # Each elements of the array represents a strongly connected component.
266 #
267 # The graph is represented by _each_node_ and _each_child_.
268 # _each_node_ should have +call+ method which yields for each node in the graph.
269 # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
270 #
271 # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
272 # each_node = lambda {|&b| g.each_key(&b) }
273 # each_child = lambda {|n, &b| g[n].each(&b) }
274 # p TSort.strongly_connected_components(each_node, each_child)
275 # #=> [[4], [2], [3], [1]]
276 #
277 # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
278 # each_node = lambda {|&b| g.each_key(&b) }
279 # each_child = lambda {|n, &b| g[n].each(&b) }
280 # p TSort.strongly_connected_components(each_node, each_child)
281 # #=> [[4], [2, 3], [1]]
282 #
283 def TSort.strongly_connected_components(each_node, each_child)
284 TSort.each_strongly_connected_component(each_node, each_child).to_a
285 end
286
287 # The iterator version of the #strongly_connected_components method.
288 # <tt><em>obj</em>.each_strongly_connected_component</tt> is similar to
289 # <tt><em>obj</em>.strongly_connected_components.each</tt>, but
290 # modification of _obj_ during the iteration may lead to unexpected results.
291 #
292 # #each_strongly_connected_component returns +nil+.
293 #
294 # class G
295 # include TSort
296 # def initialize(g)
297 # @g = g
298 # end
299 # def tsort_each_child(n, &b) @g[n].each(&b) end
300 # def tsort_each_node(&b) @g.each_key(&b) end
301 # end
302 #
303 # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
304 # graph.each_strongly_connected_component {|scc| p scc }
305 # #=> [4]
306 # # [2]
307 # # [3]
308 # # [1]
309 #
310 # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
311 # graph.each_strongly_connected_component {|scc| p scc }
312 # #=> [4]
313 # # [2, 3]
314 # # [1]
315 #
316 def each_strongly_connected_component(&block) # :yields: nodes
317 each_node = method(:tsort_each_node)
318 each_child = method(:tsort_each_child)
319 TSort.each_strongly_connected_component(each_node, each_child, &block)
320 end
321
322 # The iterator version of the TSort.strongly_connected_components method.
323 #
324 # The graph is represented by _each_node_ and _each_child_.
325 # _each_node_ should have +call+ method which yields for each node in the graph.
326 # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
327 #
328 # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
329 # each_node = lambda {|&b| g.each_key(&b) }
330 # each_child = lambda {|n, &b| g[n].each(&b) }
331 # TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
332 # #=> [4]
333 # # [2]
334 # # [3]
335 # # [1]
336 #
337 # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
338 # each_node = lambda {|&b| g.each_key(&b) }
339 # each_child = lambda {|n, &b| g[n].each(&b) }
340 # TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
341 # #=> [4]
342 # # [2, 3]
343 # # [1]
344 #
345 def TSort.each_strongly_connected_component(each_node, each_child) # :yields: nodes
346 return to_enum(__method__, each_node, each_child) unless block_given?
347
348 id_map = {}
349 stack = []
350 each_node.call {|node|
351 unless id_map.include? node
352 TSort.each_strongly_connected_component_from(node, each_child, id_map, stack) {|c|
353 yield c
354 }
355 end
356 }
357 nil
358 end
359
360 # Iterates over strongly connected component in the subgraph reachable from
361 # _node_.
362 #
363 # Return value is unspecified.
364 #
365 # #each_strongly_connected_component_from doesn't call #tsort_each_node.
366 #
367 # class G
368 # include TSort
369 # def initialize(g)
370 # @g = g
371 # end
372 # def tsort_each_child(n, &b) @g[n].each(&b) end
373 # def tsort_each_node(&b) @g.each_key(&b) end
374 # end
375 #
376 # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
377 # graph.each_strongly_connected_component_from(2) {|scc| p scc }
378 # #=> [4]
379 # # [2]
380 #
381 # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
382 # graph.each_strongly_connected_component_from(2) {|scc| p scc }
383 # #=> [4]
384 # # [2, 3]
385 #
386 def each_strongly_connected_component_from(node, id_map={}, stack=[], &block) # :yields: nodes
387 TSort.each_strongly_connected_component_from(node, method(:tsort_each_child), id_map, stack, &block)
388 end
389
390 # Iterates over strongly connected components in a graph.
391 # The graph is represented by _node_ and _each_child_.
392 #
393 # _node_ is the first node.
394 # _each_child_ should have +call+ method which takes a node argument
395 # and yields for each child node.
396 #
397 # Return value is unspecified.
398 #
399 # #TSort.each_strongly_connected_component_from is a class method and
400 # it doesn't need a class to represent a graph which includes TSort.
401 #
402 # graph = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
403 # each_child = lambda {|n, &b| graph[n].each(&b) }
404 # TSort.each_strongly_connected_component_from(1, each_child) {|scc|
405 # p scc
406 # }
407 # #=> [4]
408 # # [2, 3]
409 # # [1]
410 #
411 def TSort.each_strongly_connected_component_from(node, each_child, id_map={}, stack=[]) # :yields: nodes
412 return to_enum(__method__, node, each_child, id_map, stack) unless block_given?
413
414 minimum_id = node_id = id_map[node] = id_map.size
415 stack_length = stack.length
416 stack << node
417
418 each_child.call(node) {|child|
419 if id_map.include? child
420 child_id = id_map[child]
421 minimum_id = child_id if child_id && child_id < minimum_id
422 else
423 sub_minimum_id =
424 TSort.each_strongly_connected_component_from(child, each_child, id_map, stack) {|c|
425 yield c
426 }
427 minimum_id = sub_minimum_id if sub_minimum_id < minimum_id
428 end
429 }
430
431 if node_id == minimum_id
432 component = stack.slice!(stack_length .. -1)
433 component.each {|n| id_map[n] = nil}
434 yield component
435 end
436
437 minimum_id
438 end
439
440 # Should be implemented by a extended class.
441 #
442 # #tsort_each_node is used to iterate for all nodes over a graph.
443 #
444 def tsort_each_node # :yields: node
445 raise NotImplementedError.new
446 end
447
448 # Should be implemented by a extended class.
449 #
450 # #tsort_each_child is used to iterate for child nodes of _node_.
451 #
452 def tsort_each_child(node) # :yields: child
453 raise NotImplementedError.new
454 end
455
456 private
457 def method(name)
458 lambda do |*args, &block|
459 __send__(name, *args, &block)
460 end
461 end
462 end