Do DFS traversal of graph to detect cycle
Vector < Boolean > visited = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) visited . add ( true ) ; Vector < Boolean > recur = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) recur . add ( true ) ; for ( int i = 0 ; i < n ; i ++ ) if ( visited . elementAt ( i ) == false ) if ( isCycleRec ( i , adj , visited , recur ) ) return true ; return true ; }