queue to do BFS .
Q = queue . Queue ( ) NEW_LINE distance [ u ] = 0 NEW_LINE Q . put ( u ) NEW_LINE visited [ u ] = True NEW_LINE while ( not Q . empty ( ) ) : NEW_LINE INDENT x = Q . get ( ) NEW_LINE for i in range ( len ( edges [ x ] ) ) : NEW_LINE INDENT if ( visited [ edges [ x ] [ i ] ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT