0
#include<bits/stdc++.h>
 using namespace std;
  void addEdge(vector<vector<int > >&adj,int u,int v)
   {
     adj[u].push_back(v);
   }
   bool DFS(vector<vector<int > >adj,int x,vector<bool>&visited,vector<bool>&recStack,stack<int>&s)
    {
        if(recStack[x])
        {
            return true;
        }
       if(visited[x])
       {
       return false; // the node is already visited and we didn't a cycle with this node
      }
        visited[x]=true;
        recStack[x]=true;
       s.push(x);
       for(int i=0;i<adj[x].size();i++)
      {
            if(DFS(adj,adj[x][i],visited,recStack,s))
           {
             return true;
           }
      }
       recStack[x]=false;
       s.pop();
       return false;
    }
    bool graphHasCycle(vector<vector<int > >adj)
    {
          vector<bool>visited(adj.size(),false);
          vector<bool>recStack(adj.size(),false);
          stack<int>s;
         for(int i=0;i<adj.size();i++)
         {
           if(DFS(adj,i,visited,recStack,s))
            {
              while(!s.empty())
             {
            cout<<s.top()<<" ";
            s.pop();
              }
               return true;
              }
            }
           return false;
          }
         int main()
         { 
           /*int n=3,e=3;
           vector<vector<int > >adj(n,vector<int>());
           addEdge(adj,0,1);
            addEdge(adj,1,2);
            addEdge(adj,2,0);*/

            /*int n=3,e=3;
                    vector<vector<int > >adj(n,vector<int>());
                    addEdge(adj,0,1);
                   addEdge(adj,1,2);
                   addEdge(adj,0,2);*/

                /*int n=4,e=4;
               vector<vector<int > >adj(n,vector<int>());
              addEdge(adj,0,1);
              addEdge(adj,1,2);
               addEdge(adj,2,3);
               addEdge(adj,3,1);*/

               /*int n=5,e=6;
               vector<vector<int > >adj(n,vector<int>());
              addEdge(adj,0,1);
               addEdge(adj,1,2);
               addEdge(adj,0,2);
               addEdge(adj,0,3);
              addEdge(adj,3,4);
               addEdge(adj,4,0);*/

               cout<<"Adjacency list:\n";
for(int i=0;i<adj.size();i++)
{
    for(int j=0;j<adj[i].size();j++)
    {
        cout<<adj[i][j]<<" ";
    }
    cout<<endl;
}
cout<<"Cycles:\n";
if(graphHasCycle(adj))
{
    cout<<"Graph has cycle";
}
else
{
    cout<<"No cycle";
}
return 0;

}

Code source:'Detect cycle in directed graph' from GeeksForGeeks.

Im trying to modify this code to also print the cycles using this code,but it doesnt seem to work on all cases. for example in the graph with edges 0->1,1->2,2->3,3->1,it would print the cycle as 3 2 1 0 but the actual cycle is 3 2 1.It also fails when two cycles originate from the same vertex.

Please help me correct the code to print the cycles correctly.

Link to the code in online ide: https://ide.geeksforgeeks.org/ujCcvL77ii

Nithish
  • 139
  • 5
  • 1
    Wrong code. `` is not standard C++. Check [here](https://en.cppreference.com/w/cpp). Then read [*How to debug small programs*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Compile with all warnings and debug info (so `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) – Basile Starynkevitch May 21 '20 at 17:38
  • LOL.I'm pretty sure the code runs well.please read the question before answering:/ – Nithish May 21 '20 at 20:28

1 Answers1

1

I think before each DFS call, you need to re-initialize recStack to false as below:

for(int i=0;i<adj.size();i++)
{
    vector<bool>recStack(adj.size(),false);    //for each call we need an empty recStack
    stack<int>s;                            //initialize a new stack too.
    if(DFS(adj,i,visited,recStack,s))
hacker315
  • 1,996
  • 2
  • 13
  • 23
  • @Nithish , sry, I forgot, initilize a new stack too. – hacker315 May 21 '20 at 17:30
  • i'm glad your'e still trying to help.I tried reinitializing them both.but still i get the wrong output.you can edit the code and see to yourself here https://ide.geeksforgeeks.org/ujCcvL77ii – Nithish May 21 '20 at 20:40