Related concepts:
If the two nodes in the diagram can be mutually accessible, it is said that two nodes are strongly connected.
If there is strong connectivity to every two nodes of the graph G, the G is a strongly connected graph.
A strongly connected sub-graph of a forward graph (not contained by other strongly connected sub-graphs), called a strongly connected component. (This definition is not quite the same in Wikipedia and other great gods ' blogs, but in the definition of Wikipedia)
The function of Tarjan algorithm is to find the strong connected component in the graph.
Ideas:
Defines the order in which the Dfni stores access to the I node (timestamp), lowi the access order of the nodes with the lowest access order (i.e., the some nodes go back to the smallest DFN value that can be found ), to find the non-visited node when the stack, when found a strong connected component of the root node (judging condition dfni==lowi), the node to the top of the stack, as a strong connected component
Starting at Node 1, enumerate each of the nodes that are not visited, take the node as a point u, stack, assign dfnu=lowu=time++, enumerate each edge starting at U, and find the point at the end of v to determine:
(1)V has not been visited, then the beginning of a deep search with V, and do lowu=min (LOWU,LOWV);
(2)V is still in the stack, then do lowu=min (LOWU,DFNV); (if the access order of V is smaller, update the LOWU, update the low value of the parent node of U when backtracking )
When the enumeration is complete, it is determined whether the node is the root of a strong connected component, and then the fallback operation is performed.
Deduction Example:
starting from 1 deep search,1,2,4 sequentially into the stack
dfn={1,2,0,3,0,0}
Search to 4 o'clock there is a side pointing to 1, low4=dfn1=1
6 in-Stack
dfn={1,2,0,3,0,4}
Judgment DFN6==LOW6,6 is the root node of the first strongly connected component, from 6 to the top of the stack (actually an element), the first strong connected component is {6}
Go back to 1 and update the low value
dfn={1,2,0,3,0,4}
low={1,1,0,1,0,4}
continue deep Search to 3, another side pointing to 4, Low3=dfn4=3
5 into the stack, with one edge pointing to 6, but 6 being accessed and not inside the stack,pass
At this point 5 satisfies the condition out of the stack, the second strong connectivity component is {5}
Back to 1, update low value
dfn={1,2,5,3,6,4}
low={1,1,3,1,6,4}
The last 1 satisfies the condition, from 1 to the stack top, the third strong connectivity component is {1,2,3,4}
Complexity of:
Each point and edge is accessed only once, with a time complexity of O (n+m)
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMaxn6 #defineMaxm7 intN,M,TIME,CNT,HEADS[MAXN],DFN[MAXN],LOW[MAXN],STACK[MAXN],TOP,BELONG[MAXN];//time is the access order, belong the node belongs to the first few strong connected components .8 BOOLVIS[MAXN];//whether the node is inside the stack.9 structnodeTen { One intV,next; A }EDGE[MAXM]; - voidAddintXinty) - { theedge[++cnt].next=Heads[x]; -heads[x]=CNT; -edge[cnt].v=y; - } + voidTarjan (intu) - { +dfn[u]=low[u]=++Time ; Avis[u]=true; atStack[++top]=u;//into the stack - for(inti=heads[u];i!=0; i=edge[i].next) - { - intv=edge[i].v; - if(! DFN[V])//has been accessed, DFN initial value is 0, only access will be assigned - { in Tarjan (v); -low[u]=min (low[u],low[v]); to } + Else if(Vis[v]) low[u]=min (low[u],dfn[v]); - } the if(Dfn[u]==low[u])//the node is the root node, the stack . * { $ inti;Panax Notoginsengcnt++; - Do the { +i=stack[top--]; Avis[i]=false; thebelong[i]=CNT; +Print ();//operations such as output -} while(u!=i); $ } $ } - intMain () - { thescanf"%d%d",&n,&m); - for(intI=1; i<=m;i++)Wuyi { the intx, y; -scanf"%d%d",&x,&y); WuAdd (x, y);//default input has a forward edge - } AboutCnt=0;//CNT is the number of strongly connected components $ for(intI=1; i<=n;i++) - if(!Dfn[i]) Tarjan (i); - return 0; -}
Reference
Http://baike.baidu.com/link?url=3xvU8jjqA2McnNL07G_5XHs8so96ZLAwjmc5oMPY1K67EkixIzyHdrn6QhqCsM9da0SVN_9vvca4iEcXJMyRVK
Http://blog.sina.com.cn/s/blog_69a101130100k1cm.html
Http://blog.sina.com.cn/s/blog_691ce2b701016u53.html
Graph theory-strong connected component-tarjan algorithm