title: Find all cycles in undirected graph
body: <p>I am using <a href="http://algs4.cs.princeton.edu/41undirected/" rel="nofollow">Algorithms 4th edition</a> to polish up my graph theory a bit. The books comes with a lot of code for graph processing.<br>
  Currently, I am stuck with the following problems: How to find all cycles in an undirected graph?
  I was looking to modify the <a href="http://algs4.cs.princeton.edu/41undirected/Cycle.java.html" rel="nofollow">existing code for cycle detection</a> to do that.</p>
  
  <p>Here is the important part:</p>
  
  <pre><code>private void dfs(Graph G, int u, int v) {
          marked[v] = true;
          for (int w : G.adj(v)) {
  
              // short circuit if cycle already found
              if (cycle != null) return;
  
              if (!marked[w]) {
                  edgeTo[w] = v;
                  dfs(G, v, w);
              }
  
              // check for cycle (but disregard reverse of edge leading to v)
              else if (w != u) {
                  cycle = new Stack&lt;Integer&gt;();
                  for (int x = v; x != w; x = edgeTo[x]) {
                      cycle.push(x);
                  }
                  cycle.push(w);
                  cycle.push(v);
              }
          }
      }
  </code></pre>
  
  <p>Now, if I were to find ALL cycles, I should remove the line that returns when a cycle is found and each time a cycle is created I would store it. The part I cannot figure out is: when does the algorithm stop?  How can I be sure I have found all cycles?</p>
  
  <p>Can the above code even be modified in a way to allow me to find all cycles?</p>