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

Find cycle in Directed Graph 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 We do DFS here and we use rec_stack for keeping track of elements alreay on current DFS path.It is different from undirected in the sense that here only using visited vector we can't tell if there is a backlink or not.Eg: A ---> B---->C and A---->C. Here obviously C will already be visited but this downs't make a cycle Since there is no backlink pointing backwards on DFS path So we keep track of Path elements check if it is visited as well as in DFS path if it is not visited then make it and add to recursion stack visit the adj. ones remove the element from the recursion stack visited vector make a stack for storing the elements along the current DFS path 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