Skip to content

Commit

Permalink
Update simple_cycles docstring w/ yields and examples (networkx#5709)
Browse files Browse the repository at this point in the history
Update docstring for simple_cycles.
  • Loading branch information
rossbar authored and cvanelteren committed Apr 22, 2024
1 parent 658a32b commit 102e347
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions networkx/algorithms/cycles.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,26 @@ def simple_cycles(G):
G : NetworkX DiGraph
A directed graph
Returns
-------
cycle_generator: generator
A generator that produces elementary cycles of the graph.
Yields
------
list of nodes
Each cycle is represented by a list of nodes along the cycle.
Examples
--------
>>> 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
>>> sorted(nx.simple_cycles(G))
[[0], [0, 1, 2], [0, 2], [1, 2], [2]]
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()
>>> copyG.remove_nodes_from([1])
>>> copyG.remove_edges_from([(0, 1)])
>>> len(list(nx.simple_cycles(copyG)))
3
copy your graph and eliminate those nodes or edges before calling.
For example, to exclude self-loops from the above example:
>>> H = G.copy()
>>> H.remove_edges_from(nx.selfloop_edges(G))
>>> sorted(nx.simple_cycles(H))
[[0, 1, 2], [0, 2], [1, 2]]
Notes
-----
Expand Down

0 comments on commit 102e347

Please sign in to comment.