-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathundirectedgraph.py
More file actions
32 lines (28 loc) · 837 Bytes
/
Copy pathundirectedgraph.py
File metadata and controls
32 lines (28 loc) · 837 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
Question
Given an undirected graph with V vertices and E edges, check whether it contains any cycle or not.
solution
bool depthfirstsearch(int v,int p,vector<int>adj[],vector <bool>&visited){
visited[v]=true;
for(auto i:adj[v]){
if(!visited[i]){
if(depthfirstsearch(i,v,adj,visited))
return true;
}
else if(i!=p){
return true;
}
}
return false;
}
bool isCycle(int V, vector<int> adj[]) {
// Code here
vector<bool>visited(V,false);
for(int i=0;i<V;i++){
if(!visited[i]){
bool flag=depthfirstsearch(i,-1,adj,visited);
if(flag)
return true;
}
}
return false;
}