# Question:How to obtain all cycles in a graph from its cycle basis

## Question:How to obtain all cycles in a graph from its cycle basis

Maple

The following  question I asked a long time ago.

The user Carl Love provided a nice answer regarding the counting of cycles. Back then, I was only interested in the number of cycles, but now I am also interested in finding out these cycles.

I noticed sand15's answer where he (or she) attempted to use the cycle basis to find all cycles. This approach is theoretically viable. However, it seems that his (her) implementation had some instances of missing cycles.  (mmcdara‘s answer is also missing some cycles)

So I brought up this question again to draw attention to it. The only distinction is that I may have specifically mentioned the use of a cycle basis method. Additionally, I may give another question:

• How do we generate all cycles with a specific length (by cycle basis)?

However, I remain open-minded about alternatives that do not involve a cycle basis.

The SageMath approach for that can be referred to the link, which is actually why I remembered this question. It feels like the function in linear algebra that generates all elements through a basis would be effective.

```g=graphs.OctahedralGraph()
def gen_simple_cycles(G):
C = [frozenset(tuple(sorted(e[:2])) for e in c) for c in G.cycle_basis(output='edge')]
for S in Subsets(C):
T = set()
for c in S:
T = T.symmetric_difference(c)
H = Graph(T, format='list_of_edges')
if H.is_eulerian() and max(H.degree(),default=0)==2:
yield H.eulerian_circuit(return_vertices=True)[1]
list(gen_simple_cycles(g))
```

```[[0, 4, 2, 0],
[0, 4, 3, 0],
[0, 4, 5, 1, 0],
[2, 5, 4, 2],
[3, 5, 4, 3],
[0, 3, 1, 0],
[0, 2, 1, 0],
[0, 3, 4, 2, 0],
[0, 2, 4, 5, 1, 0],
[0, 4, 5, 2, 0],
[0, 4, 2, 1, 0],
[0, 3, 4, 5, 1, 0],
[0, 4, 5, 3, 0],
[0, 4, 3, 1, 0],
[0, 4, 2, 5, 1, 0],
[0, 4, 3, 5, 1, 0],
[0, 4, 5, 1, 3, 0],
[0, 4, 5, 1, 2, 0],
[2, 5, 3, 4, 2],
[0, 3, 1, 2, 0],
[0, 3, 4, 5, 2, 0],
[0, 3, 5, 4, 2, 0],
[0, 2, 4, 3, 1, 0],
[0, 3, 4, 2, 1, 0],
[0, 2, 5, 1, 0],
[0, 2, 4, 3, 5, 1, 0],
[0, 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