Cycle in Directed Graph

Need Help Friends.

I implemented DFS algorithm to find cycle in a directed graph. Here’s my


[1] 

The program works fine but my problem is I want to find the entire cycle, i.e. all the nodes present in that cycle. Presently my program only outputs the starting node of a cycle in graph.

Can anyone Kindly guide me on how I can find all the nodes in that cycle.


  [1]: https://github.com/Shraeyas/General-Codes/blob/master/cycle.cpp

you can maintain a stack available in c++ stl . while going deep inside the graph push the vertices inside the stack if you have reached a vertex from no further path is available so while backtracking start poping out the vertices till you don’t get a vertex from which you can move in some other direction and again start pushing the vertices in stack and time you get your vertex which is completing the cycle than pop out from stack till that vertex those all vertices will be in the cycle .

2 Likes
//