Graph theory--the marking and indentation template of strongly connected components of a direction graph

Source: Internet
Author: User

The direction of the graph in if between two points can reach each other, it is said that the two points strong connectivity, if all points within a point set can reach each other, then the point set is a strong connected component of the graph, and we need to find all the strongly connected components of the graph, so the Tarjan algorithm to strong connectivity, and a connected block is shrunk to a point so that it can form a direction-free graph, which is helpful for solving problems.

The method for finding strong connected components is that DFS looks for a point and the furthest ancestor node that its successor can reach, if the furthest ancestor node is the point of entry to DFS, indicating that all the subsequent nodes searched are in this strongly connected component, and then they are labeled as the same strong connected component.

Head, point, NXT, size are realized chain forward star, opened two, subscript 0 is the original, subscript 1 is the image after the contraction point. n is the total number of points, and T is the time axis of the DFS used to record the sequential access order in the Dfs tree and to determine the number of the farthest ancestor nodes to the successor point. STX is the label of each node on the DFS timeline, and low is the farthest ancestor node that each node can reach through its successor node, which is the number of strongly connected components each node belongs to, and scccnt is the total number of strongly connected components.

The subject is modified from a template on a large petition.

There are comments:

1#include <stdio.h>//These header files are required2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 //MAXN is the total number of points, MAXM is the total number of edges (original)8 Const intmaxn=1005;9 Const intmaxm=6005;Ten  One inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2];//0 is the original, 1 is the image after the indentation . A intn,t,scccnt;//N is the number of points, T is the time axis, scccnt is a strongly connected component - intSTX[MAXN],LOW[MAXN],SCC[MAXN];//STX is the time axis designator of the point, low is the farthest ancestor node that can be reached, and the SCC is the strongly connected component number that the point belongs to. -stack<int>S;//It is used to store the points in the strong connected components, and finally joins the strong connected components in sequence . the  - voidInit () {//initialization of the original artwork -memset (head,-1,sizeof(head)); -size[0]=size[1]=0; + } -  + voidAddintAintBintC=0){//Add Edge function, the original is 0, the original image can not pass in the parameter C, the indent graph is 1 Apoint[c][size[c]]=b; atnxt[c][size[c]]=Head[c][a]; -head[c][a]=size[c]++; - } -  - voidDfsints) { -Stx[s]=low[s]=++t;//The node's timeline designator, which can be accessed from the farthest ancestor is its own inS.push (s);//press the S point into the stack -      for(inti=head[0][s];~i;i=nxt[0][i]) { to         intj=point[0][i]; +         if(!stx[j]) {//If J has not been accessed, it means that there is no strong connected component that has been previously set, and the search continues from this point - Dfs (j); theLow[s]=min (Low[s],low[j]);//Update the farthest ancestors you can reach with the successor Point *         } $         Else if(!scc[j]) {//if SCC[J] already has a value indicating that J is already a point in the other strongly connected components, then DFS does not have a search for S point indicating that s point and J Point are not in the same strong connected component, so there is no need to processPanax Notoginsenglow[s]=min (low[s],stx[j]); -         } the     } +     if(Low[s]==stx[s]) {//oneself and the furthest ancestor that the successor can visit is himself, then all the points after this are the points of this strong connected component, and the points from the stack are marked Ascccnt++; the          while(1){ +             intu=S.top (); S.pop (); -scc[u]=scccnt; $             if(S==u) Break; $         } -     } - } the  - voidSETSCC () {Wuyimemset (STX,0,sizeof(STX)); thememset (SCC,0,sizeof(SCC)); -T=scccnt=0; Wu      for(intI=1; i<=n;++i)if(!stx[i]) DFS (i);//In turn, DFS -      for(intI=1; i<=n;++i) { About          for(intj=head[0][i];~j;j=nxt[0][j]) { $             intk=point[0][j]; -             if(Scc[i]!=scc[k]) {//two points are not in the same strong connected component, then the two strongly connected components inherit the forward edge of the two point.  -Add (Scc[i],scc[k],1); -             } A         } +     } the}

Wood has notes:

1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1005;8 Const intmaxm=6005;9 Ten inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2]; One intn,t,scccnt; A intSTX[MAXN],LOW[MAXN],SCC[MAXN]; -stack<int>S; -  the voidinit () { -memset (head,-1,sizeof(head)); -size[0]=size[1]=0; - } +  - voidAddintAintBintC=0){ +point[c][size[c]]=b; Anxt[c][size[c]]=Head[c][a]; athead[c][a]=size[c]++; - } -  - voidDfsints) { -stx[s]=low[s]=++T; - S.push (S); in      for(inti=head[0][s];~i;i=nxt[0][i]) { -         intj=point[0][i]; to         if(!Stx[j]) { + Dfs (j); -low[s]=min (low[s],low[j]); the         } *         Else if(!Scc[j]) { $low[s]=min (low[s],stx[j]);Panax Notoginseng         } -     } the     if(low[s]==Stx[s]) { +scccnt++; A          while(1){ the             intu=S.top (); S.pop (); +scc[u]=scccnt; -             if(S==u) Break; $         } $     } - } -  the voidSETSCC () { -memset (STX,0,sizeof(STX));Wuyimemset (SCC,0,sizeof(SCC)); theT=scccnt=0; -      for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); Wu      for(intI=1; i<=n;++i) { -          for(intj=head[0][i];~j;j=nxt[0][j]) { About             intk=point[0][j]; $             if(scc[i]!=Scc[k]) { -Add (Scc[i],scc[k],1); -             } -         } A     } +}

Graph theory--the marking and indentation template of strongly connected components of a direction graph

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.