adjacency list for storing graph
ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } adj . get ( 0 ) . add ( 1 ) ; adj . get ( 0 ) . add ( 3 ) ; adj . get ( 1 ) . add ( 0 ) ; adj . get ( 1 ) . add ( 2 ) ; adj . get ( 2 ) . add ( 1 ) ; adj . get ( 2 ) . add ( 3 ) ; adj . get ( 3 ) . add ( 0 ) ; adj . get ( 3 ) . add ( 2 ) ; boolean ans = isBipartite ( V , adj ) ;