title: Algorithm to find and print simple cycle in complexity of O(n) on undirected graph
body: <p>Given <code>graph G(V,E),</code> un-directed graph.  </p>
  
  <pre><code>|E| = m, |V| = n 
  </code></pre>
  
  <p>The graph's data structure is Adjacency list</p>
  
  <p>How to find and print simple cycle (or print that there is no such cycle) in complexity of <code>O(n)</code>?<br>
  (If there is cycle the output should be the list of vertex that are part of the cycle.)</p>
  
  <p>I know how to find cycle on complexity of <code>O(n)</code>, there is also gudies on the internet.<br>
  My problem is how to <strong>print</strong> it.</p>
  
  <p>This is what I tried to do:</p>
  
  <pre><code>DFS-CheckCycle ( Graph G)
  {
      p[] &lt;- null //parent array
      foreach v in V
          Color[u] &lt;- white
  
      foreach v in V
      {
          if (Color[u] = white) 
              Visit(u)
      }
  }
  
  Visit(Vertex u)
  {
      bool Cycle &lt;- false;
      Color[u] &lt;- gray
      foreach v in Adj[u]
      {
          p[v] &lt;- u
          if (Color[v] = white)
              Visit(v);
          else if (Color[v] = gray) 
          {
              Cycle &lt;- true;
              break;
          }
      }
  
      if(!Cycle)
          print "No Cycle"
      else
          PrintCycle(v,u)
  }
  
  
  
  PrintCycle(Vertex v, Vertex u)
  {
      if(v = u)
          print v;
      else
      {
          print v;
          PrintCycle(p[v], u);
      }
  }
  </code></pre>
  
  <p>remember that it needs to be <code>O(n)</code>.<br>
  My PrintCycle function doesn't print all the vertexes.</p>
  
  <p>I need some help how to print the vertexes of the cycle I found.</p>