diff --git a/Graphs/cycleDetectionInUndirectedGraph.cpp b/Graphs/cycleDetectionInUndirectedGraph.cpp new file mode 100644 index 0000000..5fe90ce --- /dev/null +++ b/Graphs/cycleDetectionInUndirectedGraph.cpp @@ -0,0 +1,113 @@ +/* + +Detect cycle in a Un Directed Graph : + +What you do in DFS ? + +Look for back edge. + +Can you use the same method here ? + +No. + +Why ? + +When you are looking foe a back-edge in a undirected graph , obviously the parent will be counted +i.e the node where it came from. +But that does not contribute to a cycle in a Undirected graph. + + +Approach : + +It's simple.Just keep checking for already visited nodes , Provided it is not the parent of the current +node.Then cycle exists :) + +*/ + +#include +using namespace std; + +bool iscyclic(int s,vector > &adj,vector &visited,int parent) +{ + + if(visited[s]==false){ + //Mark the current node as visited and print it + visited[s]=true; + vector :: iterator i; + // Recur for all the vertices adjacent to this vertex + for (i = adj[s].begin(); i != adj[s].end(); i++) + { + if (!visited[*i] && iscyclic(*i,adj,visited,s)) + { + return true; + } + //If you find any other node other than parent that is already visited + else if(*i!=parent) + { + return true; + } + } + + } + return false; + + + + +} + +int main() +{ + // Create a graph given in the above diagram + int vertices=7; + //Adjacency list representation of the graph is represented using 2-D vector + vector > adj(vertices); + adj[0].push_back(1); + adj[0].push_back(2); + adj[0].push_back(3); + adj[1].push_back(4); + adj[2].push_back(6); + adj[4].push_back(6); + adj[3].push_back(5); + vector visited(vertices,false) ; + + int start=0; + bool cyclic=false; + + //Looping if in case the graph is not connected + for(int i=0;i + +using namespace std; + +bool iscyclic(int s,vector > &adj,vector &visited,vector &rec) +{ + + if(visited[s]==false){ + //Mark the current node as visited and print it + visited[s]=true; + rec[s]=true; + + vector :: iterator i; + // Recur for all the vertices adjacent to this vertex + for (i = adj[s].begin(); i != adj[s].end(); i++) + { + if (!visited[*i] && iscyclic(*i,adj,visited,rec)) + { + return true; + } + //The node is in the recursion stack + else if(rec[*i]==true) + { + return true; + } + } + + } + + //Mark the current node in recursionn false as it has come out of the recursion stack + rec[s]=false; + return false; + + + + +} + +int main() +{ + // Create a graph given in the above diagram + int vertices=4; + //Adjacency list representation of the graph is represented using 2-D vector + vector > adj(vertices); + adj[0].push_back(1); + adj[0].push_back(2); + adj[1].push_back(2); + adj[2].push_back(3); + adj[2].push_back(0); + adj[3].push_back(3); + + vector visited(vertices,false) ; + vector rec(vertices,false) ; + + + + int start=0; + bool cyclic=false; + + for(int i=0;i1 + +0---->2 +2---->0 //While you are checking for 2's (i.e from 0) neighours you will find 0 in the rec stack + + +1---->2 + +3---->3 //Self loop + +2---->3 + +OUTPUT: + +Cycle is present + + +Time Complexity: O(V+E). +Space Complexity : O(V). + +*/ +