Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Link to the question : https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1
- bool check(vector<int>adj[],vector<bool> &visit, int curr, int parent){
- visit[curr] = true;
- for(int i = 0 ; i< adj[curr].size(); i++){
- int ncurr = adj[curr][i];
- if(visit[ncurr]==false){
- if(check(adj,visit,ncurr,curr)==true)return true;
- }
- else{
- if(parent!=ncurr)return true;
- }
- }
- return false;
- }
- class Solution
- {
- public:
- //Function to detect cycle in an undirected graph.
- bool isCycle(int V, vector<int>adj[])
- {
- // Code here
- vector<bool> visit(V,false);
- for(int i = 0; i<V; i++){
- if(visit[i]==false){
- if(check(adj,visit,i,-1)==true)return true;
- }
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement