您的位置:首页 > 其它

强连通分量算法(3)

2011-02-28 14:24 239 查看

Cheriyan–Mehlhorn/Gabow algorithm

这个算法的思路和tarjan算法差不多,效率要比tarjan算法快一点。

算法需要两个栈来进行维护。

下面这个伪代码是tarjan的伪代码修改过来的。从结构上看非常的相似。

algorithm gabow is

input: graph G = (V, E)

output: set of strongly connected components (sets of vertices)

index := 1

S1 := empty

S2 := empty

for each v in V do

if (v.index is undefined)

strongconnect(v)

end if

repeat

function strongconnect(v)

// Set the depth index for v to the smallest unused index

v.index := index

index := index + 1

S1.push(v)

S2.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)

else if (w is in S1) then

// Successor w is in stack S1 and hence in the current SCC

While( S2.top.index>w.index ) S2.top--;

end if

repeat

// If v is a root node, pop the stack and generate an SCC

if (i==S2.top) then

start a new strongly connected component

S2.top--;

repeat

w := S1.pop()

add w to current strongly connected component

until (w = v)

output the current strongly connected component

end if

end function




对于这样一个图:我们手动执行一遍程序

1.
第一步

DFN
= [1]

S1
= [1]

S2
= [1]

2.
第二步

DFN=[1
2]

S1
= [1 3]

S2
=[1 3]

3.
第三步

DFN=[1
2 3]

S1
=[1 3 5]

S2
=[1 3 5]

4.
第四步

DFN=[1
2 3 4]

S1
= [1 3 5 6]

S2
= [1 3 5 6]

5.
第五步

I
= 6

I
= top of S2;

DFN=[1
2 3]

S1=[1
3 5]

S2=[1
3 5]

6.
第六步

I=5

I=top
of S2

DFN=[1
2]

S1=[1
3]

S2=[1
3]

7.
第七步

I=3,继续搜

DFN
= [1 2 5]

S1=[1
3 4]

S2=[1
3 4]

8.
第八步

I=4,继续搜

碰到1,已经存在于S1,那么要处理S2

DFN=[1
2 5]

S1=[1
3 4]

S2=[1]

9.
第九步

回到I=3

10.
第十步

回到I=1,继续搜

DFN=[1
2 5 6]

S1=[1
3 4 2]

S2=[1
2]

11.
第十一步

I=4,已经存在于S1中,处理S2

DFN=[1
2 5 6]

S1=[1
3 4 2]

S2=[1]

12.
第十二步

回到I=2

回到I=1

I==top
of S2

从S1中弹出一个scc

S1
= []

S2
= []

算法执行完毕


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: