1

Given a data structure that resembles a linked list with a pointer pointing to next node and another pointer pointing to any random node, I have to print all the unique cycles that the structure has. Here is an example of this data structure:

enter image description here

Unique cycles here are (1,2,1), (2,3,4,5,2), (1,3,5,2,1), (3,4,3) etc. but (1,2,3,4,5,2,1), (1,2,3,4,3) is not a cycle. What should be the algorithm to print all of these unique cycles?

trent
  • 25,033
  • 7
  • 51
  • 90
  • Think of it as a graph, which you probably are because you tagged this `graph-theory`, and use [Tarjan's strongly connected components algorithm](https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm) – Guy Coder Oct 28 '20 at 17:55
  • @GuyCoder Actually it was me who added [tag:graph-theory], but I don't think that Tarjan's algorithm solves the problem. The graph in the picture has only one SCC (the whole graph) but the question is about finding the simple cycles. – trent Oct 29 '20 at 03:40
  • @trentcl Thanks. Now it looks interesting enough to try to solve, but not today, may tomorrow. – Guy Coder Oct 29 '20 at 09:05

0 Answers0