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.

Solution
Depth First Traversal can be used to detect cycle in a Graph. DFS for a connected graph produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is from a node to itself (selfloop) or one of its ancestor in the tree produced by DFS. In the following graph, there are 3 back edges, marked with cross sign. We can observe that these 3 back edges indicate 3 cycles present in the graph.

Detect Cycle in a directed graph using colors

In the previous post, we have discussed a solution that stores visited vertices in a separate array which stores vertices of current recursion call stack.

In this post a different solution is discussed. The solution is from CLRS book. The idea is to do DFS of given graph and while doing traversal, assign one of the below three colors to every vertex.

WHITE : Vertex is not processed yet.  Initially
        all vertices are WHITE.

GRAY : Vertex is being processed (DFS for this 
       vertex has started, but not finished which means
       that all descendants (ind DFS tree) of this vertex
       are not processed yet (or this vertex is in function
       call stack)

BLACK : Vertex and all its descendants are 
        processed.

While doing DFS, if we encounter an edge from current 
vertex to a GRAY vertex, then this edge is back edge 
and hence there is a cycle.

Below is C++ implementation based on above idea.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20DFS%20based%20approach%20to%20find%20if%20there%20is%20a%20cycle%0A%2F%2F%20in%20a%20directed%20graph.%20%20This%20approach%20strictly%20follows%0A%2F%2F%20the%20algorithm%20given%20in%20CLRS%20book.%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Aenum%20Color%20%7BWHITE%2C%20GRAY%2C%20BLACK%7D%3B%0A%20%0A%2F%2F%20Graph%20class%20represents%20a%20directed%20graph%20using%0A%2F%2F%20adjacency%20list%20representation%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E*%20adj%3B%20%2F%2F%20adjacency%20lists%0A%20%0A%20%20%20%20%2F%2F%20DFS%20traversal%20of%20the%20vertices%20reachable%20from%20v%0A%20%20%20%20bool%20DFSUtil(int%20v%2C%20int%20color%5B%5D)%3B%0Apublic%3A%0A%20%20%20%20Graph(int%20V)%3B%20%20%2F%2F%20Constructor%0A%20%0A%20%20%20%20%2F%2F%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%0A%20%0A%20%20%20%20bool%20isCyclic()%3B%0A%7D%3B%0A%20%0A%2F%2F%20Constructor%0AGraph%3A%3AGraph(int%20V)%0A%7B%0A%20%20%20%20this-%3EV%20%3D%20V%3B%0A%20%20%20%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20add%20an%20edge%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%20%2F%2F%20Add%20w%20to%20v’s%20list.%0A%7D%0A%20%0A%2F%2F%20Recursive%20function%20to%20find%20if%20there%20is%20back%20edge%0A%2F%2F%20in%20DFS%20subtree%20tree%20rooted%20with%20’u’%0Abool%20Graph%3A%3ADFSUtil(int%20u%2C%20int%20color%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20GRAY%20%3A%20%20This%20vertex%20is%20being%20processed%20(DFS%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20for%20this%20vertex%20has%20started%2C%20but%20not%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20ended%20(or%20this%20vertex%20is%20in%20function%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20call%20stack)%0A%20%20%20%20color%5Bu%5D%20%3D%20GRAY%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20through%20all%20adjacent%20vertices%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%20%20%20for%20(i%20%3D%20adj%5Bu%5D.begin()%3B%20i%20!%3D%20adj%5Bu%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20v%20%3D%20*i%3B%20%20%2F%2F%20An%20adjacent%20of%20u%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20is%0A%20%20%20%20%20%20%20%20if%20(color%5Bv%5D%20%3D%3D%20GRAY)%0A%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20v%20is%20not%20processed%20and%20there%20is%20a%20back%0A%20%20%20%20%20%20%20%20%2F%2F%20edge%20in%20subtree%20rooted%20with%20v%0A%20%20%20%20%20%20%20%20if%20(color%5Bv%5D%20%3D%3D%20WHITE%20%26%26%20DFSUtil(v%2C%20color))%0A%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Mark%20this%20vertex%20as%20processed%0A%20%20%20%20color%5Bu%5D%20%3D%20BLACK%3B%0A%20%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20true%20if%20there%20is%20a%20cycle%20in%20graph%0Abool%20Graph%3A%3AisCyclic()%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20color%20of%20all%20vertices%20as%20WHITE%0A%20%20%20%20int%20*color%20%3D%20new%20int%5BV%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20color%5Bi%5D%20%3D%20WHITE%3B%0A%20%0A%20%20%20%20%2F%2F%20Do%20a%20DFS%20traversal%20beginning%20with%20all%0A%20%20%20%20%2F%2F%20vertices%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(color%5Bi%5D%20%3D%3D%20WHITE)%0A%20%20%20%20%20%20%20%20%20%20%20if%20(DFSUtil(i%2C%20color)%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%20to%20test%20above%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0A%20%20%20%20Graph%20g(4)%3B%0A%20%20%20%20g.addEdge(0%2C%201)%3B%0A%20%20%20%20g.addEdge(0%2C%202)%3B%0A%20%20%20%20g.addEdge(1%2C%202)%3B%0A%20%20%20%20g.addEdge(2%2C%200)%3B%0A%20%20%20%20g.addEdge(2%2C%203)%3B%0A%20%20%20%20g.addEdge(3%2C%203)%3B%0A%20%0A%20%20%20%20if%20(g.isCyclic())%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Graph%20contains%20cycle%22%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Graph%20doesn’t%20contain%20cycle%22%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A%0A” message=”” highlight=”” provider=”manual”/]

Output :

Graph contains cycle

Time complexity of above solution is O(V + E) where V is number of vertices and E is number of edges in the graph.

Categorized in: