图中的前沿

时间:2015-10-06 14:49:54

标签: c++ algorithm graph

我试图找出图G中的所有前向和后沿。我习惯于在back and forward edges in directed graph提供伪代码。具体如下:

DFS-Visit(u)▷带边缘分类。 G必须是有向图

1.        color[u] ← GRAY
2.        time ← time + 1
3.        d[u] ← time
4.        for each vertex v adjacent to u
5.            do if color[v] ← BLACK
6.                then if d[u] < d[v]
7.                            then Classify (u, v) as a forward edge
8.                            else Classify (u, v) as a cross edge
9.                        if color[v] ← GRAY
10.                            then Classify (u, v) as a back edge
11.                       if color[v] ← WHITE
12.                            then π[v] ← u
13.                                 Classify (u, v) as a tree edge
14.                                 DFS-Visit(v)
15.        color[u] ← BLACK
16.        time ← time + 1
17.        f[u] ← time

根据算法,如果我的节点是黑色并且d [u]&lt; d [v]它是前沿。

Graph With forward edges

现在如果我们在上面的图边缘0 - >中清楚地看清楚3是前缘。所以,我的要求是找到图中的所有前沿。我使用了上面的伪代码,但它混淆了前向和交叉边缘。我根据上面的伪代码编写了下面显示的c ++代码。能不能让我知道我哪里错了。

请帮帮我。我花了很多时间找到前沿,没有什么对我有用。

以下是我的代码:

// Note  that all nodes has already been marked with white color 
dfsVisit(LinkedList* vertex, int count)
{
    if(!vertex->visited)
    {
        vertex->color="gray";
        vertex->times=count;
        count++;
        for(map<LinkedList*, string>::iterator ad=vertex->childaddr.begin(); ad!=vertex->childaddr.end(); ad++)
        {
            if(ad->first->color == "black")
            {
                if(vertex->times < ad->first->times)
                    cout<<vertex->times<<" "<<ad->first->times<<": "<<"Edge "<<vertex->node<<"->"<<ad->first->node<<" is forward edge"<<endl;
                else
                    cout<<vertex->times<<" "<<ad->first->times<<": "<<"Edge "<<vertex->node<<"->"<<ad->first->node<<" is cross edge"<<endl;
            }
            if(ad->first->color == "gray")
            {
                cout<<vertex->times<<" "<<ad->first->times<<": "<<"Edge "<<vertex->node<<"->"<<ad->first->node<<" is back edge"<<endl;
            }
            if(ad->first->color == "white")
                cout<<vertex->times<<" "<<ad->first->times<<": "<<"Edge "<<vertex->node<<"->"<<ad->first->node<<" is tree edge"<<endl;
            vertex->visited=true;
            dfsVisit(ad->first, count); 

        }
        vertex->color="black";
    }
}

0 个答案:

没有答案
相关问题