C Program to Detect Cycle in a Directed Graph Last Updated : 14 May, 2025 Comments Improve Suggest changes Like Article Like Report Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false. For example, the following graph contains three cycles 0->2->0, 0->1->2->0 and 3->3, so your function must return true. C++ // A C++ Program to detect cycle in a graph #include <iostream> #include <limits.h> #include <list> using namespace std; class Graph { int V; // No. of vertices list<int>* adj; // Pointer to an array containing adjacency lists bool isCyclicUtil(int v, bool visited[], bool* rs); // used by isCyclic() public: Graph(int V); // Constructor void addEdge(int v, int w); // to add an edge to graph bool isCyclic(); // returns true if there is a cycle in this graph }; Graph::Graph(int V) { this->V = V; adj = new list<int>[V]; } void Graph::addEdge(int v, int w) { adj[v].push_back(w); // Add w to v’s list. } // This function is a variation of DFSUytil() // in https:// www.geeksforgeeks.org/archives/18212 bool Graph::isCyclicUtil(int v, bool visited[], bool* recStack) { if (visited[v] == false) { // Mark the current node as visited and part of recursion stack visited[v] = true; recStack[v] = true; // Recur for all the vertices adjacent to this vertex list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) { if (!visited[*i] && isCyclicUtil(*i, visited, recStack)) return true; else if (recStack[*i]) return true; } } recStack[v] = false; // remove the vertex from recursion stack return false; } // Returns true if the graph contains a cycle, else false. // This function is a variation of DFS() // in https:// www.geeksforgeeks.org/archives/18212 bool Graph::isCyclic() { // Mark all the vertices as not visited and not part of recursion // stack bool* visited = new bool[V]; bool* recStack = new bool[V]; for (int i = 0; i < V; i++) { visited[i] = false; recStack[i] = false; } // Call the recursive helper function to detect cycle in different // DFS trees for (int i = 0; i < V; i++) if (isCyclicUtil(i, visited, recStack)) return true; return false; } int main() { // Create a graph given in the above diagram Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); if (g.isCyclic()) cout << "Graph contains cycle"; else cout << "Graph doesn't contain cycle"; return 0; } OutputGraph contains cycle Please refer complete article on Detect Cycle in a Directed Graph for more details! Comment More infoAdvertise with us Next Article C Program to Detect Cycle in a Directed Graph K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Graph Cycle Detection in C++ Detecting cycles in a graph is a crucial problem in graph theory that has various applications in fields like network analysis, databases, compilers, and many others. In this article, we will learn how to detect cycles in a graph in C++. Graph Cycle Detection in C++A cycle in a graph is a path that 7 min read Find if possible to visit every nodes in given Graph exactly once based on given conditions Given a Graph having N+1 nodes with 2*N-1 edges and a boolean array arr[ ], the task is to find if one can visit every nodes exactly once and print any one possible path. It's also known that N-1 edges go i-th to (i+1)-th node and rest edges go i-th to (n+1)-th node if arr[i] is 0 otherwise (n+1)-th 7 min read C++ Program to Implement Adjacency List An adjacency list is a way to represent a graph data structure in C++ using an array of linked lists. Each index of the array represents a vertex, and each element in its linked list represents the other vertices that form an edge with the vertex. In this article, we will learn how to implement an a 5 min read C Program For Detecting Loop In A Linked List Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Solution: Floyd's Cycle-Finding Algorithm Approach: This is the fastest method and has been described below: Traverse linked list using two pointers.Move one pointer(slow_p) by one and anoth 2 min read Number of single cycle components in an undirected graph Given a set of 'n' vertices and 'm' edges of an undirected simple graph (no parallel edges and no self-loop), find the number of single-cycle components present in the graph. A single-cyclic component is a graph of n nodes containing a single cycle through all nodes of the component. Example: Let us 9 min read Like