この質問は関連していますが、最近質問されたものと同じではありませんhere .
ウィキペディアの疑似コードを読んだところです。
algorithm tarjan is
input: graph G = (V, E)
output: set of strongly connected components (sets of vertices)
index := 0
S := empty
for each v in V do
if (v.index is undefined) then
strongconnect(v)
end if
end for
function strongconnect(v)
// Set the depth index for v to the smallest unused index
v.index := index
v.lowlink := index
index := index + 1
S.push(v)
// Consider successors of v
for each (v, w) in E do
if (w.index is undefined) then
// Successor w has not yet been visited; recurse on it
strongconnect(w)
v.lowlink := min(v.lowlink, w.lowlink)
else if (w is in S) then
// Successor w is in stack S and hence in the current SCC
v.lowlink := min(v.lowlink, w.index)
end if
end for
// If v is a root node, pop the stack and generate an SCC
if (v.lowlink = v.index) then
start a new strongly connected component
repeat
w := S.pop()
add w to current strongly connected component
until (w = v)
output the current strongly connected component
end if
end function
2 つの非常に基本的な質問があるため、明らかに適切に理解していなかったに違いありません。
と言うとき
if (w is in S)
、要素はインデックスによって順序付けられるため、この操作は O(N) または少なくとも O(logN) の複雑さではありませんか? ルート ノードからアクセスできる新しいノードごとにこれを実行する必要があるため、全体的な複雑さはありませんO(NlogN)
。さらに、S はスタックであるため、概念的には最上位の要素のみにアクセスできる必要があります。その中で検索を実装するにはどうすればよいでしょうか? ここでは、二分探索木の方が優れたデータ構造であるべきではありませんか?この部分では:
そうでなければ (w は S にある) then
v.lowlink := min(v.lowlink, w.index)
w.index
and notを使用する特定の理由はありw.lowlink
ますか? 使用する利点はw.lowlink
、前の質問 (リンクされた質問) に回答できることです。SCC 内のLLs
すべてのノードの は、すべてのノードで同じであることが保証されます。