关联点在Tarjan的实施中反复出现

时间:2015-08-29 11:52:51

标签: c++ algorithm graph tarjans-algorithm

我最近学会了线性时间算法来计算图表中的关节点。我的实现在在线评判测试数据上正确运行,因此代码没有问题。但是,我似乎很难在DFS运行中如何在多个关节点出现多个关节点。让我解释一下

我有一个列表,存储关节点,如果遇到它们。现在,当我最后打印列表时,我得到了正确的关节点,但是列表中出现了多个关节点出现不止一次的顶点。根据我的说法,这不应该发生,因为我们只遇到每个顶点ONCE。那么为什么我会在列表中重复输入?为了解决这个问题,我在原始代码中使用了一个HashSet来存储它们,然后只打印出最终的内容,从而给出了正确的答案。这是我的问题代码。该算法主要基于维基百科上的伪代码:https://en.wikipedia.org/wiki/Biconnected_component

这是我在C ++中实现的代码:

/*input
7 6
0 1
1 2
3 4
2 4
2 6
5 2
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb emplace_back
#define sz 3005 //In the current scenario, I need only a maximum on 3000 vertices

typedef long long int ll;

//Created by Shreyans Sheth [bholagabbar]

bool visited [sz]; //whether the node has been discoverd in the DFS run or not
int low [sz]; //time of the earliest discovered vertex reachable from the vertex
int disc [sz]; //time at which vertex was explored
int parent [sz]; //stores the parents of each vertex
vector<int> a[sz]; //Adjacency List for graph
int rtime; //Time
vector<int> ap; //Stored the articulation points

void DFS(int s)
{
    visited[s]=1;
    low[s]=disc[s]=++rtime;
    int nchild=0;
    for(auto i:a[s])
    {
        if(!visited[i])
        {
            nchild++;//INcrement children of the current vertex
            parent[i]=s;
            DFS(i);
            low[s]=min(low[s],low[i]);
            /* s is an articulation point iff
             1. It the the root and has more than 1 child.
             2. It is not the root and no vertex in the subtree rooted at one of its
                children has a back-link to its ancestor.
                A child has a back-link to an ancestor of its parent when its low
                value is less than the discovery time of its parent.*/
                if((parent[s]==-1 && nchild>1)||(parent[s]!=-1 && low[i]>=disc[s]))
                    ap.pb(s);//Adding the articulation points. How are they repeated?
        }
        else if(visited[i] && i!=parent[s])
            low[s]=min(low[s],disc[i]);
    }

}

void ArticulationPoints(int n)//Driver Funtion
{
    ap.clear();
    rtime=0;//The time for each cycle of DFS
    for(int i=0;i<n;i++)
    {
        parent[i]=-1;//Initializing parents as -1. True for roots
        visited[i]=0;//All points not visited
        low[i]=disc[i]=INT_MAX;
    }
    for(int i=0;i<n;i++)
        if(!visited[i])//Vertex not discoverdd
            DFS(i);
}

int main()
{
    int n,m;//number of vertices, edges
    cin>>n>>m;
    for(int i=0;i<m;i++)//Building Graph
    {
        int x,y;
        cin>>x>>y;
        a[x].pb(y);
        a[y].pb(x);
    }
    ArticulationPoints(n);//Calculating Articulation points
    cout<<"Articulation Points are:\n";
    for(int i:ap)
        cout<<i<<endl;
    return 0;
}

带输入和输出的代码:http://ideone.com/u5dYOy(看看2如何出现三次?)

为什么会这样?我在算法中遗漏了什么。我相信我对算法的运行有一个很好的想法。任何帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:1)

#include <bits/stdc++.h>

<强> Don't do this.

除此之外,您的代码以多种方式偏离伪代码。作为参考,这是您链接到的伪代码:

GetArticulationPoints(i, d)
    visited[i] = true
    depth[i] = d
    low[i] = d
    childCount = 0
    isArticulation = false
    for each ni in adj[i]
        if not visited[ni]
            parent[ni] = i
            GetArticulationPoints(ni, d + 1)
            childCount = childCount + 1
            if low[ni] >= depth[i]
                isArticulation = true
            low[i] = Min(low[i], low[ni])
        else if ni <> parent[i]
            low[i] = Min(low[i], depth[ni])
    if (parent[i] <> null and isArticulation) or (parent[i] == null and childCount > 1)
        Output i as articulation point
  1. 您没有d参数。相反,您增加一个全局变量。但是你永远不会减少这个变量,因此当你访问更多节点时它会不断增长。在伪代码中,d表示您在树中的当前深度。两个兄弟姐妹应该有相同的深度,但在你的情况下,一个人会有更大的深度。

    据我所知,这对于这种算法没有任何区别,但是,如果你不遵循伪代码,它通常也会成为bug的根源。无论如何都应该避免全局变量。

    解决方案:在您的函数中添加int d参数并像伪代码一样处理它:通过递归调用函数时向其添加+ 1。初始值可以是任何值,但通常设置为01

  2. 您的if条件,比伪代码更复杂。我不知道他们是否一定是错的,但是这与你使用的不同名字一起可能会引入错误。如果是第一次实施并且您非常依赖伪代码,我建议您坚持其风格。

    解决方案:DFS功能更改为:

    void DFS(int s, int d)
    {
        visited[s]=1;
        low[s]=disc[s]=d;
        int nchild=0;
        int isArticulation = 0;
        for(auto i:a[s])
        {
            if(!visited[i])
            {
                nchild++;//INcrement children of the current vertex
                parent[i]=s;
                DFS(i, d + 1);
                low[s]=min(low[s],low[i]);
                /* s is an articulation point iff
                 1. It the the root and has more than 1 child.
                 2. It is not the root and no vertex in the subtree rooted at one of its
                    children has a back-link to its ancestor.
                    A child has a back-link to an ancestor of its parent when its low
                    value is less than the discovery time of its parent.*/
                    if (low[i] >= disc[s])
                        isArticulation = 1;
            }
            else if(i != parent[s])
                low[s] = min(low[s], disc[i]);
        }
    
        if ((parent[s] != -1 && isArticulation) || (parent[s] == -1 && nchild > 1))
            ap.pb(s); 
    } 
    

    您的if条件中有最后一个if not visited,我猜测是导致您重复的原因(因为可能有多个i low[i] >= disc[s]虽然我没有检查它,所以你存储了所有这些的关节点)。

  3. 我还建议你使用更好的变量名,这样你才能知道代表什么。它将使算法的实际逻辑也更容易理解。