How to find all simple cycles in an undirected graph efficiently?

How can we find all simple cycles in an undirected graph efficiently?

  • Can someone tell me, how to find those points which are forming cycle in a graph. Ex: let's a graph has following edges: 0 1 1 2 2 3 3 4 5 2 4 6 6 5 so here, 2,3,4,5,6 are points which are forming cycles in graph.

  • Answer:

    If you could do this efficiently you could find all cycles, take the largest and check to see if it was a Hamiltonian cycle.  So I think this is NP-Complete.  I guess a simple algorithm would be to just enumerate all the paths in the graph and check if they were cycles.  Simple but not efficient

Arjun Nayini at Quora Visit the source

Was this solution helpful to you?

Other answers

The main algorithm in http://www.sciencedirect.com/science/article/pii/S002437951400233X has to examine every simple chordless cycle in a particular graph, so it shouldn't be too bad to repurpose it to list those.  Once you have those you can form all of the simple cycles as unions of simple chordless cycles, but as others have mentioned there may be exponentially many simple cycles.

Justin Rising

Use dfs to find cycles in a graph as it saves memory. Maintain the dfs stack that  stores the "under processing nodes (gray color)" in the stack and - just keep track when a visited node is tried to be accessed by a new node. . Get the node which was already visited but was tried to be accessed. . Find the position in stack.(as the stack has visited nodes) right from this position move up till top of the stack , you will get the cycle in cyclic order. You may miss some sub cycles (cycles inside cycle) but you will never miss any disjoint cycle. (So you have a solution in case the graph has disjoint cycles). Actually it is not cool to find all cycles in an undirected graph as it would be of the order 2^V.( worst case - graph having V(V-1)/2 edges ). But still for sub cycle cases you can find the cycles from this algo by tracking it from the available cycles you have( figure it out yourself!! ). But it will make the complexity non plynomial. In case you need a robust solution brute force will be the only way out as said in previous posts.

Aditya Prakash

Each node gets its number as its value Now repeat the following steps: each node gives its value to everything it's connected to each node takes as its new value the max of the value it already had, and what it just received. stop repeating when values no longer change now group nodes by value: ones with equal value are in the same cycle. On further consideration, this is the algorithm for connected components, which is related but not the same. A node can be in two simple cycles but only in one connected component. I don't know if this algorithm can be extended to that question.

Victor Eijkhout

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.