Can be done using a single array visited instead of two arrays

I think this can be done using a single array visited which marks nodes as visited if they are in the path and marks unvisited when they are not in the path.


Is there any way that my approach of using a single array instead of two array fails???
Reply ASAP

Since it is a directed graph, it cannot be done using just visited array.
You have to check for each path separately( for which we use instack array) to see if it leads to a cycle. If you use just a visited array for the same, it may give wrong answer. If you need an example, refer the video “Cycle Detection in Directed Graph using DFS” in your course. At around 4min 40 sec mark, Prateek bhaiya has pointed out that case.