Implements breadth first search.