苏木三少
错的不是你,而是这个世界。

『图论』有向图强连通分量的 Tarjan 算法

由某985千千大佬制作:原文链接—-传送门

[有向图强连通分量]

在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components)。

 

下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达。{5},{6}也分别是两个强连通分量。

直接根据定义,用双向遍历取交集的方法求强连通分量,时间复杂度为O(N^2+M)。更好的方法是Kosaraju算法或Tarjan算法,两者的时间复杂度都是O(N+M)。本文介绍的是Tarjan算法。

 

[Tarjan算法]

Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。

 

定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。由定义可以得出,


1
2
3
4
5
6
<span class="token function">Low</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token operator">=</span>Min
<span class="token punctuation">{</span>
    <span class="token function">DFN</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token function">Low</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">)</span>为树枝边,u为v的父节点
    <span class="token function">DFN</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span>v<span class="token punctuation">)</span>为指向栈中节点的后向边<span class="token punctuation">(</span>非横叉边<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
C++

当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量。

 

算法伪代码如下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="token function">tarjan</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    DFN<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span>Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>Index                      <span class="token comment" spellcheck="true">// 为节点u设定次序编号和Low初值</span>
    Stack<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>u<span class="token punctuation">)</span>                              <span class="token comment" spellcheck="true">// 将节点u压入栈中</span>
    <span class="token keyword">for</span> each <span class="token punctuation">(</span>u<span class="token punctuation">,</span> v<span class="token punctuation">)</span> in E                       <span class="token comment" spellcheck="true">// 枚举每一条边</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>v is <span class="token operator">not</span> visted<span class="token punctuation">)</span>               <span class="token comment" spellcheck="true">// 如果节点v未被访问过</span>
            <span class="token function">tarjan</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>                  <span class="token comment" spellcheck="true">// 继续向下找</span>
            Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">,</span> Low<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>v in S<span class="token punctuation">)</span>                   <span class="token comment" spellcheck="true">// 如果节点v还在栈内</span>
            Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">min</span><span class="token punctuation">(</span>Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">,</span> DFN<span class="token punctuation">[</span>v<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>DFN<span class="token punctuation">[</span>u<span class="token punctuation">]</span> <span class="token operator">==</span> Low<span class="token punctuation">[</span>u<span class="token punctuation">]</span><span class="token punctuation">)</span>                      <span class="token comment" spellcheck="true">// 如果节点u是强连通分量的根</span>
        repeat
            v <span class="token operator">=</span> S<span class="token punctuation">.</span>pop                  <span class="token comment" spellcheck="true">// 将v退栈,为该强连通分量中一个顶点</span>
            print v
        until <span class="token punctuation">(</span>u<span class="token operator">==</span> v<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
C++

 

接下来是对算法流程的演示。

从节点1开始DFS,把遍历到的节点加入栈中。搜索到节点u=6时,DFN[6]=LOW[6],找到了一个强连通分量。退栈到u=v为止,{6}为一个强连通分量。

返回节点5,发现DFN[5]=LOW[5],退栈后{5}为一个强连通分量。

返回节点3,继续搜索到节点4,把4加入堆栈。发现节点4向节点1有后向边,节点1还在栈中,所以LOW[4]=1。节点6已经出栈,(4,6)是横叉边,返回3,(3,4)为树枝边,所以LOW[3]=LOW[4]=1。

继续回到节点1,最后访问节点2。访问边(2,4),4还在栈中,所以LOW[2]=DFN[4]=5。返回1后,发现DFN[1]=LOW[1],把栈中节点全部取出,组成一个连通分量{1,3,4,2}。

至此,算法结束。经过该算法,求出了图中全部的三个强连通分量{1,3,4,2},{5},{6}。

 

可以发现,运行Tarjan算法的过程中,每个顶点都被访问了一次,且只进出了一次堆栈,每条边也只被访问了一次,所以该算法的时间复杂度为O(N+M)。

 

求有向图的强连通分量还有一个强有力的算法,为Kosaraju算法。Kosaraju是基于对有向图及其逆图两次DFS的方法,其时间复杂度也是O(N+M)。与Trajan算法相比,Kosaraju算法可能会稍微更直观一些。但是Tarjan只用对原图进行一次DFS,不用建立逆图,更简洁。在实际的测试中,Tarjan算法的运行效率也比Kosaraju算法高30%左右。此外,该Tarjan算法与求无向图的双连通分量(割点、桥)的Tarjan算法也有着很深的联系。学习该Tarjan算法,也有助于深入理解求双连通分量的Tarjan算法,两者可以类比、组合理解。

 

求有向图的强连通分量的Tarjan算法是以其发明者Robert Tarjan命名的。Robert Tarjan还发明了求双连通分量的Tarjan算法,以及求最近公共祖先的离线Tarjan算法,在此对Tarjan表示崇高的敬意。

 

附:tarjan算法的C++程序


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<span class="token keyword">void</span> <span class="token function">tarjan</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> j<span class="token punctuation">;</span>
    DFN<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token operator">++</span>Dindex<span class="token punctuation">;</span>
    instack<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span>
    Stap<span class="token punctuation">[</span><span class="token operator">++</span>Stop<span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>edge <span class="token operator">*</span>e<span class="token operator">=</span>V<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>e<span class="token punctuation">;</span>e<span class="token operator">=</span>e<span class="token operator">-</span><span class="token operator">&gt;</span>next<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        j<span class="token operator">=</span>e<span class="token operator">-</span><span class="token operator">&gt;</span>t<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>DFN<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">tarjan</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>LOW<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&lt;</span>LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>LOW<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>instack<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> DFN<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">&lt;</span>LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
            LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>DFN<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>DFN<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">==</span>LOW<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        Bcnt<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token keyword">do</span>
        <span class="token punctuation">{</span>
            j<span class="token operator">=</span>Stap<span class="token punctuation">[</span>Stop<span class="token operator">--</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            instack<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">;</span>
            Belong<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>Bcnt<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>j<span class="token operator">!=</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">solve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> i<span class="token punctuation">;</span>
    Stop<span class="token operator">=</span>Bcnt<span class="token operator">=</span>Dindex<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>DFN<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>DFN<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span>N<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>DFN<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token function">tarjan</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
赞(6) 打赏
有问题的朋友随时留言,或者加我为好友。我的QQ是805375353. <<苏木三少博客 » 『图论』有向图强连通分量的 Tarjan 算法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

十年之约