Sort neighbors of every node and find the Kth node
for ( int i = 0 ; i < V ; i ++ ) { if ( g . get ( i ) . size ( ) > 0 ) Collections . sort ( g . get ( i ) , new Comparator < pair > ( ) { public int compare ( pair p1 , pair p2 ) { return p1 . first - p2 . first ; } } ) ;