title: Detecting a cycle in a directed graph using DFS?
body: <p>So, I was trying to find a cycle using DFS in a directed graph. Now, I know that if topological sort of a graph is not possible, then the graph contains a cycle. I made the following algorithm for topological sort. I am not sure where should I modify this code in order to <code>return true</code> or <code>false</code> and checking if my graph contains a cycle or not. Here is the algorithm that I use:</p>
  
  <pre><code>DFS-Loop (Graph G)
  
  1. Mark all vertices as unexplored.
  2. Set label = n // number of vertices
  3. For each vertex V (belonging to) G
     -- If V not yet explored,
     -- DFS (G,V)
  4. Set f(V) = current_label // here, f(V) is basically the position of V in the ordering
  5. current_label-- 
  
  Where DFS(G,V) will be something like: 
  
  1. Mark V as explored.
  2. For every vertex K belonging to Adj.(V)
     -- If K is not explored, 
     -- Call DFS(G,K)
  </code></pre>
  
  <p>Where should I add this check for containing a cycle or not?</p>
  
  <p>Thanks!</p>