Cycle detection and the stack algorithm