强连通分量及缩点tarjan算法解析

来源:互联网 发布:php tp框架下载 编辑:程序博客网 时间:2024/05/01 02:22

http://blog.csdn.net/justlovetao/article/details/6673602 

有向图强连通分量的Tarjan算法 [有向图强连通分量]

在有向图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的子树能够追溯到的最早的栈中节点的次序号。由定义可以得出,

Low(u)=Min{   DFN(u),(第一次被搜索到)   Low(v),(u,v)为树枝边,u为v的父节点   DFN(v),(u,v)为指向栈中节点的后向边(非横叉边)(即沿着u搜到v时,发现v已经被搜过了,也就是说形成了一个环)}

当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量。(实质上就是这个点的整个子树回溯完了,并且下面都是死路)


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

从节点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,3,4的low都是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++程序

#include<iostream>  
  1. #include<cstring>  
  2. #include<cstdio>  
  3. using namespace std;  
  4. #define N 100  
  5. #define M 100  
  6. struct Edge  
  7. {  
  8.     int v;  
  9.     int next;  
  10. };  
  11. Edge edge[M];//边的集合  
  12.   
  13. int node[N];//顶点集合  
  14. int instack[N];//标记是否在stack中  
  15. int stack[N];  
  16. int Belong[N];//各顶点属于哪个强连通分量  
  17. int DFN[N];//节点u搜索的序号(时间戳)  
  18. int LOW[N];//u或u的子树能够追溯到的最早的栈中节点的序号(时间戳)  
  19. int n, m;//n:点的个数;m:边的条数  
  20. int cnt_edge;//边的计数器  
  21. int Index;//序号(时间戳)  
  22. int top;  
  23. int Bcnt;//有多少个强连通分量  
  24.   
  25. void add_edge(int u, int v)//邻接表存储  
  26. {  
  27.     edge[cnt_edge].next = node[u];  
  28.     edge[cnt_edge].v = v;  
  29.     node[u] = cnt_edge++;  
  30. }  
  31. void tarjan(int u)  
  32. {  
  33.     int i,j;  
  34.     int v;  
  35.     DFN[u]=LOW[u]=++Index;  
  36.     instack[u]=true;  
  37.     stack[++top]=u;  
  38.     for (i = node[u]; i != -1; i = edge[i].next)  
  39.     {  
  40.         v=edge[i].v;  
  41.         if (!DFN[v])//如果点v没被访问  
  42.         {  
  43.             tarjan(v);  
  44.             if (LOW[v]<LOW[u])  
  45.                 LOW[u]=LOW[v];  
  46.         }  
  47.         else//如果点v已经被访问过  
  48.             if (instack[v] && DFN[v]<LOW[u])  
  49.                 LOW[u]=DFN[v];  
  50.     }  
  51.     if (DFN[u]==LOW[u])  
  52.     {  
  53.         Bcnt++;  
  54.         do  
  55.         {  
  56.             j=stack[top--];  
  57.             instack[j]=false;  
  58.             Belong[j]=Bcnt;  
  59.         }  
  60.         while (j!=u);  
  61.     }  
  62. }  
  63. void solve()  
  64. {  
  65.     int i;  
  66.     top=Bcnt=Index=0;  
  67.     memset(DFN,0,sizeof(DFN));  
  68.     memset(LOW,0,sizeof(LOW));  
  69.     for (i=1;i<=n;i++)  
  70.         if (!DFN[i])  
  71.             tarjan(i);  
  72. }  
  73. int main()  
  74. {  
  75.     freopen("in.txt","r",stdin);  
  76.     int i,j,k;  
  77.     cnt_edge=0;  
  78.     memset(node,-1,sizeof(node));  
  79.     scanf("%d%d",&n,&m);  
  80.     for(i=1;i<=m;i++)  
  81.     {  
  82.         scanf("%d%d",&j,&k);  
  83.         add_edge(j,k);  
  84.     }  
  85.     solve();  
  86.     for(i=1;i<=n;i++)  
  87.         printf("%d ",Belong[i]);  
  88. }  
  89.   
  90. </pre><br>  

模板题目:
Strongly Connected Components  

Write a program to find the strongly connected components in a digraph.

Format of functions:

void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );

where Graph is defined as the following:

typedef struct VNode *PtrToVNode;struct VNode {    Vertex Vert;    PtrToVNode Next;};typedef struct GNode *Graph;struct GNode {    int NumOfVertices;    int NumOfEdges;    PtrToVNode *Array;};

Here void (*visit)(Vertex V) is a function parameter that is passed into StronglyConnectedComponents to handle (print with a certain format) each vertex that is visited. The function StronglyConnectedComponents is supposed to print a return after each component is found.

Sample program of judge:

#include <stdio.h>#include <stdlib.h>#define MaxVertices 10  /* maximum number of vertices */typedef int Vertex;     /* vertices are numbered from 0 to MaxVertices-1 */typedef struct VNode *PtrToVNode;struct VNode {    Vertex Vert;    PtrToVNode Next;};typedef struct GNode *Graph;struct GNode {    int NumOfVertices;    int NumOfEdges;    PtrToVNode *Array;};Graph ReadG(); /* details omitted */void PrintV( Vertex V ){   printf("%d ", V);}void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) );int main(){    Graph G = ReadG();    StronglyConnectedComponents( G, PrintV );    return 0;}/* Your function will be put here */

Sample Input (for the graph shown in the figure):

4 50 11 22 03 13 2

Sample Output:

3 1 2 0

Note: The output order does not matter. That is, a solution like

0 1 2 3

is also considered correct.

int instack[100];int index;int DFN[100];int low[100];int stack[100];int top;void DFS(int u,Graph G,void (*visit)(Vertex V)){DFN[u] = ++index;low[u] = DFN[u];PtrToVNode temphead = G->Array[u];stack[++top] = u;instack[u]=1;while(temphead){int cur = temphead->Vert;if(!DFN[cur]) {DFS(cur,G,visit);if(low[cur]<low[u]) low[u]=low[cur];}else{if(instack[cur] && DFN[cur]<low[u])low[u]=DFN[cur];}temphead = temphead -> Next;}if(DFN[u] == low[u]){int temp;do{temp = stack[top--];visit(temp);instack[temp]=0;}while(temp != u);printf("\n");}}StronglyConnectedComponents( Graph G,void (*visit)(Vertex V)){int V = G->NumOfVertices;for(int i=0;i<V;i++) low[i]=0;for(int i=0;i<V;i++){if(!DFN[i]) DFS(i,G,visit);}}



0 0
原创粉丝点击