simple_cycles(G)
A :None:None:`simple cycle`
, or :None:None:`elementary circuit`
, is a closed path where no node appears twice. Two elementary circuits are distinct if they are not cyclic permutations of each other.
This is a nonrecursive, iterator/generator version of Johnson's algorithm . There may be better algorithms for some cases .
The implementation follows pp. 79-80 in .
The time complexity is $O((n+e)(c+1))$ for $n$ nodes, $e$ edges and $c$ elementary circuits.
A directed graph
A generator that produces elementary cycles of the graph. Each cycle is represented by a list of nodes along the cycle.
Find simple cycles (elementary circuits) of a directed graph.
>>> edges = [(0, 0), (0, 1), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)]
... G = nx.DiGraph(edges)
... len(list(nx.simple_cycles(G))) 5
To filter the cycles so that they don't include certain nodes or edges, copy your graph and eliminate those nodes or edges before calling
>>> copyG = G.copy()See :
... copyG.remove_nodes_from([1])
... copyG.remove_edges_from([(0, 1)])
... len(list(nx.simple_cycles(copyG))) 3
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.cycles.minimum_cycle_basis
networkx.algorithms.cycles.find_cycle
networkx.algorithms.cycles.recursive_simple_cycles
networkx.algorithms.cycles.cycle_basis
networkx.algorithms.cycles.simple_cycles
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them