后向和前向边缘 [英] back and forward edges in directed graph

查看:233
本文介绍了后向和前向边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的图论。我编写一个代码,以找到有向图中的所有前进和后沿。我搜索和实现的代码如下。
我的代码进入无限循环。
请你看看。我们会非常感谢你。

I am new to graph theory. I am writing a code to find all forward and back edges in directed graph. I searched and implemented the code as below. My code is going into infinite loop. Could you please have a look. We will very greateful to you.

void dfsVisit(u)
      {
        u->color="gray";
        u->time=count;
        count++;
        for(every child node v of u)
        {
           if(v->color == "black")
           {
              if(u->time < v->time)
                  cout<<"Edge "<<u<<"->"<<v<<" is forward edge"<<endl;
              else
                  cout<<"Edge "<<u<<"->"<<v<<" is cross edge"<<endl;
            }
            if(v->color == "gray")
                 cout<<"Edge "<<u<<"->"<<v<<" is back edge"<<endl;
           if(v->color == "white")
                 cout<<"Edge "<<u<<"->"<<v<<" is tree edge"<<endl;
          dfsVisit(v); 
         }
         u->color="black";
         u->time=count;
         count++;
    }


推荐答案

您的代码:


  1. 您为每个孩子呼叫 dfsVisit 其颜色。你缩进到一个更深的层次,但是你没有在白色附加任何括号,如果 - 基于这个伪代码,只有 cout 受if语句影响。
    但是因为这只是一个伪代码,你应该发布你写的原始C ++代码清楚。

  1. you call dfsVisit for every child, regardless of its color. You indented it into a deeper level, but you didn't add any parenthesis around the white if - based on this pseudocode, only the cout is affected by the if statement. But since this is just a pseudocode, you should post the original C++ code you wrote to be clear.

(灰色,黑色和白色),但你从来没有在任何地方设置白色。在调用 dfsVisit 之前,您的边会初始化为白色? (你应该考虑使用枚举或枚举类的颜色,它是cleanear,你不太可能导致错别字的错别字)

you have three colors in your conditions (grey, black, and white), but you never set white anywhere. Your edges are initialized to white before you call dfsVisit? (you should consider using an enum or enum class for the colors, it's cleanear and you'll less likely to cause bugs with typos)

这篇关于后向和前向边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆