Short answer: Floyd’s cycle-finding algorithm detects a cycle in a sequence defined by x, f(x), f(f(x)), … with two pointers and no stored history. The tortoise advances one step and the hare two. If there is a cycle, they meet. The meeting point is then used to find the start of the cycle: reset one pointer to the start and advance both by one step. They meet at the cycle entrance. The time is linear in the tail length plus the cycle length, and the memory is a handful of values.
What the problem actually asks
A linked structure or a functional graph can loop. Storing every seen value detects the loop and costs memory proportional to the tail plus the cycle. If the state itself is the only storage, two speeds suffice. Inside a cycle of length λ, the hare gains one step per iteration on the tortoise, so it catches up in at most λ iterations after both are on the cycle.
The second phase uses a standard congruence. If the tail has length μ and the meeting happened at a point that is k steps into the cycle, then moving one pointer to the start and walking both in lockstep meets at the entrance. Brent’s algorithm changes the outer schedule and often uses fewer function evaluations. Floyd’s schedule is the one with the short proof.
What the algorithm guarantees
If the sequence eventually cycles, the hare meets the tortoise. If the sequence runs into a sink with no cycle, the implementation must define f at the end or the hare runs off the structure. On a rho-shaped functional graph the meeting happens, the entrance is found, and the cycle length is the number of steps to return to the meeting point. Time is O(μ+λ) function evaluations. Extra memory is O(1).
The algorithm finds one cycle reachable from the start. It does not list every cycle in a general graph. A graph cycle that is not a functional iteration needs a depth-first search and a back edge.
The working steps
- Set tortoise and hare to the start. Advance the tortoise by one application of f and the hare by two, and repeat until they are equal or a terminal state appears.
- If they never meet, the sequence does not cycle from this start.
- Leave the tortoise at the meeting point. Reset the hare to the start. Advance both one step at a time until they meet. That point is the first repeated state, the cycle entrance.
- The cycle length is the number of steps to walk from the entrance back to itself, or from the meeting point around to itself.
- Pollard’s rho uses this loop with f(x) = x²+c modulo n and a gcd in place of an equality test on hidden residues.
The O(1) memory bound is for a functional sequence, one successor per state. A general directed graph can have several successors, and the tortoise and hare do not search it. Depth-first search does. Brent’s cycle algorithm is the alternative schedule with power-of-two resets.
Approaches that look simpler and fail
| Approach | Why it fails when every seen state is stored and called Floyd’s algorithm |
|---|---|
| Insert each value into a set until a repeat | The repeat is detected and the memory is proportional to the number of distinct values. Floyd’s algorithm uses two pointers and constant memory. |
| Reset both pointers to the start after they meet | The entrance-finding phase keeps one pointer at the meeting point and moves the other from the start. Resetting both loses the congruence. |
| Use the method to list cycles in a general directed graph | A node with two outgoing edges is not a function. The back-edge classification of a depth-first search is the graph algorithm. |
| Assume the first meeting point is the cycle entrance | The meeting point can be anywhere on the cycle. The second phase finds the entrance. |
A different evaluation schedule is Brent’s algorithm. A factored integer from a polynomial walk is Pollard’s rho. Floyd’s algorithm is the tortoise-and-hare cycle finder.
A working example
This Python fragment moves a tortoise one step and a hare two steps on a functional graph. They meet on the cycle. A second lockstep walk finds the entrance. The tail length is 3 and the cycle length is 3.
# 0->1->2->3->4->5->3
succ = [1, 2, 3, 4, 5, 3]
def f(x):
return succ[x]
tortoise = hare = 0
while True:
tortoise = f(tortoise)
hare = f(f(hare))
if tortoise == hare:
break
hare = 0
mu = 0
while tortoise != hare:
tortoise, hare = f(tortoise), f(hare)
mu += 1
lam = 1
walker = f(tortoise)
while walker != tortoise:
walker = f(walker)
lam += 1
assert (mu, lam) == (3, 3)
print(mu, lam)
Where it is used
Floyd’s tortoise and hare finds a cycle in a linked structure and is the cycle walk inside Pollard rho. Brent’s variant resets on powers of two and is the one factoring code usually runs. Either walk assumes the function is deterministic.
When you can drop this algorithm
Drop a set of seen states when the sequence is long and only the cycle matters. Use the two pointers. Keep a set when the states are cheap to store and a constant-memory proof is unnecessary.
Why this stays published
We publish this so a search for Floyd’s cycle algorithm returns the two phases, the meeting on the cycle and the lockstep walk that finds the entrance. Floyd’s cycle algorithm detects a cycle and its entrance with two pointers and constant memory, in time linear in the tail plus the cycle length.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
