Dual-connected component-tarjan

Source: Internet
Author: User

Point dual-connectivity component:

In a undirected graph, if any node in the graph cannot change the graph's connectivity, the graph is a two-connected undirected graph. A connected undirected graph is dual-connected, and only when it has no key points.

Strongly connected components:

In directed graph G, if two vertices vi and vj (vi <> vj) have a directed path from vi to vj, there is also a directed path from vj to vi, which is called two vertices that are strongly connected. If each vertex of directed graph G is strongly connected, G is a strongly connected graph. A very large strongly connected subgraph of A digraph of a non-strongly connected graph. It is called a strongly connected component.

Tarjan algorithm:

 

Tarjan (u) {DFN [u] = Low [u] = ++ Index // set the sequence number and Low initial value Stack for node u. push (u) // push node u into stack for each (u, v) in E // enumerate each side if (v is not visted) // If node v has not been accessed by tarjan (v) // continue to find Low [u] = min (Low [u], Low [v]) else if (v in S) // if node v is still in the stack, Low [u] = min (Low [u], DFN [v]) if (DFN [u] = Low [u]) // if the node u is the root of the strongly connected component, repeat v = S. pop // roll back v to stack, print v until (u = v) for a vertex in the strongly connected component )}

The essence of the tarjan algorithm:

Perform dfs on all nodes and record the time found at this point during traversal, which is stored in dfn. When y searches for a node x before

It indicates that the distance from node x to node y constitutes a strongly connected component. The specific code implementation is to put this point into the stack every time a vertex is searched, and the low Array records this section.

Point and the minimum dfn in the connected component of the node. When low [x] = dfn [x], it indicates that a connected component has been formed, then, the nodes in the stack are output to x.

The tarjan algorithm calculates point dual-connectivity:

 

Void tarjan (int x) {int I; low [x] = dnf [x] = times ++; for (I = head [x]; I! =-1; I = edge [I]. next) {if (vis [I]) continue; vis [I] = vis [I ^ 1] = 1; int y = edge [I]. e; if (! Dnf [y]) {st. push (I); tarjan (y); low [x] = min (low [x], low [y]); if (low [y]> = dnf [x]) // at this time, the points in the stack form a double-connected component {while (1) {yw = st. top (); st. pop (); if (edge [yw]. u = x) break ;}} else if (dnf [y] <dnf [x]) {st. push (I); low [x] = min (low [x], dnf [y]) ;}}
void tarjan(int x){    dnf[x]=low[x]=times++;    instack[x]=1;    qq.push(x);    for(int i=head[x];i!=-1;i=edge[i].next)    {        int y=edge[i].e;        if(vis[i])continue;        vis[i]=vis[i^1]=1;        if(!dnf[y])        {            tarjan(y);            low[x]=min(low[x],low[y]);        }        else if(instack[y])        {            low[x]=min(low[x],dnf[y]);        }    }    if(low[x]==dnf[x])    {        int y=-1;        while(x!=y)        {            y=qq.top();            qq.pop();            instack[y]=0;            num[y]=nums;        }        nums++;    }}

 

Related Article

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.