Optimized Breath_first_search