This problem, for me, looks very interesting. It was about to find a simple cycle (i.e. cycle where are not repeat nodes) in a directed graph.

My solution is going like this, i.e, this graph is a case problem: enter image description here

I know that there is a cycle in a graph, when you can find "back edges" in a depth-first-search (dashed in my picture in DFSTree), and for a moment I can sure for a few cycles, but not for all, simple cycles. Because, the directed egdes so important to from a cycle, i.e (0123) != (0321)

I'm thinking in make a dfs for each node with back-edges, but I'm not sure, and it's not clear. So, I ask you, if you guide me. Thanks!. enter image description here

Here is my count of simple loops for my case problem.

enter image description here enter image description hereenter image description here enter image description here enter image description here enter image description here


1 Answer 1


You may be interested in the algorithm presented in this paper:

Finding all the elementary circuits of a directed graph. Donald B. Johnson. SIAM J. COMPUT. Vol. 4, No. 1, March 1975

Abstract. An algorithm is presented which finds all the elementary circuits-of a directed graph in time bounded by O((n + e)(c + 1)) and space bounded by O(n + e), where there are n vertices, e edges and c elementary circuits in the graph. The algorithm resembles algorithms by Tiernan and Tarjan, but is faster because it considers each edge at most twice between any one circuit and the next in the output sequence.

The paper contains a complete algorithm.

  • $\begingroup$ Ok. The paper is perfect, but Can I go to anywher with my work, or just look the paper? I was searching to you introduce me to the solution, what are i forgeting with my idea? $\endgroup$ Commented Dec 6, 2012 at 21:32
  • 2
    $\begingroup$ Your main problem is that the dfs-tree is not unique (e.g., swap "1" with "3" in your diagram). You would need to look at all possible dfs-trees to enumerate all cycles. $\endgroup$
    – badroit
    Commented Dec 6, 2012 at 21:58
  • 1
    $\begingroup$ In case you need a Java implementation of this algorithm: github.com/1123/johnson $\endgroup$
    – user152468
    Commented Feb 14, 2015 at 9:26
  • 1
    $\begingroup$ @badroit link is broken... :( $\endgroup$ Commented Jul 9, 2016 at 6:11
  • $\begingroup$ @lalongooo, thanks yes, I replaced it. $\endgroup$
    – badroit
    Commented Jul 9, 2016 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.