Tarjian algorithm to find strong Unicom component

Source: Internet
Author: User

If two vertices can be connected to each other, the two vertices are strongly connected (strongly connected). If there is strong connectivity to every two vertices of the graph G, the G is a strongly connected graph. Strongly connected graphs have a strongly connected sub-graph of the graph, called the strong connected component (strongly connected components). , the sub-diagram {1,2,3,4} is a strongly connected component because the vertex 1,2,3,4 22 is up. {5},{6} is also a two strong connected component. The Tarjan algorithm is used to find the strong connected component of the graph. The Tarjan algorithm for the strong connected component of the graph is named after its inventor Robert Tarjan. Robert Tarjan also invented the Tarjan algorithm for finding two connected components. Tarjan algorithm is based on graph depth First search algorithm, each strong connected component is a subtrees tree in the search tree. When searching, the unhandled node in the current search tree is added to a stack, which can be used to determine if the node in the stack is a strong connected component.defines the order number (timestamp) of the DFN (U) search for a node u, and the number of nodes in the oldest stack that can be traced back to the Low (U) Tree of U or U. When DFN (u) =low (u), all nodes on the search subtree with the root of u are a strongly connected component. The next step is a demonstration of the algorithm flow. Start Dfs from Node 1 and add the traversed node to the stack. When searching for node u=6, Dfn[6]=low[6], a strongly connected component was found. Until U=v, {6} is a strongly connected component. Return node 5, found Dfn[5]=low[5], after the stack {5} is a strong connected component. Return to Node 3, continue to search for node 4, add 4 to the stack. It is found that node 4 has a back edge to Node 1, and Node 1 is still in the stack, so low[4]=1. Node 6 has been out of the stack, (4,6) is the cross side, return 3, (3,4) for the branch edge, so low[3]=low[4]=1. Continue back to Node 1 and finally Access Node 2. Access Edge (2,4), 4 is still in the stack, so low[2]=dfn[4]=5. Returns 1, finds Dfn[1]=low[1], takes all the nodes out of the stack and makes up a connected component {1,3,4,2}. At this point, the algorithm ends. Through this algorithm, we find all the three strong connected components {1,3,4,2},{5},{6} in the graph.It can be found that in the process of running the Tarjan algorithm, each vertex is accessed once and only once in and out of the stack, and each edge is accessed only once, so the time complexity of the algorithm is O (n+m).
structnode{intV,next;} E[M];inthead[n],cnt;intP[N],ST[N],ID,TOP,SCC;intDfn[n],low[n],belong[n];voidAddintUintv) {E[CNT].V=v,e[cnt].next=Head[u]; Head[u]=cnt++;}voidinit () {memset (head,-1,sizeof(head)); Memset (P,0,sizeof(p)); memset (DFN,0,sizeof(DFN)); ID=top=cnt=0;}voidDfsintu) {Dfn[u]=low[u]=++ID; st[++top]=u;p[u]=1; intv;  for(inti=head[u];i!=-1; i=E[i].next) {v=e[i].v; if(!Dfn[v])            {DFS (v); if(Low[v]<low[u]) low[u]=Low[v]; }Else if(p[v]&&dfn[v]<Low[u]) {Low[u]=Dfn[v]; }    }    if(dfn[u]==Low[u]) {        ++SCC;  Do{v=st[top--]; P[V]=0; BELONG[V]=SCC; } while(v!=u); }}voidTarjian (intN) {     for(intI=1; i<=n;i++){        if(!Dfn[i]) DFS (i); } printf ("%d\n", SCC);  for(intI=1; i<=n;i++) {printf ("%d%d\n", I,belong[i]); }}intMain () {intn,m,u,v; scanf ("%d%d",&n,&m);    Init ();  while(m--) {scanf ("%d%d",&u,&v);    Add (U,V);    } Tarjian (n); return 0;} 

Tarjian algorithm to find strong Unicom component

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.