Algo-Phantoms / Algo-Tree

Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
MIT License
363 stars 619 forks source link

Detect a cycle in a directed graph #2056

Closed ranisnehal closed 3 years ago

ranisnehal commented 3 years ago

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

github-actions[bot] commented 3 years ago

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.

github-actions[bot] commented 3 years ago

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

ranisnehal commented 3 years ago

/ 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)

ranisnehal commented 3 years ago

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

ranisnehal commented 3 years ago

2056 DETECTING NUMBER OF CYCLES IN A DIRECTED GRAPH