FindCycle
✖
FindCycle
Details

- A cycle is also known as a circuit or loop.
- A cycle is a path with no repetitions of vertices or edges other than the starting and ending vertices.
- FindCycle gives a list of cycles. Each cycle is given as a list of edges.
- FindCycle will return an empty list if there is no cycle.
- FindCycle[g,kspec,All] finds all the cycles.
- For weighted graphs, FindCycle[g,k] gives all cycles with total weights less than k.
- FindCycle works with undirected graphs, directed graphs, and multigraphs.

Background & Context
- FindCycle attempts to find one or more distinct cycles in a graph. Cycles are returned as a list of edge lists or as {} if none exist. A cycle of a graph (more properly called a circuit when the cycle is identified using an explicit path with particular endpoints) is a consecutive sequence of distinct edges such that the first and last edges coincide at their endpoints. Cycle enumeration can be used for planning a cyclic route in many situations (subway, road trip, etc.), computing voltage or current in electronic circuits, or discovering infinite loops in computer programs.
- In general, FindCycle[g,kspec,s] attempts to find s cycles of length kspec. The count specification s may be omitted (in which case it is taken to be 1), may be a positive integer, or can be All. The length specification kspec may be a positive integer k (in which case it stands for cycles of length k or less), Infinity, a positive integer inside a list {k} (in which case it stands for cycles of length exactly k), or a list of two positive integers {kmin,kmax} (in which case it stands for cycles of lengths kmin through kmax).
- A graph for which FindCycle[g,{3}] returns {} is known as a triangle-free graph, and one for which FindCycle[g,{4}] returns {} is known as square free. A cycle of length n, where n is the number of vertices in a graph, is known as a Hamiltonian cycle, and a graph possessing such a cycle is said to be Hamiltonian.
- A graph that does not contain any cycle is called an acyclic graph and can be tested for using AcyclicGraphQ.
- FindCycle returns simple cycles, while FindHamiltonianCycle, FindEulerianCycle, and FindFundamentalCycles return specific types of cycles. FindPath may be used to find a path (a set of edges for which the endpoints do not coincide) between two specific vertices, returned as a set of consecutive vertices along the path.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases

https://wolfram.com/xid/0rsxyjhsu-co2cc7

https://wolfram.com/xid/0rsxyjhsu-r3wdf


https://wolfram.com/xid/0rsxyjhsu-vqpkgi


https://wolfram.com/xid/0rsxyjhsu-d3vhle

https://wolfram.com/xid/0rsxyjhsu-miia26

Scope (12)Survey of the scope of standard use cases
Specification (6)
FindCycle works with undirected graphs:

https://wolfram.com/xid/0rsxyjhsu-lfx0x8


https://wolfram.com/xid/0rsxyjhsu-15kl6n


https://wolfram.com/xid/0rsxyjhsu-ighgre


https://wolfram.com/xid/0rsxyjhsu-kbffo7

Use rules to specify the graph:

https://wolfram.com/xid/0rsxyjhsu-bndh30

FindCycle works with large graphs:

https://wolfram.com/xid/0rsxyjhsu-lkg873

https://wolfram.com/xid/0rsxyjhsu-b5ef0w

Enumeration (6)

https://wolfram.com/xid/0rsxyjhsu-eygfi

A cycle of length at most length 6:

https://wolfram.com/xid/0rsxyjhsu-1cmllb

A cycle of length between 3 and 5:

https://wolfram.com/xid/0rsxyjhsu-v6o6ke

A cycle that includes a given vertex:

https://wolfram.com/xid/0rsxyjhsu-lbl8wg


https://wolfram.com/xid/0rsxyjhsu-muxwzs

FindCycle gives an empty list if there is no cycle:

https://wolfram.com/xid/0rsxyjhsu-6c79w

Applications (4)Sample problems that can be solved with this function
Find Hamiltonian cycles that visit each vertex exactly once:

https://wolfram.com/xid/0rsxyjhsu-bu7km8


https://wolfram.com/xid/0rsxyjhsu-fvlylz


https://wolfram.com/xid/0rsxyjhsu-rffun

Find cycles with a given property:

https://wolfram.com/xid/0rsxyjhsu-b3mwr8


https://wolfram.com/xid/0rsxyjhsu-ilq0ns


https://wolfram.com/xid/0rsxyjhsu-j9l0ig

https://wolfram.com/xid/0rsxyjhsu-g4okdx

Find the longest loops in the Korean Busan Underground:

https://wolfram.com/xid/0rsxyjhsu-6b42fb
The length of the longest loops:

https://wolfram.com/xid/0rsxyjhsu-2jx5zd

https://wolfram.com/xid/0rsxyjhsu-cuq9c8


https://wolfram.com/xid/0rsxyjhsu-7e9to4


https://wolfram.com/xid/0rsxyjhsu-hy2i75


https://wolfram.com/xid/0rsxyjhsu-03yo19
For a given starting node, find 5 tours of at least length 20 that avoid streets with bad dogs:

https://wolfram.com/xid/0rsxyjhsu-eno3xt


https://wolfram.com/xid/0rsxyjhsu-mzrvk7

Properties & Relations (3)Properties of the function, and connections to other functions
Use FindHamiltonianCycle to find cycles that visit each vertex exactly once:

https://wolfram.com/xid/0rsxyjhsu-il8uue

https://wolfram.com/xid/0rsxyjhsu-jmb6at


https://wolfram.com/xid/0rsxyjhsu-cnl73p

EdgeCycleMatrix gives a basis for all cycles:

https://wolfram.com/xid/0rsxyjhsu-pydno


https://wolfram.com/xid/0rsxyjhsu-c2aec


https://wolfram.com/xid/0rsxyjhsu-1hd4g

https://wolfram.com/xid/0rsxyjhsu-bh8qbt

Find all cycles from FindCycle:

https://wolfram.com/xid/0rsxyjhsu-c3rmgu

FindCycle gives an empty list for acyclic graphs:

https://wolfram.com/xid/0rsxyjhsu-xshfy0


https://wolfram.com/xid/0rsxyjhsu-ffzjia


https://wolfram.com/xid/0rsxyjhsu-66fmhb

Possible Issues (1)Common pitfalls and unexpected behavior
FindCycle ignores self-loops:

https://wolfram.com/xid/0rsxyjhsu-die81e

https://wolfram.com/xid/0rsxyjhsu-xzhbfc

Wolfram Research (2014), FindCycle, Wolfram Language function, https://reference.wolfram.com/language/ref/FindCycle.html (updated 2015).
Text
Wolfram Research (2014), FindCycle, Wolfram Language function, https://reference.wolfram.com/language/ref/FindCycle.html (updated 2015).
Wolfram Research (2014), FindCycle, Wolfram Language function, https://reference.wolfram.com/language/ref/FindCycle.html (updated 2015).
CMS
Wolfram Language. 2014. "FindCycle." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/FindCycle.html.
Wolfram Language. 2014. "FindCycle." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/FindCycle.html.
APA
Wolfram Language. (2014). FindCycle. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FindCycle.html
Wolfram Language. (2014). FindCycle. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FindCycle.html
BibTeX
@misc{reference.wolfram_2025_findcycle, author="Wolfram Research", title="{FindCycle}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/FindCycle.html}", note=[Accessed: 10-July-2025
]}
BibLaTeX
@online{reference.wolfram_2025_findcycle, organization={Wolfram Research}, title={FindCycle}, year={2015}, url={https://reference.wolfram.com/language/ref/FindCycle.html}, note=[Accessed: 10-July-2025
]}