Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There is no need to reverse and eliminate cycles - instead, to fix a cycle direction it's enough to compare the second vertex with the second but last vertex:

undirected_cycles = [ c in g.to_directed().all_simple_cycles() if len(c)>=4 and c[1]<c[-2] ]

There is no need to reverse and eliminate cycles - instead, to fix a cycle direction it's enough to compare the second vertex with the second but last vertex:

g = graphs.CompleteGraph(4)
undirected_cycles = [ c for c in g.to_directed().all_simple_cycles() if len(c)>=4 and c[1]<c[-2] ]

Alternatively all cycles can be constructed from .cycle_basis() like:

def gen_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')
        yield H.eulerian_circuit(return_vertices=True)[1]

list( gen_cycles( graphs.CompleteGraph(4) ) )

There is no need to reverse and eliminate cycles - instead, to fix a cycle direction it's enough to compare the second vertex with the second but last vertex:

g = graphs.CompleteGraph(4)
undirected_cycles = [ c for c in g.to_directed().all_simple_cycles() if len(c)>=4 and c[1]<c[-2] ]

Alternatively all cycles can be constructed from .cycle_basis() like:

def gen_cycles(G):
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 max(H.degree(),default=0)==2:
            yield H.eulerian_circuit(return_vertices=True)[1]

list( gen_cycles( gen_simple_cycles( graphs.CompleteGraph(4) ) )

There is no need to reverse and eliminate cycles - instead, to fix a cycle direction it's enough to compare the second vertex with the second but last vertex:

g = graphs.CompleteGraph(4)
undirected_cycles = [ c for c in g.to_directed().all_simple_cycles() if len(c)>=4 and c[1]<c[-2] ]

Alternatively Alternatively, all simple cycles can be constructed from .cycle_basis() like::

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 max(H.degree(),default=0)==2:
            yield H.eulerian_circuit(return_vertices=True)[1]

list( gen_simple_cycles( graphs.CompleteGraph(4) ) )

There is no need to reverse and eliminate cycles - instead, to fix a cycle direction it's enough to compare the second vertex with the second but last vertex:

g = graphs.CompleteGraph(4)
undirected_cycles = [ c for c in g.to_directed().all_simple_cycles() if len(c)>=4 and c[1]<c[-2] ]

Alternatively, all simple cycles can be constructed from .cycle_basis():

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( graphs.CompleteGraph(4) ) )