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:
- Returns:
- cycle
numpy.ndarrayorNone The vertices of the cycle in order (the edge from the last to the first vertex closes it), or its edge indices, or
Noneif there is no cycle.
- cycle
See also
is_DAGTest for directed acyclic graphs
girthLength of the shortest cycle
cycle_basisBasis of the cycle space
find_negative_cycleCycle 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 byall_circuits().Examples
>>> g = gt.Graph([(0, 1), (1, 2), (2, 3), (3, 1)]) >>> print(gt.find_cycle(g)) [1 2 3]