本文簡要介紹ruby語言中 TSort模塊
的用法。
TSort
使用 Tarjan 算法對強連通分量進行拓撲排序。
TSort
旨在能夠與任何可以解釋為有向圖的對象一起使用。
TSort
需要兩種方法將對象解釋為圖形, tsort_each_node
和 tsort_each_child。
-
tsort_each_node
用於迭代圖上的所有節點。 -
tsort_each_child
用於迭代給定節點的子節點。
節點的相等性由 eql?和哈希,因為 TSort
在內部使用 Hash
。
一個簡單的例子
以下示例演示如何將 TSort
模塊混合到現有類(在本例中為 Hash
)。在這裏,我們將散列中的每個鍵視為圖中的一個節點,因此我們隻需將所需的 tsort_each_node
方法別名為 Hash 的 each_key 方法。對於散列中的每個鍵,關聯的值是節點的子節點的數組。這種選擇反過來導致我們實現所需的 tsort_each_child
方法,該方法獲取子節點數組,然後使用用戶提供的塊迭代該數組。
require 'tsort'
class Hash
include TSort
alias tsort_each_node each_key
def tsort_each_child(node, &block)
fetch(node).each(&block)
end
end
{1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
#=> [3, 2, 1, 4]
{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
#=> [[4], [2, 3], [1]]
一個更現實的例子
一個非常簡單的‘make’ 類工具可以實現如下:
require 'tsort'
class Make
def initialize
@dep = {}
@dep.default = []
end
def rule(outputs, inputs=[], &block)
triple = [outputs, inputs, block]
outputs.each {|f| @dep[f] = [triple]}
@dep[triple] = inputs
end
def build(target)
each_strongly_connected_component_from(target) {|ns|
if ns.length != 1
fs = ns.delete_if {|n| Array === n}
raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
end
n = ns.first
if Array === n
outputs, inputs, block = n
inputs_time = inputs.map {|f| File.mtime f}.max
begin
outputs_time = outputs.map {|f| File.mtime f}.min
rescue Errno::ENOENT
outputs_time = nil
end
if outputs_time == nil ||
inputs_time != nil && outputs_time <= inputs_time
sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
block.call
end
end
}
end
def tsort_each_child(node, &block)
@dep[node].each(&block)
end
include TSort
end
def command(arg)
print arg, "\n"
system arg
end
m = Make.new
m.rule(%w[t1]) { command 'date > t1' }
m.rule(%w[t2]) { command 'date > t2' }
m.rule(%w[t3]) { command 'date > t3' }
m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
m.build('t5')
錯誤
-
'tsort.rb' 是錯誤的名稱,因為這個庫使用 Tarjan 的強連接組件算法。雖然“strongly_connected_components.rb”是正確的,但是太長了。
參考
-
Tarjan,“深度優先搜索和線性圖算法”,
-
SIAM Journal on Computing
,卷。 1,第 2 期,第 146-160 頁,1972 年 6 月。
相關用法
- Ruby TSort.tsort用法及代碼示例
- Ruby TSort.strongly_connected_components用法及代碼示例
- Ruby TSort.each_strongly_connected_component用法及代碼示例
- Ruby TSort.each_strongly_connected_component_from用法及代碼示例
- Ruby TSort.tsort_each用法及代碼示例
- Ruby Time tv_sec用法及代碼示例
- Ruby Time usec用法及代碼示例
- Ruby TCPServer.accept用法及代碼示例
- Ruby Time yday()用法及代碼示例
- Ruby Time succ()用法及代碼示例
- Ruby Time mon()用法及代碼示例
- Ruby Time.gmtime用法及代碼示例
- Ruby Time iso8601用法及代碼示例
- Ruby Time.at用法及代碼示例
- Ruby Thread.kill用法及代碼示例
- Ruby Time.utc_offset用法及代碼示例
- Ruby Time.isdst用法及代碼示例
- Ruby TracePoint.defined_class用法及代碼示例
- Ruby Time.time + numeric用法及代碼示例
- Ruby Thread.pending_interrupt?用法及代碼示例
- Ruby Time wednesday?用法及代碼示例
- Ruby Time.wednesday?用法及代碼示例
- Ruby Thread kill()用法及代碼示例
- Ruby Time asctime()用法及代碼示例
- Ruby TypeError類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 TSort模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。