Given the edges of a graph, I wish to create an algorithm in python to find all cycles where there are no other cycles within it, I have tried various ideas for several days with no 100% reliability.
For example,
This graph has the edges as follow:
[[0,1],[2,1],[0,2],[0,3],[3,1],[3,2]]
And there are 7 possible distinct cycles/loops:
[[0, 2, 1, 0], [0, 3, 2, 1, 0], [0, 3, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0], [0, 3, 2, 0], [1, 3, 2, 1]]
But the cycle [0,3,2,1,0]
has the cycle [0,2,1,0]
and [0,3,2,0]
embedded within it. Similarly [0,2,3,1,0]
has the cycles [0,3,2,0]
and [0,3,1,0]
embedded within it. Same goes for [0,3,1,2,0]
and [1,3,2,1]
.
Therefore, my python program should filter all that out and give
[[0,2,1,0],[0,3,1,0],[0,3,2,0]]
which are cycles with no other cycles within it.