Algo-Phantoms/Algo-Tree

Detect a cycle in a directed graph

ranisnehal opened this issue · 5 comments

DETECT A CYCLE IN A DIRECTED GRAPH

We will be given a directed graph ,we have to check whether the graphs contains a cycle or not . our function should return true if the graph is containing at least one cycle else it will return false.

LANGUAGE:- IN C++

PLEASE ASSIGN THIS ISSUE TO ME.

THANK YOU:
SNEHAL RANI

Hi 😄, @ranisnehal Thanks for creating issue at AlgoTree, do read and follow the Code of Conduct and the Contribution Guidelines while contributing. Refer to PR's which has been merged earlier in AlgoTree Click Here Like, How many File they have changed?, Which type of files need to be change? and many more.

Hi 😄, thanks for creating issue at AlgoTree, do read and follow the Code of Conduct and the Contribution Guidelines while contributing.

/* Here is the solution of the program in c++*/
#include<bits/stdc++.h>
#include
using namespace std;

class solution{
public:

bool solve(int src , vector<int> &vis, vector<int> &order,vector<int> adj[]){
    vis[src]= 1;
    order[src]= 1;
    for(auto x:adj[src])
    {
      if(!vis[x])
       {   
         bool conf = solve(x,vis,order,adj);
         if(conf == true)
         return true;
        }
     else if(order[x])
      return true;
    }
    order[src] = 0;
    return false;
}

bool iscyclic(int v,vector adj[])
{
vector vis(v,0);
vector order(v,0);
for(int i=0;i<v;i++)
{
if(!vis[i])
{
bool c = solve(i,vis,order,adj);
if(c==true);
return true;
}
}
return false;
}
};

TIME COMPLEXITY:- O(V)

I have fix the issue #2056 in c++.

#2056 DETECTING NUMBER OF CYCLES IN A DIRECTED GRAPH #