-1

I'm trying to find all cycles in an undirected graph and didn't find any algorithm for the same in any online sites / geeksforgeeks.

There is one for directed graph (Johnson algorithm), but it isn't working (desired o/p) on undirected.

Any suggestions will be appreciated.

  • But the first google result that comes up points you to the geeksforgeeks site with this exact problem and a possible solution. – ACV Nov 08 '19 at 17:32
  • 1
    Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. We expect you to finish your basic research before asking here for help. StackOverflow is a knowledge base for *specific* programming problems -- not a design, coding, research, or tutorial resource. – Prune Nov 08 '19 at 17:37
  • Yes, I saw the link already and it doesn't works. Try to add one more edge in the example as in, addEdge(12, 9); Now it'll give incorrect results –  Nov 08 '19 at 18:47
  • Johnson's algorithm doesn't find *all* cycles. In a complete graph with n vertices there are more than n! cycles, since every permutation of vertices is a cycle; no algorithm can list them all in less than O(1) time per cycle, for O(n!) overall. – j_random_hacker Nov 09 '19 at 15:46

1 Answers1

1

Approach: Using the graph coloring method, mark all the vertex of the different cycles with unique numbers. Once the graph traversal is completed, push all the similar marked numbers to an adjacency list and print the adjacency list accordingly. Given below is the algorithm:

  • Insert the edges into an adjacency list.
  • Call the DFS function which uses the coloring method to mark the vertex.
  • Whenever there is a partially visited vertex, backtrack till the current vertex is reached and mark all of them with cycle numbers. Once all the vertexes are marked, increase the cycle number.
  • Once Dfs is completed, iterate for the edges and push the same marked number edges to another adjacency list.
  • Iterate in the another adjacency list and print the vertex cycle-number wise.

Time Complexity: O(N + M), where N is number of vertex and M is the number of edges. Auxiliary Space: O(N + M)

Source: https://www.geeksforgeeks.org/print-all-the-cycles-in-an-undirected-graph/

You can find the code there as well.

HK1911
  • 59
  • 8
  • Are you sure this is correct? Consider a fully connected graph with `V` vertices. Any subset of vertices will form a cycle. That's at least `2^V` cycles, and we're not even considering order. – Primusa Nov 08 '19 at 17:43
  • I can try the fully connected case, but i think it will only find one cycle and it will print, "Cycle 1 = a, b, c, d, e.." (all nodes) in that case. – HK1911 Nov 08 '19 at 17:46
  • 1
    Yes, I saw the link already and it doesn't works. Try to add one more edge in the example as in, addEdge(12, 9); Now it'll give incorrect results –  Nov 08 '19 at 18:48