implement bread first search optimized for space