Cycle detection algorithms