Cute breadth first search algorithm