WOLFRAM

finds a cycle in the graph g.

FindCycle[g,k]

finds a cycle of length at most k in the graph g.

FindCycle[g,{k}]

finds a cycle of length exactly k.

FindCycle[g,{kmin,kmax}]

finds a cycle of length between kmin and kmax.

FindCycle[g,kspec,s]

finds at most s cycles.

FindCycle[{g,v},]

finds cycles that include the vertex v.

FindCycle[{vw,},]

uses rules vw to specify the graph g.

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 all

Basic Examples  (2)Summary of the most common use cases

Find a cycle in a graph:

Out[2]=2

Highlight the cycle:

Out[3]=3

Find all cycles in a graph:

Out[2]=2

Scope  (12)Survey of the scope of standard use cases

Specification  (6)

FindCycle works with undirected graphs:

Out[1]=1

Directed graphs:

Out[1]=1

Weighted graphs:

Out[1]=1

Multigraphs:

Out[1]=1

Use rules to specify the graph:

Out[1]=1

FindCycle works with large graphs:

Out[2]=2

Enumeration  (6)

A cycle of length exactly 8:

Out[1]=1

A cycle of length at most length 6:

Out[1]=1

A cycle of length between 3 and 5:

Out[1]=1

A cycle that includes a given vertex:

Out[1]=1

Find all cycles:

FindCycle gives an empty list if there is no cycle:

Out[1]=1

Applications  (4)Sample problems that can be solved with this function

Find Hamiltonian cycles that visit each vertex exactly once:

Out[1]=1
Out[2]=2

Show the cycle:

Out[3]=3

Find cycles with a given property:

Out[1]=1

Include a vertex:

Out[2]=2

Include an edge:

Find the longest loops in the Korean Busan Underground:

The length of the longest loops:

Out[3]=3

Find the loops:

Out[5]=5

Plan a dog walk tour:

For a given starting node, find 5 tours of at least length 20 that avoid streets with bad dogs:

Out[2]=2
Out[3]=3

Properties & Relations  (3)Properties of the function, and connections to other functions

Use FindHamiltonianCycle to find cycles that visit each vertex exactly once:

Out[2]=2

Equivalent to:

Out[3]=3

EdgeCycleMatrix gives a basis for all cycles:

Out[1]=1
Out[2]=2

Build all cycles:

Out[4]=4

Find all cycles from FindCycle:

Out[5]=5

FindCycle gives an empty list for acyclic graphs:

Out[1]=1
Out[2]=2
Out[3]=3

Possible Issues  (1)Common pitfalls and unexpected behavior

FindCycle ignores self-loops:

Out[2]=2
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).

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 ]}

@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 ]}

@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 ]}