图的强连通tarjan学习

来源:互联网 发布:电商大数据 下载 编辑:程序博客网 时间:2024/05/23 23:47


Tarjan 算法

转载自http://www.cnblogs.com/shadowland/p/5872257.html

一.算法简介

Tarjan 算法一种由Robert Tarjan提出的求解有向图强连通分量的算法,它能做到线性时间的复杂度。

 

我们定义:

如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向图的极大强连通子图,称为强连通分量(strongly connected components)。

例如:在上图中,{1 , 2 , 3 , 4 } , { 5 } ,  { 6 } 三个区域可以相互连通,称为这个图的强连通分量。

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

再Tarjan算法中,有如下定义。

DFN[ i ] : 在DFS中该节点被搜索的次序(时间戳)

LOW[ i ] : 为i或i的子树能够追溯到的最早的栈中节点的次序号

当DFN[ i ]==LOW[ i ]时,为i或i的子树可以构成一个强连通分量。

 

二.算法图示

以1为Tarjan 算法的起始点,如图

顺次DFS搜到节点6

 回溯时发现LOW[ 5 ]==DFN[ 5 ] ,  LOW[ 6 ]==DFN[ 6 ] ,则{ 5 } , { 6 } 为两个强连通分量。回溯至3节点,拓展节点4.

拓展节点1 , 发现1再栈中更新LOW[ 4 ],LOW[ 3 ] 的值为1

 回溯节点1,拓展节点2

自此,Tarjan Algorithm 结束,{1 , 2 , 3 , 4 } , { 5 } ,  { 6 } 为图中的三个强连通分量。

不难发现,Tarjan Algorithm 的时间复杂度为O(E+V).

hdu1269

#include<bits/stdc++.h>using namespace std;const int maxn = 10000 + 10;int n,m;int head[maxn * 10];struct Edge{Edge(){}Edge(int x,int y):to(x),next(y){}int to;int next;}edge[maxn*10];int dfn[maxn];int low[maxn];int stacks[maxn];int top;bool vis[maxn];int ans;void tarjan(int x,int &len){//cout << x << endl;dfn[x] = len;low[x] = len;vis[x] = true;//记录x是否在栈中 stacks[++top] = x;for(int i = head[x]; i != -1; i = edge[i].next){int v = edge[i].to;if(!dfn[v]){len ++;tarjan(v,len);low[x] = min(low[x],low[v]);}else if(vis[v])low[x] = min(low[x],dfn[v]);}if(dfn[x] == low[x]){ans ++;vis[x] = false;while(stacks[top --] != x)//先减是为了防止出现一个点成联通的情况{vis[stacks[top + 1]] = false;}}}int main(){while( ~ scanf("%d%d",&n,&m)){ans = 0;memset(vis,false,sizeof(vis));top = 0;if(n == 0 && m == 0)break;memset(head,-1,sizeof(head));memset(dfn,0,sizeof(dfn));for(int i = 1; i <= m; i ++){int x,y;scanf("%d%d",&x,&y);edge[i] = Edge(y,head[x]);head[x] = i;}for(int i = 1; i <= n; i ++){if(!dfn[i]){int len = 1;tarjan(i,len);}}//for(int i = 1; i <= n; i ++)//{//cout <<i << " "  << dfn[i] <<" " << low[i] << endl; //}if(ans == 1)cout << "Yes" << endl;else cout << "No" <<endl; }return 0;}


三.算法模板

复制代码
 1 void Tarjan ( int x ) { 2          dfn[ x ] = ++dfs_num ; 3          low[ x ] = dfs_num ; 4          vis [ x ] = true ;//是否在栈中 5          stack [ ++top ] = x ; 6          for ( int i=head[ x ] ; i!=0 ; i=e[i].next ){ 7                   int temp = e[ i ].to ; 8                   if ( !dfn[ temp ] ){ 9                            Tarjan ( temp ) ;10                            low[ x ] = gmin ( low[ x ] , low[ temp ] ) ;11                  }12                  else if ( vis[ temp ])low[ x ] = gmin ( low[ x ] , dfn[ temp ] ) ;13          }14          if ( dfn[ x ]==low[ x ] ) {//构成强连通分量15                   vis[ x ] = false ;16                   color[ x ] = ++col_num ;//染色17                   while ( stack[ top --] != x ) {//清空//先减是为了防止出现一个点成联通的情况18                            color [stack[ top + 1]] = col_num ;19                            vis [ stack[ top + 1] ] = false ;20                  }21                  top -- ;22          }23 }
复制代码
0 0