Detect Cycle Undirected Graph, is a Graphs related problem and in this post we will see how we can solve this challenge in C++

Please note that it's Function problem i.e. you need to write your solution in the form of Function(s) only. Driver Code to call/invoke your function would be added by GfG's Online Judge. The structure of the class is as follows which contains an integer V denoting the no of vertices and a list of adjacency vertices. class Graph { int V; list *adj; public : Graph(int V); void addEdge(int v,int w); bool isCyclic(); }; You are required to complete this method Do DFS and if a visited vertex is encountered then return true In undirected graph if there is an edge between u and v then u has v as adj and v has u as adj.So obviously when the dfs for the adj of curr will happen then in its turn it will again see that previous curr as its adj. and it will be visited but is not actually a cycle ,so for each vertex will use "prev" which conatins the previous node from which it was called check if already visited or not else mark as visited visit the adj vertices visited vector do dfs and if at any time we encounter an already visited vertex then a cycle is present

Please check the main.cpp snippet for the solution.

This solution originally posted at: Github by @susantabiswas