networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturnsBackRef
find_cycle(G, source=None, orientation=None)

The cycle is a list of edges indicating the cyclic path. Orientation of directed edges is controlled by :None:None:`orientation`.

Parameters

G : graph

A directed/undirected graph/multigraph.

source : node, list of nodes

The node from which the traversal begins. If None, then a source is chosen arbitrarily and repeatedly until all edges from each node in the graph are searched.

orientation : None | 'original' | 'reverse' | 'ignore' (default: None)

For directed graphs and directed multigraphs, edge traversals need not respect the original orientation of the edges. When set to 'reverse' every edge is traversed in the reverse direction. When set to 'ignore', every edge is treated as undirected. When set to 'original', every edge is treated as directed. In all three cases, the yielded edge tuples add a last entry to indicate the direction in which that edge was traversed. If orientation is None, the yielded edge has no direction indicated. The direction is respected, but not reported.

Raises

NetworkXNoCycle

If no cycle was found.

Returns

edges : directed edges

A list of directed edges indicating the path taken for the loop. If no cycle is found, then an exception is raised. For graphs, an edge is of the form :None:None:`(u, v)` where u and :None:None:`v` are the tail and head of the edge as determined by the traversal. For multigraphs, an edge is of the form :None:None:`(u, v, key)`, where :None:None:`key` is the key of the edge. When the graph is directed, then u and :None:None:`v` are always in the order of the actual directed edge. If orientation is not None then the edge tuple is extended to include the direction of traversal ('forward' or 'reverse') on that edge.

Returns a cycle found via depth-first traversal.

See Also

simple_cycles

Examples

In this example, we construct a DAG and find, in the first call, that there are no directed cycles, and so an exception is raised. In the second call, we ignore edge orientations and find that there is an undirected cycle. Note that the second call finds a directed cycle while effectively traversing an undirected graph, and so, we found an "undirected cycle". This means that this DAG structure does not form a directed tree (which is also known as a polytree).

This example is valid syntax, but raise an exception at execution
>>> G = nx.DiGraph([(0, 1), (0, 2), (1, 2)])
... nx.find_cycle(G, orientation="original") Traceback (most recent call last): ... networkx.exception.NetworkXNoCycle: No cycle found.
>>> list(nx.find_cycle(G, orientation="ignore"))
[(0, 1, 'forward'), (1, 2, 'forward'), (0, 2, 'reverse')]
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.cycles.find_cycle

Local connectivity graph

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


GitHub : /networkx/algorithms/cycles.py#336
type: <class 'function'>
Commit: