Skip to content

Floyd's Cycle-Finding Algorithm

A constant-extra-space method that advances two references at one and two steps along the same successor chain to detect a reachable cycle.

Version
v1 · 2026-10-03 · History
Domain-specific #
13234
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomains
Algorithms, Data Structures → Computer Science & Software Engineering
Aliases
Tortoise and Hare Algorithm, Floyd's Tortoise and Hare

Core Idea

Floyd's cycle-finding algorithm (tortoise and hare) follows one deterministic successor chain from a chosen start with two references. The tortoise takes one step and the hare two each round; a meeting after they begin moving witnesses a reachable cycle. On a partial chain such as a linked list, the hare may instead reach null, proving no reachable loop. A first meeting detects the cycle but need not locate its entry or length.[1][2]

How would you explain it like I'm…

Turtle and Rabbit Race

A slow turtle and a fast rabbit start down the same path. The rabbit takes two steps every time the turtle takes one. If the path loops around like a circle, the rabbit will catch up to the turtle from behind. If the path just ends, the rabbit reaches the end first, and then you know there's no loop.

Tortoise and Hare Loop Finder

Floyd's Cycle-Finding Algorithm checks whether following 'go to the next one' over and over will ever bring you back somewhere you've been. You use two pointers: a tortoise that moves one step at a time and a hare that moves two. They start together, but that doesn't count as a meeting. If they land on the same spot later, there's a loop. If the hare hits a dead end, there's no loop. With a little more work you can also find where the loop starts and how long it is.

Two-Speed Pointer Cycle Detection

Floyd's Cycle-Finding Algorithm, also called the tortoise-and-hare method, asks whether repeatedly applying a 'successor' step from a start ever revisits a state. Two references move through the same sequence: the tortoise one step per round, the hare two. If they meet (ignoring their shared start), the sequence contains a cycle; if the hare reaches a missing successor, like the end of a linked list, there is no reachable loop. The meeting point is not necessarily where the loop begins, so a second phase resets one reference to the start and moves both one step at a time until they meet at the loop's entry. This is one specific method; others like Brent's method use a different schedule, and finding cycles in general graphs is a broader problem.

 

Floyd's Cycle-Finding Algorithm detects whether iterating a successor function from a given start eventually revisits a state. It maintains two references: the tortoise advances one successor per round and the hare two; their common starting point is ignored. A later meeting proves the reachable sequence cycles, while the hare hitting a null successor (as in a singly linked list) proves no loop is reachable. For a total function on a finite set, a revisit is guaranteed by pigeonhole, and the orbit decomposes into a nonrepeating prefix of length μ followed by a cycle of length λ. The first meeting only certifies cycling; the meeting state need not be the cycle's entry. A second phase resets one reference to the start and advances both singly, meeting at the entry (giving μ), and a further lap around the loop measures λ. It is one named technique, distinct from Brent's method and from general graph cycle detection with many outgoing edges. Its attribution is qualified: it is described as ascribed to Floyd by Knuth, which does not establish that Floyd published it.

Scope of Application

In a linked list, successor means a node's next pointer; in a finite iterated function, it means another function evaluation. A total map on finitely many states necessarily reaches a cycle, with tail length \(\mu\) and cycle length \(\lambda\). A list with a terminal null may not. MIT's mathematical-computation lecture and Cornell's linked-list exercise show these unlike settings for the same two-speed method.[3][4][1]

Clarity

Do not conflate the named method with all cycle detection. Brent's method uses a different comparison schedule; a branching-graph cycle search can discover cycles outside a single chosen orbit. Likewise, distinguish detecting repetition from finding its entry or period: resetting one reference to the start and moving both singly can find the entry, and a subsequent circuit counts the period.[2][1]

Manages Complexity

The method replaces a visited-state table with two moving positions. Once both are on a cycle, the hare gains one position per round modulo the cycle length, so a meeting follows. With constant-cost successor and equality, it uses \(O(\mu+\lambda)\) rounds and \(O(1)\) auxiliary references; the underlying data and the cost of generating a successor are separate. Saving memory can require extra evaluations, especially when entry and period are also wanted.[1][4]

Abstract Reasoning

First specify a unique successor from the start and whether it can terminate. Then advance one and two steps, compare only after movement, and guard a possibly absent fast successor. A later equality implies a reachable loop; termination implies none on that chain. Do not infer absence of cycles in unvisited graph branches. If entry is required, reset one reference to the start and advance both singly from the first meeting; the second equality is the cycle entry.[1]

Knowledge Transfer

A constructed list \(A\to B\to C\to D\to B\) gives a loop whose first fast-slow meeting can be at \(D\), while the entry is \(B\). A constructed finite map \(0\mapsto1\mapsto2\mapsto3\mapsto4\mapsto2\) has \(\mu=2\) and \(\lambda=3\). The same one/two-step and post-start equality roles operate on node identities and mathematical state values, but only the partial list can terminate at null. The proposed parent is live Algorithm, not Cycle or the live frontier-based Search Algorithm identity.[1][3]

Relationships to Other Abstractions

Local relationship map for Floyd's Cycle-Finding AlgorithmParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Floyd's Cycle-FindingAlgorithmDOMAINPrime abstraction: Algorithm — is a kind ofAlgorithmPRIME

Current abstraction Floyd's Cycle-Finding Algorithm Domain-specific

Parents (1) — outgoing typed relations

  • Floyd's Cycle-Finding Algorithm is a kind of Algorithm Prime

    Floyd's two-speed successor procedure is a specific algorithm for deciding whether one reachable orbit repeats.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Floyd's Cycle-Finding Algorithm sits in a moderately populated region (57th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.

Family — Distributed Systems Theorems & Fallacies (19 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] Cornell CS 2110, “Linked Data,” lecture 13, original course material, exercise 13.11. registry ↩a ↩b ↩c ↩d ↩e ↩f

[2] Peter Gammie, “The Tortoise and Hare Algorithm”, Archive of Formal Proofs (2015), original abstract. registry ↩a ↩b

[3] John Burkardt, “CYCLE_FLOYD — Cycle Detection by Floyd's Method”, original author-maintained software description. registry ↩a ↩b

[4] MIT Mathematics 18.783, Lecture 9 slides, original PDF, pp. 13–14. registry ↩a ↩b