public static boolean containsCycle ( int [ ] parents ) { for ( int i = 0 ; i < parents . length ; i ++ ) { int numAncestors = 0 ; int parent = parents [ i ] ; while ( parent != ParentsArray . WALL_POSITION ) { numAncestors += 1 ; if ( numAncestors > parents . length - 1 ) { return true ; } parent = parents [ parent ] ; } } return false ; }
Checks if a dependency tree represented as a parents array contains a cycle .