Connected graph/cut point, cut edge

First understand the meaning of each noun

Undirected graph: A graph
without direction Undirected connected graph: An undirected graph in which any two nodes in the graph can reach each other Undirected
disconnected graph: Not an undirected connected
graph Subgraph
cut point: if this point is deleted, it is not connected .
Cut edge: if this edge is deleted, it is not connected
. Strongly connected directed graph: a directed graph that can be reached by any two points
. Strongly Connected Components (SCC) of
Undirected Connected Graphs: Maximally Connected Subgraphs of Non-Strongly Connected Graphs

For finding cut points and cut edges in an undirected graph

Why there are cut points and non-cut points, that is, because the undirected graph is connected with a ring, the points in the ring are naturally not cut points. If the undirected connected graph is a tree, then all points are cut points.

For the Tarjan algorithm, select a root node for dfs search, and use the dfn array to store the occurrence time of each node (the time starts from 1, and 0 means that it has not been searched)

If there is a ring in the graph, there must be first (the earliest node) and last (the latest node) in the ring, and last and fisrt have an edge that can be directly connected, and when last searches for the next node, it will be found first

At this time, a low array is needed to store the earliest occurrence time that can be searched for each point. Only when fisrt is searched for last in the ring, the low array can become smaller, and it will make all the nodes in the tree chain from fisrt->last. Point i so that low[i]=min(low[i],dfn[first]) (the code should be low[u]=min(low[u],low[v]))

After searching all points

If the cut point is obtained, each point can be divided into a root node and a non-root node

For the root node, if there are two child nodes, then the point is the cut point (for how to judge how many child nodes there are, use a fa[] array to store and transfer it when searching, and finally judge how many are by The root node can be converted)

For node i that is not the root, if low[i]>=dfn[fa[i]], the father of the point is a cut point (because the point does not pass through the edge of this point and the father can only search for Father's Day at most, then delete it The parent node, the node above the parent cannot be found)

If you want to cut edges, you only need to search for each point i that is not the root node. If low[i]>dfn[fa[i]], then the edge connected to the parent is a cut edge (because if the point can be searched for The earliest point that appears is later than that of the father, which means that the point above the father cannot be searched)

code show as below

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int dfn[1005],low[1005],fa[1005];
bool vis[1005];
vector<int>G[1005];
int cnt;
void tarjan(int u)
{
    dfn[u]=low[u]=++cnt;
    int i,j;
    for(i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(dfn[v]==0)
        {
            fa[v]=u;
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
}
int gedian(int n)
{
    int num=0;
    memset(vis,false,sizeof(vis));
    int i,j,fu;
    for(i=2; i<=n; i++)
    {
        fu=fa[i];
        if(fu==1)
            num++;
        else
        {
            if(low[i]>=dfn[fu])
                vis[fu]=true;
        }
    }
    if(num>=2)
        vis[1]=true;
    int sum=0;
    for(i=1; i<=n; i++)
    {
        if(vis[i])
            sum++;
    }
    return sum;
}
int gebian(int n)
{
    int i,j,sum=0,fu;
    for(i=1;i<=n;i++)
    {
        fu=fa[i];
        if(fu>0&&low[i]>dfn[fu])
            sum++;
    }
    return sum;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=1; i<=n; i++)
            G[i].clear();
        int x,y;
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        memset(dfn,0,sizeof(dfn));
        cnt=0;
        memset(fa,0,sizeof(fa));
        tarjan(1);

    }
}
After running taijan once, the directed graph becomes a tree, which must be discarded from the original graph ring, but precisely because this edge changes the low value, because the tree points from top to bottom, only this edge goes to The point is pointed up, so that
the low value of the lower point of the edge can become the dfn value of the upper point to
find the cut point.
For the root, if there are two subtrees, the point is a cut point.
Otherwise, if the point u has a child node v, such that low[v]> =dfn[u], then u is the cut point to
find the cut edge.
For a child node v of an edge, the parent node u has low[v]>dfn[u], then the edge is a cut edge

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324960278&siteId=291194637
cut