Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update simple_cycles docstring w/ yields and examples #5709

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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