First version of efficient breadth first algorithm