Sub topic 12
Functional Graphs and Floyd's Tortoise and Hare
This chapter assumes you know three-color DFS from earlier chapters, can read Python, and are comfortable with Big-O notation. It covers a special kind of graph where you can detect a cycle using constant extra memory.
What a Functional Graph Is
A functional graph is a directed graph in which every vertex has exactly one outgoing edge. A function f that maps a finite set to itself defines one. Each value x has a single edge x -> f(x).
When you start at some x0 and keep applying f, you get the sequence x0, f(x0), f(f(x0)), .... The set is finite, so some value must eventually repeat. From then on the sequence loops forever. Drawn out, the path looks like the Greek letter rho (ρ): a tail that leads into a loop.
Two numbers describe that shape:
| Symbol | Meaning |
|---|---|
| `mu` | Index of the first element that lies on the cycle (the tail length) |
| `lam` | Number of vertices in the cycle |
Why Not Just Use DFS or a Set?
You could store every value you visit in a set and stop at the first repeat. That takes O(mu + lam) memory. Sometimes f is a formula rather than a stored graph, as in pseudorandom number generators, Pollard's rho factoring, or a linked list you can only walk. In those cases the visited values may be too many to store. Floyd's algorithm needs only two pointers.
The Algorithm
Floyd's algorithm runs in three phases:
- Find a meeting point. Move a tortoise one step at a time and a hare two steps at a time until they land on the same value.
- Find
mu. Reset the tortoise tox0. Move both pointers one step at a time. They meet at the start of the cycle. - Find
lam. Hold the tortoise still and walk the hare around the cycle until it comes back.
def floyd_cycle(f, x0):
tortoise, hare = f(x0), f(f(x0))
while tortoise != hare:
tortoise, hare = f(tortoise), f(f(hare))
mu, tortoise = 0, x0
while tortoise != hare:
tortoise, hare = f(tortoise), f(hare)
mu += 1
lam, hare = 1, f(tortoise)
while tortoise != hare:
hare = f(hare)
lam += 1
return mu, lam
nxt = [1, 2, 3, 4, 5, 3]
print(floyd_cycle(lambda x: nxt[x], 0)) # (3, 3)Why the Pointers Meet Where They Do
Write x_i for the i-th element of the sequence. Suppose phase 1 ends after i steps, with the tortoise at x_i and the hare at x_2i. Both pointers are on the cycle, and they match, so the gap 2i - i = i must be a multiple of lam. Also i >= mu, because the tortoise has to be on the cycle before the hare can catch it.
In phase 2, one pointer starts at x_0 and the other at x_i. After mu steps they sit at x_mu and x_(mu+i). Both positions are on the cycle and differ by a multiple of lam, so they are the same vertex. Neither pointer can match the other earlier, because the first pointer is still on the tail before step mu. The meeting point is therefore exactly the cycle entry.
Total time is O(mu + lam), and extra memory is O(1).
Common Errors
- Starting both pointers at
x0and checking equality before moving them. The loop exits at once with a false meeting. - Comparing objects by identity when
freturns new but equal values. Use==or compare keys. - Starting
lamat 0 instead of 1 in phase 3, which undercounts by one.
Exercise
For nxt = [2, 0, 3, 4, 2] and x0 = 1, what does floyd_cycle(lambda x: nxt[x], 1) return?
Show answer
(2, 3). The sequence is 1, 0, 2, 3, 4, 2, ..., so the cycle 2 -> 3 -> 4 -> 2 starts at index 2 and has 3 vertices.
Exercise
Phase 2 resets only the tortoise to x0 and leaves the hare at the meeting point. Why does this reset make the two pointers meet at the cycle entry?
Show answer
The meeting index i is a multiple of lam and is at least mu. After mu more single steps, the pointers are at xmu and x(mu+i). These are the same vertex because they differ by a multiple of the cycle length, and this is the first index where the pointer from x0 reaches the cycle.