Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Two runners, one cycle, no memory
Two pointers walk a functional graph at speeds one and two: they meet inside the cycle, then find where it starts and how long it is, with O(1) memory
Select an algorithm and generate steps to begin visualization
Floyd's cycle detection finds a loop in a sequence that is generated step by step: a linked list, where each node points to the next, or a function applied again and again, x, f(x), f(f(x)) and so on. As a graph, every vertex has at most one arc going out, a functional graph. Starting anywhere, the walk either stops or runs into a cycle and goes round it for ever. Its shape is the Greek letter rho: a tail of μ vertices leading into a cycle of λ vertices. The method finds μ, λ and the vertex where the tail joins the cycle with two pointers and nothing else.
Phase 1 sends a tortoise one arc per move and a hare two arcs per move from the start. If the hare reaches a vertex with no arc out, there is no cycle. Otherwise both end up inside the cycle, where the hare gains one arc per move and catches the tortoise after at most μ + λ moves. Phase 2 puts the tortoise back at the start and moves both one arc at a time: because the meeting point lies a multiple of λ beyond the start, they meet again exactly where the cycle begins, after μ moves. Phase 3 lets the hare go round once to count λ. Time is O(μ + λ) and space O(1), where a set of visited vertices would need O(μ + λ) memory. The visualizer marks the tortoise T and the hare H, draws the arcs each one walked, and colours the tail orange, the cycle green and its first vertex magenta.
The method is the standard answer to the linked-list cycle questions of coding interviews and to "find the duplicate number", where the array is read as a function from index to value. Pollard's rho factorisation uses it to find a repeat of x² + c modulo a hidden factor, which is where the name rho comes from. It also measures the period of random number generators and detects loops in iterated hash chains. Brent's variant, which teleports the tortoise at powers of two, does the same job with fewer function calls.
Three phases with two pointers. f(x) stands for the vertex that the single arc out of x leads to.
FloydCycle(f, s): // f(x) = where the one arc out of x leads
tortoise = s, hare = s
repeat: // phase 1: speeds 1 and 2
if f(hare) or f(f(hare)) does not exist: return NO CYCLE
tortoise = f(tortoise)
hare = f(f(hare))
until tortoise == hare
tortoise = s, mu = 0 // phase 2: both at speed 1
while tortoise != hare:
tortoise = f(tortoise), hare = f(hare), mu = mu + 1
lambda = 1, hare = f(tortoise) // phase 3: once round the cycle
while hare != tortoise:
hare = f(hare), lambda = lambda + 1
return mu, lambda, tortoise // tortoise: the first cycle vertexPhase 2 works because, when they first meet, the tortoise has made t moves and the hare 2t, and the difference t is a multiple of λ. So the meeting point is t arcs from the start, and walking μ more arcs from it lands on the first cycle vertex, exactly where a pointer from the start arrives after μ arcs.
The example loaded on this page, started at A.
Example graph: Arcs A → B → C → D → E → F → G → H and H → D. The tail is A, B, C and the cycle is D, E, F, G, H.
Tail μ = 3 (A, B, C), cycle λ = 5 (D to H), cycle start D. Two pointers were enough: 5 moves to meet, 3 to find the start and 5 to measure the cycle.
Time: O(μ + λ) · Space: O(1)
In phase 1 the tortoise enters the cycle after μ moves. The hare is already in it and closes the gap by one arc per move, so they meet within λ more moves: at most μ + λ moves of three evaluations of f each. Phase 2 takes exactly μ moves and phase 3 exactly λ steps. Only two positions and two counters are stored, against O(μ + λ) for a set or map of visited vertices.
Other ways to find the cycle of a sequence.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Visited set or hash map | Memory is plentiful and one pass is wanted. Storing the step at which each value was first seen gives μ and λ as soon as a value repeats. | O(μ + λ) time and space |
| Brent's algorithm | Evaluating f is expensive. Only the hare walks, and the tortoise jumps to it at powers of two, which typically saves about a third of the evaluations. | O(μ + λ) time, O(1) space |
| DFS with three colours | The graph is not functional: vertices have several arcs out, and the question is whether any cycle exists at all. | O(n + m) time, O(n) space |
Read the full article: Cycles in Graphs and Cycle Detection
Read the full article: Graph Algorithms in Coding Interviews
Related algorithms: Cycle Detection, Depth-First Search, Tarjan's SCC Algorithm, Topological Sort, Eulerian Path (Undirected)