template for set operations, breadth first search