c++ - back and forward edges in directed graph -


i new graph theory. writing code find forward , edges in directed graph. searched , implemented code below. code going infinite loop. please have look. greateful 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<<" forward edge"<<endl;               else                   cout<<"edge "<<u<<"->"<<v<<" cross edge"<<endl;             }             if(v->color == "gray")                  cout<<"edge "<<u<<"->"<<v<<" edge"<<endl;            if(v->color == "white")                  cout<<"edge "<<u<<"->"<<v<<" tree edge"<<endl;           dfsvisit(v);           }          u->color="black";          u->time=count;          count++;     } 

i see 2 possible problems code:

  1. you call dfsvisit every child, regardless of color. indented deeper level, didn't add parenthesis around white if - based on pseudocode, cout affected if statement. since pseudocode, should post original c++ code wrote clear.

  2. you have 3 colors in conditions (grey, black, , white), never set white anywhere. edges initialized white before call dfsvisit? (you should consider using enum or enum class colors, it's cleanear , you'll less cause bugs typos)


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -