A utility function to print the adjacency list representation of graph
static void printGraph ( ArrayList < ArrayList < Integer > > adj ) { for ( int i = 0 ; i < adj . size ( ) ; i ++ ) { System . out . println ( " Adjacency list of vertex " + i); System . out . print ( " head " ) ; for ( int j = 0 ; j < adj . get ( i ) . size ( ) ; j ++ ) { System . out . print ( " ▁ - > ▁ " + adj . get ( i ) . get ( j ) ) ; } System . out . println ( ) ; } }