Tarjan algorithm--The cut point and the bridge of the non-direction graph

Source: Internet
Author: User

I. Basic CONCEPTS

1. Bridge: is in the undirected diagram of such an edge, if you remove this edge, then the entire undirected graph will be divided into two parts, such an edge is called a bridge undirected graph, if the deletion of an edge, the diagram becomes disconnected, it is called the bridge.

2. Cut point: In the undirected connected graph, if a point is deleted, the graph becomes disconnected, then the point is said to be a cut point.

Second: The application of Tarjan algorithm in finding bridge and cutting point

1. Cut point: 1) The current node is the root of the time, the condition is "to have an extra subtrees tree" (if there is a subtree, remove this point also has no effect, if there are two sub-trees, remove this, two sub-tree is not connected.) )

2) The current node U is not the root of the time, the condition is "low[v]>=dfn[u", that is, after you traverse the point, can be turned upward, up to u, if you can turn to the top of U, then there is a ring, after removing u, the diagram is still connected.

Make sure v up to u can be turned up

2. Bridge: If a non-u,v is a bridge,

1) When and only if the u,v is a branch edge, it is necessary to meet the DFN (u) <low (v), that is, v up to u and above the point, then the u--v must be able to have 1 or more edges can not be deleted, because they have a part of the non-ring, is the bridge.

If V can turn up to u then U--v is a ring, deleting one of the paths, can be connected.

3. Note the point:

1) When seeking the bridge: because the side is no direction, so the father and child node relationship needs to set their own,

In the process of Tarjan if (V is not the parent node of u) low[u]=min (Low[u],dfn[v]);

Because if V is the father of U, then this non-forward side is mistaken for a ring.

2) When looking for a bridge: pay attention to see if there is a heavy edge, there must be a heavy edge is not a bridge, but also to avoid miscalculation.

4. It is also possible to Tarjan (), find out the DFN and low of each point, and record the parent node of each point in the DFS process, traverse the LOW,DFN of all points to look for bridges and cut points

Three: the template for bridging and cutting points:

#include <iostream>using namespacestd; #include<cstdio>#include<cstring>#include<vector>#defineN 201Vector<int>G[n];intN,m,low[n],dfn[n];BOOLIs_cut[n];intFather[n];inttim=0;voidinput () {scanf ("%d%d",&n,&m); intb;  for(intI=1; i<=m;++i) {scanf ("%d%d",&a,&b); G[a].push_back (b);/*adjacency table stores non-forward edges*/G[b].push_back (a); }}voidTarjan (intIintFather) {Father[i]=father;/*Record every point of the Father*/Dfn[i]=low[i]=tim++;  for(intj=0; J<g[i].size (); + +j) {intk=G[i][j]; if(dfn[k]==-1) {Tarjan (k,i); Low[i]=min (low[i],low[k]); }        Else if(father!=k)/*If K is the father of I, then this is the non-edge of the heavy edge, there is a heavy edge so it must not be the bridge*/Low[i]=min (low[i],low[k]); }}voidcount () {introotson=0; Tarjan (1,0);  for(intI=2; i<=n;++i) {intv=Father[i]; if(v==1) Rootson++;/*Count the number of tree nodes in the root node, the number of subtree in the root >=2, is the cut point*/        Else{            if(Low[i]>=dfn[v])/*conditions for cutting points*/Is_cut[v]=true; }    }    if(rootson>1) is_cut[1]=true;  for(intI=1; i<=n;++i)if(Is_cut[i]) printf ("%d\n", i);  for(intI=1; i<=n;++i) {intv=Father[i]; if(v>0&AMP;&AMP;LOW[I]&GT;DFN[V])/*conditions of the bridge*/printf ("%d,%d\n", V,i); }    }intMain () {input (); memset (DFN,-1,sizeof(DFN)); memset (father,0,sizeof(father)); memset (Low,-1,sizeof(low)); memset (Is_cut,false,sizeof(Is_cut));    Count (); return 0;}

Tarjan algorithm--The cut point and the bridge of the non-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.