-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectACycleInUndirectedGraphUsingBFS.cpp
More file actions
44 lines (39 loc) · 1.27 KB
/
Copy pathDetectACycleInUndirectedGraphUsingBFS.cpp
File metadata and controls
44 lines (39 loc) · 1.27 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
43
44
// Q155 https://www.codingninjas.com/codestudio/problems/1062670?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=1
// Time: O(E+V) //BFS
// Space: O(E+V)+O(V)+O(V)
#include<bits/stdc++.h>
bool bfscycle(int s,vector<bool> &vis,vector<int> adj[]){
queue<pair<int,int>> q;
vis[s]=true;
q.push({s,-1});
sort(adj[s].begin(),adj[s].end());
while(!q.empty()){
int node=q.front().first;
int par=q.front().second;
q.pop();
for(auto it:adj[node]){
if(!vis[it]){
vis[it]=true;
q.push({it,node});
}
else{
if(par!=it) return true;
}
}
}
return false;
}
string cycleDetection (vector<vector<int>>& edge, int v, int e){
vector<bool> vis(v+1,false);
vector<int> adj[v+1]; //vector of arrays
for(int i=0;i<e;i++){
adj[edge[i][0]].push_back(edge[i][1]);
adj[edge[i][1]].push_back(edge[i][0]);
}
for(int i=1;i<=v;i++){
if(!vis[i]){
if(bfscycle(i,vis,adj)) return "Yes";
}
}
return "No";
}