queue to do BFS .
Queue Q = new Queue ( ) ; distance [ u ] = 0 ; Q . Enqueue ( u ) ; visited [ u ] = true ; while ( Q . Count != 0 ) { int x = ( int ) Q . Dequeue ( ) ; for ( int i = 0 ; i < edges [ x ] . Count ; i ++ ) { if ( ( bool ) visited [ ( int ) edges [ x ] [ i ] ] ) continue ;