find_cycle

Contents

find_cycle#

graph_tool.structure.find_cycle(g, source=None, edges=False)[git][source]#

Return a cycle of the graph, if there is one.

Parameters:
gGraph

Graph to be used.

sourceVertex or int (optional, default: None)

If given, only the cycles reachable from this vertex are considered.

edgesbool (optional, default: False)

If True, the edge indices of the cycle are returned instead of its vertices.

Returns:
cyclenumpy.ndarray or None

The vertices of the cycle in order (the edge from the last to the first vertex closes it), or its edge indices, or None if there is no cycle.

See also

is_DAG

Test for directed acyclic graphs

girth

Length of the shortest cycle

cycle_basis

Basis of the cycle space

find_negative_cycle

Cycle of negative total weight

Notes

A depth-first search is made, which stops at the first edge that closes a cycle: for directed graphs, an edge to a vertex on the current search path, and for undirected graphs, any edge to a visited vertex other than the one through which the current vertex was reached. Self-loops are cycles of length one, and parallel edges of undirected graphs cycles of length two. The algorithm runs in time \(O(V + E)\). The shortest cycle is given by girth(), and all of them by all_circuits().

Examples

>>> g = gt.Graph([(0, 1), (1, 2), (2, 3), (3, 1)])
>>> print(gt.find_cycle(g))
[1 2 3]