0

I have an adjacency matrix for a graph. And I need to output all the cycles in this graph. How can I do this using dfs?

void al_dfs(int tmp_vertex,int** Graph,vector<int>& color,int number,vector<int>& result){
    color[tmp_vertex] = 1;
    for(int i = 0;i<number;i++){
        if((color[i] == 0)){ al_dfs(i,Graph,color,number,result); result.push_back(i); };
        if((color[i] == 1)){
            for(int i = 0;i< result.size();i++) cout << result[i] << " ";
        }
    }
    color[tmp_vertex] = 2;
}

I wrote a small function, but I understand that there is a lot of things that have not been completed yet.

  • 1
    Are you looking for algorithmic help or do you know how you think about it and have a specific problem coding it? I don't understand why you're setting the color to `2`, for example – lucidbrot Apr 17 '21 at 18:59
  • @lucidbrot I'm looking for algorithmic help.2 means the vertex is processed. – Eugenos_Programos Apr 17 '21 at 19:13
  • Maybe anything [here](https://stackoverflow.com/questions/546655/finding-all-cycles-in-a-directed-graph#comment2892719_546655) is helpful to you? The second-highest voted answer there does not seem to me like it does what you stated. What exactly are your requiements? Just detecting whether there is at least one circle is way easier than *printing* *all* circles. And do you specifically want to do this "using bfs"? – lucidbrot Apr 17 '21 at 19:30
  • Why are you passing Graph as parameter? You're not using it. Try to be more complete. It's rather difficult to help you with so little info. – JHBonarius Apr 17 '21 at 19:43

0 Answers0