分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
有向图强连通分量的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<cstring>
- #include<cstdio>
- using namespace std;
- #define N 100
- #define M 100
- struct Edge
- {
- int v;
- int next;
- };
- Edge edge[M];//边的集合
- int node[N];//顶点集合
- int instack[N];//标记是否在stack中
- int stack[N];
- int Belong[N];//各顶点属于哪个强连通分量
- int DFN[N];//节点u搜索的序号(时间戳)
- int LOW[N];//u或u的子树能够追溯到的最早的栈中节点的序号(时间戳)
- int n, m;//n:点的个数;m:边的条数
- int cnt_edge;//边的计数器
- int Index;//序号(时间戳)
- int top;
- int Bcnt;//有多少个强连通分量
- void add_edge(int u, int v)//邻接表存储
- {
- edge[cnt_edge].next = node[u];
- edge[cnt_edge].v = v;
- node[u] = cnt_edge++;
- }
- void tarjan(int u)
- {
- int i,j;
- int v;
- DFN[u]=LOW[u]=++Index;
- instack[u]=true;
- stack[++top]=u;
- for (i = node[u]; i != -1; i = edge[i].next)
- {
- v=edge[i].v;
- if (!DFN[v])//如果点v没被访问
- {
- tarjan(v);
- if (LOW[v]<LOW[u])
- LOW[u]=LOW[v];
- }
- else//如果点v已经被访问过
- if (instack[v] && DFN[v]<LOW[u])
- LOW[u]=DFN[v];
- }
- if (DFN[u]==LOW[u])
- {
- Bcnt++;
- do
- {
- j=stack[top--];
- instack[j]=false;
- Belong[j]=Bcnt;
- }
- while (j!=u);
- }
- }
- void solve()
- {
- int i;
- top=Bcnt=Index=0;
- memset(DFN,0,sizeof(DFN));
- memset(LOW,0,sizeof(LOW));
- for (i=1;i<=n;i++)
- if (!DFN[i])
- tarjan(i);
- }
- int main()
- {
- freopen("in.txt","r",stdin);
- int i,j,k;
- cnt_edge=0;
- memset(node,-1,sizeof(node));
- scanf("%d%d",&n,&m);
- for(i=1;i<=m;i++)
- {
- scanf("%d%d",&j,&k);
- add_edge(j,k);
- }
- solve();
- for(i=1;i<=n;i++)
- printf("%d ",Belong[i]);
- }
- </pre><br>
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); }}
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
有向图强连通分量的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<cstring>
- #include<cstdio>
- using namespace std;
- #define N 100
- #define M 100
- struct Edge
- {
- int v;
- int next;
- };
- Edge edge[M];//边的集合
- int node[N];//顶点集合
- int instack[N];//标记是否在stack中
- int stack[N];
- int Belong[N];//各顶点属于哪个强连通分量
- int DFN[N];//节点u搜索的序号(时间戳)
- int LOW[N];//u或u的子树能够追溯到的最早的栈中节点的序号(时间戳)
- int n, m;//n:点的个数;m:边的条数
- int cnt_edge;//边的计数器
- int Index;//序号(时间戳)
- int top;
- int Bcnt;//有多少个强连通分量
- void add_edge(int u, int v)//邻接表存储
- {
- edge[cnt_edge].next = node[u];
- edge[cnt_edge].v = v;
- node[u] = cnt_edge++;
- }
- void tarjan(int u)
- {
- int i,j;
- int v;
- DFN[u]=LOW[u]=++Index;
- instack[u]=true;
- stack[++top]=u;
- for (i = node[u]; i != -1; i = edge[i].next)
- {
- v=edge[i].v;
- if (!DFN[v])//如果点v没被访问
- {
- tarjan(v);
- if (LOW[v]<LOW[u])
- LOW[u]=LOW[v];
- }
- else//如果点v已经被访问过
- if (instack[v] && DFN[v]<LOW[u])
- LOW[u]=DFN[v];
- }
- if (DFN[u]==LOW[u])
- {
- Bcnt++;
- do
- {
- j=stack[top--];
- instack[j]=false;
- Belong[j]=Bcnt;
- }
- while (j!=u);
- }
- }
- void solve()
- {
- int i;
- top=Bcnt=Index=0;
- memset(DFN,0,sizeof(DFN));
- memset(LOW,0,sizeof(LOW));
- for (i=1;i<=n;i++)
- if (!DFN[i])
- tarjan(i);
- }
- int main()
- {
- freopen("in.txt","r",stdin);
- int i,j,k;
- cnt_edge=0;
- memset(node,-1,sizeof(node));
- scanf("%d%d",&n,&m);
- for(i=1;i<=m;i++)
- {
- scanf("%d%d",&j,&k);
- add_edge(j,k);
- }
- solve();
- for(i=1;i<=n;i++)
- printf("%d ",Belong[i]);
- }
- </pre><br>
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); }}