Two connected components of Tarjan three algorithms (two connected components)

Source: Internet
Author: User
Tags min

Definition:
For a connected graph, if any two points have at least two points do not repeat the path, it is said that the graph is a point double-connected (for short), if any two points there is at least two edges do not repeat the path, it is said that the graph is the edge of the two-connected . The definition of a point double connected graph is equivalent to any two edges being in a simple ring, while the definition of edge double connected graph is equivalent to any one edge in at least one simple ring. For an undirected graph, the maximal sub-graph of the point-double-connected is called the Point-double -connected component (the double-connected component), and the maximal sub-graph of the edge-double-connected is called the edge-connected component . This blog is to summarize the method of solving the two-connected component of undirected graph point and the double connected component of the edge.

algorithm:
In fact, the solution of the two-connected component and the edge-connected component is closely related to the solution of the cut point. At most, there is only one common point for the two connected components, that is, one cutting top, and any one cutting top is a common point with at least two points connected to each other. The two connected components of different sides have no common point, while the bridge is not in the two-connected component of any one side, the point double connected component must be a side double connected component.
The following first introduces the Tarjan algorithm of the point dual connected component :
In the previous blog, we already know how to solve the cut-off, it is easy to find that when we find the top of the cut, we have completed a single large double-connected sub-graph access, then we will be in the process of Dfs to save the traversed points, is not the point of the two connected components can be obtained. In order to implement the algorithm, we can save the traversed edges with a stack in the process of solving the cutting top (note not the point.) since the different two-connected components have a common point that is cut-top ), then whenever a point is found a double connected component, that is, the child node V and the parent node U satisfies the relationship low[v]>=dfn[u], we will take the stack of things out until the current edge.
Note that in the stack is not the point, but the edge, this is because the point of the two connected components there is a repetition point, if we put in the stack is a point, then for some points of the two connected components, there will be less points (these points are cut-top).
Code:

struct edge{int u,v;
Edge (int u=0,int v=0): U (U), V (v) {}}E[MAXM];
int N,M,STAMP,DFN[MAXN],LOW[MAXN],ISCUT[MAXN],BCCNO[MAXN];
int scnt,stack[maxm],bcc_cnt;

Vector<int> VEC[MAXN],BCC[MAXN];
    void Tarjan (int index,int fa) {int child=0,tmp;
    Dfn[index]=low[index]=++stamp;
        for (int i=0;i<vec[index].size (); i++) {tmp=e[vec[index][i]].v;
            if (!dfn[tmp]) {stack[++scnt]=vec[index][i],child++;
            Tarjan (Tmp,index);
            Low[index]=min (Low[index],low[tmp]);
                if (Low[tmp]>=dfn[index]) {iscut[index]=1;
                Bcc[++bcc_cnt].clear ();
                    while (1) {int num=stack[scnt--];
                        if (bccno[e[num].u]!=bcc_cnt) {bcc[bcc_cnt].push_back (E[NUM].U);
                    bccno[e[num].u]=bcc_cnt;
     } if (bccno[e[num].v]!=bcc_cnt)               {Bcc[bcc_cnt].push_back (E[NUM].V);
                    bccno[e[num].v]=bcc_cnt;
                } if (E[num].u==index && e[num].v==tmp) break; }}} and else if (Dfn[tmp]<dfn[index] && tmp!=fa) {stack[++scnt]=v
            Ec[index][i];
        Low[index]=min (Low[index], dfn[tmp]);
}} if (Fa<0 && child==1) iscut[index]=0;
    } void Find_bcc () {//BCCNO value of the cut top is meaningless memset (dfn,0,sizeof (DFN));
    memset (low,0,sizeof (Low));
    memset (iscut,0,sizeof (iscut));
    memset (bccno,0,sizeof (BCCNO));
    Memset (Bcc,0,sizeof (BCC));
    stamp=scnt=bcc_cnt=0;
for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i,-1); }

It is important to note that at the end of the algorithm, each node has a number that represents which point it belongs to, but the number of the cut-off is completely meaningless . This algorithm uses two time stamps and stacks flexibly, and completes the discovery of the point double connected component.
Example: Uvalive 5135

Then we introduce the solution algorithm of the side double connected component :
The solution of the side double connected component is very simple, because there is no common edge between the two connected components, and the bridge is not in any one of the two connected components, so the algorithm is very simple, that is, DFS first found all the bridge, once again Dfs (excluding the bridge) to find the edge of the dual connectivity components.
PS: Of course, it can be implemented with DFS one time.
Code:

struct edge{int u,v;
Edge (int u=0,int v=0): U (U), V (v) {}}E[MAXM];
int n,m,stamp,dfn[maxn],low[maxn],bccno[maxn],bcc_cnt;
Vector<int> VEC[MAXN],BCC[MAXN];

BOOL G[MAXN][MAXN],ISBRIDGE[MAXM];
    void Tarjan (int index,int fa) {int tmp;
    Dfn[index]=low[index]=++stamp;
        for (int i=0;i<vec[index].size (); i++) {tmp=e[vec[index][i]].v;
            if (!dfn[tmp]) {Tarjan (tmp,index);
            Low[index]=min (Low[index],low[tmp]);
        if (Low[tmp]>dfn[index]) isbridge[vec[index][i]]=isbridge[vec[index][i]^1]=1;
        } else if (Dfn[tmp]<dfn[index] && tmp!=fa) {low[index]=min (Low[index], dfn[tmp]);
    }}} void Dfs (int index) {dfn[index]=1;
    bccno[index]=bcc_cnt;
        for (int i=0;i<vec[index].size (); i++) {int tmp=vec[index][i];
        if (isbridge[tmp]) continue;
       if (!DFN[E[TMP].V]) {DFS (E[TMP].V); }}} void Find_ebcc () {bcc_cnt=stamp=0;
    memset (dfn,0,sizeof (DFN));
    memset (low,0,sizeof (Low));
    memset (isbridge,0,sizeof (Isbridge));
    memset (bccno,0,sizeof (BCCNO));
    Memset (Bcc,0,sizeof (BCC));
    for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i,-1);
    memset (dfn,0,sizeof (DFN));
            for (int i=1;i<=n;i++) {if (!dfn[i]) {bcc_cnt++;
        DFS (i); }
    }               
}

Example: POJ 3352

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.