algorithm for detecting cycles in data