-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetect_cycle_in_undirected_graph.cpp
More file actions
42 lines (41 loc) · 1.08 KB
/
Detect_cycle_in_undirected_graph.cpp
File metadata and controls
42 lines (41 loc) · 1.08 KB
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
38
39
40
41
42
https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1
//detect cycle in undirected graph
//TC - O(V+E) +O(N)
//SC - O(N)
bool bfs(int node,vector<int>adj[],vector<int>&vis){
queue<pair<int,int>>q; // node parent
q.push({node,-1});
vis[node]=1;
while(!q.empty()){
int curr = q.front().first;
int par = q.front().second;
q.pop();
for(auto it:adj[node]){
if(!vis[it]){
vis[it]=1;
q.push({it,node});
}else if(par!= it) return true;
}
}
}
bool dfsisCycle(int node,int parent,vector<int>adj[],int vis[]){
vis[node]=1;
for(auto it:adj[node]){
if(!vis[it]){
if(dfsisCycle(it,node,adj,vis) == true) return true;
}else if(it!= parent){
return true;
}
}
}
bool isCycle(int V, vector<int> adj[]) {
int vis[V];
memset(vis,0,V);
//vector<int>vis(V,0);
for(int i=0;i<V;i++){
if(!vis[i]){
if(dfsisCycle(i,-1,adj,vis) == true) return true;
}
}
return true;
}