Skip to main content

floyd_partial

Function floyd_partial 

Source
pub fn floyd_partial<T, FS>(start: T, successor: FS) -> (usize, T, usize)
where T: Clone + PartialEq, FS: Fn(T) -> T,
Expand description

Identify a cycle in an infinite sequence using Floyd’s algorithm (partial version). Return the cycle size, an element in the cycle, and an upper bound on the index of the first element.

This function computes the cycle length λ and returns an element within the cycle, along with an upper bound μ̃ on the index of the first cycle element. The upper bound μ̃ satisfies μ ≤ μ̃ < μ + λ, where μ is the minimal index.

This is faster than floyd as it skips the computation of the minimal μ. The upper bound μ̃ is sufficient for many applications, such as computing f^n(x) for large n, where knowing the exact starting point of the cycle is not necessary.

§Example

use pathfinding::prelude::floyd_partial;

let (lam, _elem, mu_tilde) = floyd_partial(1, |x| (x * 2) % 7);
assert_eq!(lam, 3); // Cycle length
assert!(mu_tilde == 0); // Upper bound on mu (start is in cycle, so mu = 0)

§Warning

If no cycle exist, this function loops forever.