Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cycle Detection in Graphs #71

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions Graphs/cycleDetectionInUndirectedGraph.cpp
Original file line number Diff line number Diff line change
@@ -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<bits/stdc++.h>
using namespace std;

bool iscyclic(int s,vector<vector<int> > &adj,vector<bool> &visited,int parent)
{

if(visited[s]==false){
//Mark the current node as visited and print it
visited[s]=true;
vector<int> :: 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<vector<int> > 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<bool> visited(vertices,false) ;

int start=0;
bool cyclic=false;

//Looping if in case the graph is not connected
for(int i=0;i<vertices;i++){
if(iscyclic(i,adj,visited,-1)){
cyclic=true;
break;

}
}



if(cyclic==true)
cout<<"Cycle is present";
else
cout<<"Cycle is not present";

return 0;
}
/*

INPUT :

1 - 4
/ \
0 -2 - 6
\
3- 5


OUTPUT:

Cycle is present


Time Complexity: O(V+E).
Space Complexity : O(V).
*/
123 changes: 123 additions & 0 deletions Graphs/cycleDetectioninDirectedGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Detect cycle in a Directed Graph:

When do you say a graph has a cycle ?

When there is a edge(back-edge) from any of it's children to ancestors.

Approach :

Use a another boolean array to keep track of the nodes visited during recursion.
While traversing if any of it's chidren is already in the recursion stack and is visited
then the graph contains a cycle.

Using DFS.


*/

#include<bits/stdc++.h>

using namespace std;

bool iscyclic(int s,vector<vector<int> > &adj,vector<bool> &visited,vector<bool> &rec)
{

if(visited[s]==false){
//Mark the current node as visited and print it
visited[s]=true;
rec[s]=true;

vector<int> :: 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<vector<int> > 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<bool> visited(vertices,false) ;
vector<bool> rec(vertices,false) ;



int start=0;
bool cyclic=false;

for(int i=0;i<vertices;i++){
if(iscyclic(i,adj,visited,rec)){
cyclic=true;
break;

}
}



if(cyclic==true)
cout<<"Cycle is present";
else
cout<<"Cycle is not present";

return 0;
}

/*


INPUT:


0---->1

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

*/