forked from Master-Helix/DSA-Graphs_Important_Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetect_Cycle_UNDIRECTED.cpp
More file actions
37 lines (34 loc) · 866 Bytes
/
Copy pathDetect_Cycle_UNDIRECTED.cpp
File metadata and controls
37 lines (34 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
bool DFS(vector<int> adj[],int i,bool visited[],int parent)
{
visited[i]=true;
for(int x:adj[i])
{
if(visited[x]==false)
{
if(DFS(adj,x,visited,i)==true)
{
return true;
}
}
else if(x!=parent)
{
return true;
}
}
return false;
}
bool isCycle(int V, vector<int> adj[]) {
bool visited[V]={false};
for(int i=0;i<V;i++)
{
if(visited[i]==false)
{
if(DFS(adj,i,visited,-1)==true)
{
return true;
}
}
}
return false;
// Code here
}